Assistant Professor, Electronics and Telecommunication Engineering, Walchank Institute of Technology, Solaapur. Today, we will discuss Timer Counter Programming Part 3. Learning outcome. At the end of this session, students will be able to describe key features of 8051 Timer Counter and its programming contents. In continuation of the previous lecture, we will study now Timer Counter Mode 2 and its programming example as well as Timer Counter Mode 3 and its programming example. Let us see how 8051 Timer Counter in Mode 2 works. Here, this is a 8-bit counter timer operation. If you look at, this is a TLX 8 bits and here THX 8 bits. That means, X stands for here either 0 or 1. If it is 0, then Timer 0 lower byte will be there and this is loaded from corresponding higher byte value. If you look at, we get input pulses from the previous stage which goes to the TLX 8 bits and when counter is started, this starts counting upwards and when it rolls over from all 1s to 0s, a TFX is generated and which in turn generates the interrupt. After that, this signal which is rolling from all 1s to all 0s is also triggered to load this THX value again into TLX. So, counting is performed in TLX while reload value is stored in THX. For example, if you load THX this one with 50H, then TLX will count from 50H in upward direction up to FFH and then TFX is generated and 50H is again reloaded in TLX. So, this way reloading operation is done after this TLX rolls from all 1s to all 0s. So, this mode is used for bout rate generation. For example, we will see programming timer counter 1 in mode 2. So, here what we are doing is we are writing a C program for counter 1 in mode 2 and display count in TL1 since we are using timer counter as counter and timer 1 we are using. So, the count will be in TL1 and this we are going to display on port 1. Here, assume a 1 hertz external clock is connected to pin T1. So, T1 is associated with the timer 1. After every 1 second a pulse on this will increment this TL1 and this will be available on port 1. So, this is what the program we write for doing this operation. Hash include reg51.h include file is there. Then we create a variable named as T1 of type and we assign a port 3.5 because T1 pin is available on port 3 pin and fifth bit. This is the main program. So, here this T1 equal to 1 the use of this is to initialize this port 3.1 as a input port because we are connecting 1 hertz external clock signal to T1. So, this port 3.5 pin will act as a input pin and here we are initializing T mod as 60 hexadecimal value. How this come we will see here. This is a T mod format and since we are using timer 1 higher 4 bits are used for programming lower 4 bits are not used. So, lower 4 bits are all 0s and higher 4 bits we are programming. So, here gate is made equal to 0 C oblique T bar since it is a counting operation this must be 1 and since we are using mode 2 this must be 1 0. So, this comes out to be 6 0 is the T mod value. Then TH1 is initialized to 0 because the counting starts from 0 and when we get first pulse the TL1 will have count value 1 second pulse 2 and so on and this is repeated forever. So, while this is 1 infinite loop start the timer TR1 that means timer 1 is started here and whatever count available in TL1 is transferred to port 1. So, this operation is done till the count reaches the maximum value that is 255 and when 255 count is reached TF1 will be generated. So, this is repeated this is done till TF1 equal to 0. If TF1 is 1 then again the counter is stopped by making TR1 equal to 0 TF1 equal to 0 and this is repeated again from the beginning. So, this is what the explanation of timer 1 in mode 2. Let us see timer counter mode 3. In this timer 0 in mode 3 uses TL0 and TH0 as two separate counters TL0 and TH0 as two separate counters. TR1 and TF1 are associated with the TH0 that means TR1 which is used to start and stop the timer is associated with TH0 and TF1 flag is associated with TH0 and TR0 and TF0 is associated with TL0. So, on timer 1 is same as setting TR1 equal to 0. So, timer 1 we cannot use if we use timer 0 because here timer 0 in mode 3 uses this TL0 and TH0. So, we cannot use timer 1 when we set timer 0 in mode 3. So, is same as setting TR1 equal to 0 means this is not used. Split mode is useful when two timers are required in addition to a bout rate generator. Let us have a question. What initial count value must be loaded in TH1 in mode 2 to generate a time delay of 100 microsecond? You pause the video and answer the question. As we see programming timer 1 in mode 2, mode 2 is a 8 bit operation. So, calculation of timer 1 reload value in mode 2 needed to achieve timer delay of 100 microsecond and oscillator frequency is 11.0592 megahertz. Since mode 2 uses 8 bit counting, the final count value is FFH which is equivalent to 255 decimal. The formula for finding initial count is same as before time delay is equal to final count minus initial count plus 1 whole multiplied by timer clock period. So, here time delay is given as 100 microsecond. So, it is 100 to 10 raised to minus 6 is equal to final count is 255 minus initial count which we want to find out plus 1 into timer clock period is 1.085 into 10 raised to minus 6 for 11.0592 megahertz oscillator frequency. So, initial count we get as 163.83 which must be rounded to the nearest integer as 164. If you convert it into hexadecimal we get A4H. So, the timer 1 is loaded with th1 equal to 0xA4H. Now, so programming timer 0 in mode 3. So, here what we are doing is we are going to generate a frequency of a square wave of 125 kilohertz on port 3.0 using mode 3 operation. Assume oscillator frequency is 11.0592 megahertz. So, we want to generate 125 kilohertz square wave on this port pin for that we use mode 3. So, here this is what the program written for this. So, this is main program which is used to complement CPL 3.0 complements bit after some delay that is A call delay is there S jump main. So, this some delay this P 3.0 is complemented and this delay should be found for 125 kilohertz square wave signal. How this is found just we will see. We are here using interrupt a method. So, that is why we enable the interrupt in I register by SETB EA instruction. Then we are enabling timer 0 operation by using SETB ET0 interrupt is enabled for timer 0. Then move th0 th0 is loaded with 0 fc. This value is found for generating a square wave of 125 kilohertz. How this value is found just look at. We need to make a delay program for the time period for half cycle of 125 kilohertz. That is why time is 1 by the frequency 125 into 10 raise to 3 means this is there is equal to 8 microsecond for full cycle. But what we require is for half cycle that is why it is a 4 microsecond. Since it is a 8 bit counting the final count is ffh that is 255 decimal value formula is same. We put the values time delay we required is a 4 microsecond. So, 4 into 10 raise to minus 6 final count is 255 initial count we want to find out plus 1 into this timer clock period. We get initial count is equal to 252.31 which must be rounded to 252 which comes out to be fc. So, timer 0 is loaded with th0 is equal to fch that is why this th0 value is loaded with fch. Then we start timer 1 because our control th0 is controlled by tr1 as we have seen previously. So, this is started and we wait for here this tf1 flag to generate. So, this the program will be in this loop till this tf1 is set. Once this is set again the timer clear tf1 again return to the main program. So, this way this delay routine will generate a time delay of 4 microsecond and after every 4 microsecond this CPL p3.0 will be complemented and so we get 125 kHz square wave on p3.0. These are my references the 8051 microcontroller architecture programming and applications by Kenneth Ayla and the 8051 microcontroller embedded system by Muhammad Ali Mazzadi. Thank you.