 Sudhakar Barbade, assistant professor, electronics and telecommunication engineering, Walchen Institute of Technology, Solabor. Today, we will discuss 8051 timers, oblique counters and its programming. Learning outcome, at the end of this session, students will be able to describe key features of 8051 timer counter and its programming contents. First we will see the basics of 8051 timer counter. Then the special function registers, T-mod and T-con, which are required for programming timer counter we will discuss and timer counter control logic is very important to understand the concept of programming timer counter in 8051. And lastly the programming of timer in mode 0 we will see. Let us see the timer counter features of 8051. 8051 has two 16 bit up timers and counters that means the counting is done in upward mode. The names of these two counters are T0 and T1 respectively. They can be configured to operate either as timers or as counters. The operation of the timers counters is controlled by two special function registers called SFRs that is T-mod and T-con. Now let us discuss timer mode control register T-mod. T-mod register is not bit addressable, T-mod address is 89H. So this is what the T-mod register, which is a 8 bit register, lower 4 bits are used for controlling timer 0, higher 4 bits are used for controlling timer 1. Here if you look at there is a gate bit, which is used to start and stop the timer. Gate bit is 0, timer can be started and stopped by using TR0 bit and if you want to start and stop the timer by external event, gate must be 1. So if it is 0, internally only we are controlling the timer operation and if this is 1 means external signal is controlling the start and stop of the timer. See oblique T-bar, it is a counter oblique timer, this bit. If this bit is 1, we are using timer as a counter and if it is 0, we are using as a timer. Then M0, M1 are mode selection bits, this is M0, M1, these are mode selection bits. If you look at there are 4 possibilities with 2 bits, when M1, M0 are 0, 0, we use it as 13 bit timer. When it is a 0, 1, we are using 16 bit timer, 1, 0, it is 8 bit or auto reload and when it is 1, 1, it is a split timer. Signal goes to the timer or counter and there are 2 controls here. This is one switch, this must be on so that whatever the signal on this line will be passed to the timer. Otherwise, if this switch is off, the signal to be counted or the clock to be counted will not go to the timer. If this C oblique T-bar is 0, then timer mode is selected. That means this switch is connected to this line and whatever the oscillator frequency of the system is divided by 12 and it goes to this line. If you want to use it this as a counter, then C oblique T-bar must be 1 and this switch position will be now moved to this line and so which is here, the signal will be whatever the signal on this will be available here. So what signal is available here is decided by the C oblique T-bar bit in the T-moder register. And if this bit is 0, then only this bit will be 1 and our gate output will be 1. This programming bit is used to switch on and off this switch. Interrupt to control the operation of this switch, then this gate must be 1. So in the timer function mode, the counter is incremented every machine cycle. Machine cycle has 12 T states, that's why hence the clock rate is one-twelfth of the oscillator frequency. Now we will see T-con register. This is used to control the timer counter operation. The lower 4 bits are used to control the operation of related with the interrupt and the higher 4 bits are used for timer control. T-con register is a bit addressable. We can individually access these bits and T-con address is 88H, TF1 bit number 1 overflow flag, it is set when timer rolls from all ones to zeros. It is cleared when processor execute ISR located at address 001BH. So ISR related with this timer 1 is 001BH. Similarly ISR related with the timer 0 is 000B. So this TF0 bit is used to indicate the overflow of a timer 0. So this is TF1, this is TF0. Now we will see what is TR1. TR1 run control bit set to 1 to start the timer counter. So when we set this bit the timer starts and when we reset it we stop the timer. So this is TR1 controlling timer 1 and TR0 which controls timer 0. TL and TX are the timer registers. These are 16 bit registers but this mode gives only 13 bit up counting that is why 8 plus 5 is 13 and this goes to this TLX when this overflows it goes to this THX and when this completely overflows a TFX is generated and which in turn generate the interrupt. So in this mode both T0 and T1 timers are used as a 13 bit up counter. When the counter rolls over from all 1s to all 0s TFX flags is set and interrupt is generated. The 13 bit counter can hold values between 0000 to 1FFFH in THX and TLX. So this count is maximum value the 13 bit register can hold. We load in the TLX and THX is initial count. So how to find out this? This is found by having whatever the required time delay is equal to final count. Now in this case the final count is what 1FFFH for 13 bit up counting. So this is the final count minus initial count which we want to find out and we know the desired time delay whatever we require we put it here plus 1 multiplied by timer clock period. So timer clock period how it is found just to look at. The timer clock period is 1.085 microseconds for a crystal of 11.059 to megahertz. As we saw already frequency given to the timer is divided by 12 much time period of the system. Now let us have a question what will be T-mod value to initialize timer 0 in mode 0. Please pause the video and answer the question. You might have thought over the question and the answer is 00H. Now we will see programming timer in mode 0 and there are two methods of programming polling method and interrupt method. So this algorithm or steps are given for programming timer in polling method to generate a time delay using timer in mode 0 following are the steps to load the T-mod value register indicating which timer is to be used. First we should load TLX and THX with the initial count value then we set start the timer and then you keep monitoring this TFX flag by using this JNVTFX instruction and get out of the loop when TFX becomes high. After that you stop the timer with this instruction CLRTRX instruction then clear the TF flag for the next round with clear TFX instruction and go back to step 2 to load TLX and THX again. So if this time delay is to be repeated then you go to the step 2. Programming mode 0 timer 0. So this is what the calculation done calculation of timer 0 reload value in mode 0 needed to achieve timer delay of 100 microsecond. Final frequency is 11.059 to megahertz. So here in 13 bit the final count is 1FFFH which is equivalent to 8191 decimal value. We put this value time delay is 100 microsecond that's why 100 into 10 raised to minus 6 final count is 8191 minus initial which we want to find out plus 1 multiplied by timer clock period for 11.059 to megahertz is this one already we discussed. From this we get this value 8099.84 which must be rounded to nearest integer which becomes 8100 decimal. If you convert into the hexadecimal it becomes 1FFFH. So timer 0 should be loaded with THX0 as 1F and TLX0 as A4. This is what programming of timer 0 in mode 0. So already we have found the initial count value for generating time delay of 100 microsecond. So first step is already we know T mod is to be loaded with 00H because we are using timer 0 in mode 0. Timer registers are to be loaded with found value TH with 1F TL equal to A4 and start the timer 0 and here you wait for TF0 to become 1. So here become 1 only when that timer 0 is over closed. After that you clear TR0, clear TF0 for the next operation and this is what the function to generate a time delay of 100 microsecond. This is what C version of the function we studied in the previous slide. So delay is a function here T mod is loaded with 00, hexadecimal X stands for hexadecimal value, TL0, TH0 are loaded with these values whatever we have found and we start the timer by setting this TR0 is equal to 1 and we wait for TF0 to become 1. And once it becomes 1 then against TR0 is reset, TF0 is reset for the next operation. These are my references for creating this presentation 8051 controller architecture programming and applications by Kenneth Ayla and another is 8051 microcontroller and embedded system by Muhammad Ali Mazdi. Thank you.