 Today, in continuation with the previous lecture, we will see Timer Counter Programming Part 2. Myself, Sudhakar Barabade, Assistant Professor, Electronics and Telecondition Engineering, Valchand Institute of Technology, Sulapur. Learning outcome, at the end of this session, students will be able to describe key features of 8051 Timer Counter and its programming contents. Here we will see Timer Counter Mode 1, next we will see Programming Timer Counter using polling method and Programming Timer Counter using Interruptor method. So these are the two methods to use Timer and Counter. This is what the diagram of how the Timer Counter in Mode 1 works. So here is the input from previous stage is given to the Timer registers, TLX with 8 bit, THX with 8 bits and this is what after all the bits of this 16 bit timer rolls from 1 to 0, TFX will be generated and after that Interrupted generated. So basically this mode is similar to Mode 0 except for the fact that both Timer 0 and Timer 1 are used as a 16 bit up counter. So instead of 13 bit with Mode 0, here Mode 1 uses 16 bit up counting. The 16 bit counter can hold values between 0, 0, 0 to FF, FFH. So the maximum count in hexadecimal, the 16 bit register can hold is FF, FFH. So this is loaded in TLX, X stands for here either of Timer 0 register or Timer 1 register. If X is 0 then it is a TH0, TL0 means Timer 0 register and if X is 1 this is a Timer 1 register. And the formula for finding the initial count which is to be loaded into this TLX and TLX is same as the previous one that is time delay equal to final count minus initial count plus 1 multiplied by Timer clock period, programming Timer in Mode 1. So basically how the calculation of initial count it is also called as reload value here calculation of Timer 0 reload value in Mode 1 needed to achieve Timer delay of 100 microsecond. Assume at oscillator frequency is 11.0592 megahertz. So here the final count is since it is a 16 bit the final count is FF, FFH in hexadecimal if you convert into decimal it comes out to be 65535. If you put this value final count into this equation initial count we have to find out time delay is 100 microsecond and Timer clock period is already we have discussed with this crystal the Timer clock period becomes 1.085 into 10 raised to minus 6. So these values are put here and from this initial count is found initial count comes out to be 65443.84 which must be rounded to the nearest integer and it comes 65444 if you convert this into hexadecimal 6B24H. So the Timer 0 should be loaded with value 6B into H0 and 24 in TL0. Here we are discussing programming of Timer 0 in Mode 1. This is example 1 using polling method the algorithm for polling method already we have discussed in the programming part 1 that is why we directly write here program this is a function to generate 100 microsecond delay using Timer 0. As we see here this Timer value is to be loaded with 01 because we are using Mode 1 here then TL0 and TA0 these values already we have found just now these are loaded then Timer is started by using this instruction set BTR0 and you wait for this counter to overflow from all 1s to all 0s and then TF0 flag will be set so we should wait here till this TF0 is set and once it is set that means Timer is overflowed and then again we have the counter by using this instruction CLRTR0 clear we clear the overflow flag and this is done to again restart the counting. Here we will discuss programmer in Mode 1 using interrupt method so this is what the implemented for interrupt method programming. The interrupt method makes use of register called interrupt enable register i register interrupt enable register bit addressable and this is what the format of i register here AES, ET1, EX1, ET0 and EX0 with related to our timer ET0 is a timer interrupt related with timer 0 and ET1 is a timer interrupt related with timer 1 and this is enabling the interrupt in a system. The ISR corresponding to interrupts are executed when the TFX flags of respective timers are raised. If you are programming in C then we require this interrupt number for timer 0 interrupt is 1 and for timer 1 it is 3. After execution of ISR controller returns to whatever it was doing earlier. So ISR is executed only when interrupt is occurred and after completing this ISR the processor continues where it was stopped. Now let us have a question what will be the hex code of i register to enable timer 0 in interrupt mode. You pause the video and answer the question. You might have thought over the question and the answer is to enable the interrupt this bit must be 1. These are don't care so for don't care we assume 0 so it is 1, 0, 0 this is also 0 this is related with the timer 1 and since we are using timer 0 so this is also 0 this is also 0 but this must be 1 because we are enabling timer 0 for interrupt so this must be 1 and this must be also 0. So this comes out to be 1, 0, 0, 0, 0, 0, 0, 1, 0 that means this is 8, 2. Programming timer in mode 1 using interrupt method. So these are the steps or we can say it is algorithm. This is algorithm to implement interrupt method of programming timer. To generate a time delay using timer in mode 1 following are the steps using interrupt method. Load the T-mod value register indicating which timer is to be used. Load registers TLX and THX with initial count value. Enable the timer interrupt by passing the hex value to I register. Start the timer with set B TRX instruction. So this is here we are using this interrupt enable register. Then last is write interrupt service routine for the timer interrupt. Here while writing interrupt service routine we should take care of these two constraints. If the timer has to be stopped after once the interrupt has occurred the ISR must contain the statement to stop the timer. Means if you want to execute the delay only once then timer must be stopped at the end of the ISR. And if ISR for timer interrupt has to be repeated again and again the timer run need not be cleared. Timer will start updating from 0, 0, 0, 0, 0, H and not to the initial value in case of mode 0 and mode 1. So the initial value must be reloaded in the interrupt service routine. So here what we should do is we should every time load the counter values in a counter registers in a ISR. This is what the C program for programming timer 0 in mode 1. Use of timer 0 in mode 1 for blinking LED with interrupt method. What we have done is here LED is connected to port 1 0th pin and this LED should blink with some delay and that delay program we are writing here. Here what we should do is you analyze the program and find the time delay generated. Assume associated frequency is 11.0592 megahertz. So here program is written and by observing the program and then analyzing the program you must find out the time delay. So let us see first the program. Here this is a main program hash include EG51.h include file is there. Then we are declaring this P1.0 port 1 0th bit pin as a LED variable assigned to this is LED of type S bit. Then interrupt service routine. Since the address of interrupt service routine related with timer 0 is 1 we are writing here interrupt 1 and ISR name is timer and here what we have done is just stated with the LED variable is complemented here. So the new value of the LED will be complement of the previous LED value. So this is a interrupt service routine and this is what the main program. Here the T-mod is loaded with hexadecimal value 0 1 because we are using timer 0 in mode 1. TH0 initial value we have not found but it is loaded 0 0. Here also 0 0. That means TL0 and TH0 values are 0 0. So from this information we should find out how much time delay we get and IE value is 8 2. Already we have found how to find out the value of this IE register. Then timer is started by this and this is to be repeated forever. That is why this infinite while loop is used. Now if you look at the analysis of the program what we require is to find out how much time delay we get. This is known to us final count since it is a 16-bit counting. Mode 1 means 16-bit counting. Final count is 65535 minus initial count here it is given that is 0 and plus 1 timer clock period. So from this we get a time delay of 71.1 millisecond. So these are references for preparing these slides. The 8051 micro controller architecture, programming and applications by Kenith Aila and second is the 8051 microcontroller and embedded system by Muhammad Ali Mazdi. Thank you.