{"id":221,"date":"2018-07-27T04:53:46","date_gmt":"2018-07-27T04:53:46","guid":{"rendered":"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=221"},"modified":"2018-07-27T05:06:38","modified_gmt":"2018-07-27T05:06:38","slug":"programming-timer-in-embedded-c","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/chapter\/programming-timer-in-embedded-c\/","title":{"rendered":"Programming Timer in Embedded C"},"content":{"raw":"<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In this lecture timer registers in C will be discussed.Details of counters in 8051 and delay length calculation will be discussed. Timer and counter programming in Embedded C is explained with example.<\/p>\r\n&nbsp;\r\n\r\n<strong>1. Timers<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">8051 has two timers. They are Timer 0 and Timer 1. It is independently configured to operate in a variety of modes as a timer or as an event counter.When operating as a timer, the timer\/counter runs for a programmed length of time, then issues an interrupt request. But when operating as a counter, the timer\/counter counts negative transitions on an external pin. After a preset number of counts, the counter issues an interrupt request.<\/p>\r\n&nbsp;\r\n\r\n<strong>1.1 Timers Registers<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Some of the basic timer registers are TMOD, TCON, THx and TLx.TMOD is used to select the mode of operation.TCON is used to control the timer to run or stop.The details of TCON register is shown below.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-224 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-124.png\" alt=\"\" width=\"711\" height=\"395\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em;text-align: initial\">THx and TLx registers are used to hold the count to generate desired time delays.TMOD is an 8-bit register in which lower 4 bits are for Timer 0 and upper 4 bits are for Timer 1. Lower 2 bits are used to set the timer mode while upper 2 bits is used to specify the operation.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>1.2 Timers Operation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Timer 0 and Timer 1 are 16 bit wide. It consists of registers THx and TLx (x= 0, 1) connected in cascade to form a 16-bit timer. It can be accessed as low byte and high byte. Lower byte register is referred to as TLx, while higher byte register is referred to as THx. The timer\/ counter is enabled only while the INTx is high and TRx control pin is set.Setting the run control bit (TRx) in TCON register turns the timer on by allowing the selected input to increment TLx. When TLx overflows it increments THx. But when THx overflows it sets the timer overflow flag (TFx) in TCON register. Setting the TRx does not clear the THx and TLx timer registers.BothTimer 0 and Timer 1 have four operating modes. These modes are selected by bit-pairs (M1, M0) in TMOD register. Modes 0, 1,and 2 are the same for both timers\/counters. The other basics of timer and counter is explained in 8051 Timer programming in Assembly Language in the previous chapter.<\/p>\r\n&nbsp;\r\n\r\n<strong>1.3 Steps to generate Time Delay<\/strong>\r\n\r\n&nbsp;\r\n\r\nFollowing are the steps to generate the time delay.\r\n\r\n&nbsp;\r\n\r\n1.\u00a0\u00a0\u00a0 Load the TMOD value register with mode and timer0 or 1.\r\n\r\n&nbsp;\r\n\r\n2.\u00a0\u00a0\u00a0 Load registers TL and TH with count corresponding to delay.\r\n\r\n&nbsp;\r\n\r\n3.\u00a0\u00a0\u00a0 Start the timer.\r\n\r\n&nbsp;\r\n\r\n4.\u00a0\u00a0\u00a0 Continuously check the timer flag (TF) with the \u201cJNB TFx,target\u201d instruction to see if it is raised, if it is raised(TF=1) then get out of the loop.\r\n\r\n&nbsp;\r\n\r\n5.\u00a0\u00a0\u00a0 Stop the timer.\r\n\r\n&nbsp;\r\n\r\n6.\u00a0\u00a0\u00a0 Clear the TF flag for the next round.\r\n\r\n&nbsp;\r\n\r\n7.\u00a0\u00a0\u00a0 Go back to Step 2 to load TH and TL again.\r\n\r\n&nbsp;\r\n\r\n<strong>2.\u00a0 <\/strong><strong>Embedded C program for 8051 Timer 0 and Timer 1<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The general purpose registers of 8051, such as R0-R7, A and B, are under the control of C compiler.They are not accessed directly by C statements.In case of SFRs, the entire RAM space of 80 \u2013 FFH is accessible directly using 8051 C statements.We can access the time registers TH, TL, and TMOD directly using the reg51.h header file.<\/p>\r\n&nbsp;\r\n\r\n<strong>2.1 Access timer registers in C<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Example 1 explained below gives the details about how to access 8051 timer registers like TH, TL, and TMOD in C and it also shows how to access TR and TF bits.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Example 1<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Write an 8051 C program to toggle all the bits of port P1 continuously with some delay in between. Use Timer 0, 16-bit mode to generate the delay.<\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid T0Delay(void);\r\n\r\n&nbsp;\r\n\r\nvoid main(void){\r\n\r\n&nbsp;\r\n\r\nwhile (1) {\r\n\r\n&nbsp;\r\n\r\nP1=0x55;\r\n\r\n&nbsp;\r\n\r\nT0Delay();\r\n\r\n&nbsp;\r\n\r\nP1=0xAA;\r\n\r\n&nbsp;\r\n\r\nT0Delay();\r\n\r\n&nbsp;\r\n\r\n}\u00a0 }\r\n\r\n&nbsp;\r\n\r\nvoid T0Delay(){\r\n\r\n&nbsp;\r\n\r\nTMOD=0x01;\r\n\r\n&nbsp;\r\n\r\nTL0=0x00;\r\n\r\n&nbsp;\r\n\r\nTH0=0x35;\r\n\r\n&nbsp;\r\n\r\nTR0=1;\r\n\r\n&nbsp;\r\n\r\nwhile (TF0==0);\r\n\r\n&nbsp;\r\n\r\nTR0=0;\r\n\r\n&nbsp;\r\n\r\nTF0=0;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nCalculating delay:\r\n\r\n&nbsp;\r\n\r\nFFFFH \u2013 3500H = CAFFH\r\n\r\n&nbsp;\r\n\r\n= 51967 + 1 = 51968\r\n\r\n&nbsp;\r\n\r\n=51968 \u00d7 1.085 \u03bcs = 56.384 ms is the approximate delay.\r\n\r\n&nbsp;\r\n\r\n<strong>2.1 Calculating delay using timer in C<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As mentioned in the previous chapter the delay length is depend on three factors. They are Crystal frequency, the number of clocks per machine cycle and the C compiler.To speed up the 8051, many recent versions of the 8051 have reduced the number of clocks per machine cycle from 12 to four, or even one. The frequency for the timer is always 1\/12th the frequency of the crystal attached to the 8051, regardless of the 8051 version. We can also use crystal frequency as the clock source. The following are the steps to find TH, TL register values. We can assume XTAL = 11.0592 MHz for 8051 systems.<\/p>\r\n&nbsp;\r\n\r\n<strong>Steps for finding the TH, TL register values<\/strong>\r\n\r\n&nbsp;\r\n\r\n1.\u00a0 Divide the desired time delay by 1.085 \u03bcs.\r\n\r\n&nbsp;\r\n\r\n2.\u00a0 Perform 65536 \u2013 n, where n is the decimal value we got in Step 1.\r\n\r\n&nbsp;\r\n\r\n3.\u00a0\u00a0 Convert the result of Step2 to hex, where yyxx is the initial hex value to be loaded\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\ninto the timer\u2019s register.\r\n\r\n&nbsp;\r\n\r\n4. Set TL = xx and TH = yy.\r\n\r\n&nbsp;\r\n\r\n<strong>Times 0\/1 Delay Using Mode 1 (16-bit Non-Auto-reload)<\/strong>\r\n\r\n&nbsp;\r\n\r\nExample 2 and 3 shows 8051 C programming of the timer 0 and timer 1 in mode 1 in a 16-bit non-auto reload mode.\r\n\r\n&nbsp;\r\n\r\n<strong>Example 2<\/strong>\r\n\r\n&nbsp;\r\n\r\nWrite an 8051 C program to toggle only bit P1.5 continuously every 50 ms. Use Timer 0, mode 1 (16-bit) to create the delay. Test the program on the (a) AT89C51 and (b) DS89C420.\r\n\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid T0M1Delay(void);\r\n\r\n&nbsp;\r\n\r\nsbit mybit=P1^5;\r\n\r\n&nbsp;\r\n\r\nvoid main(void){\r\n\r\n&nbsp;\r\n\r\nwhile (1) {\r\n\r\n&nbsp;\r\n\r\nmybit=~mybit;\r\n\r\n&nbsp;\r\n\r\nT0M1Delay();\r\n\r\n&nbsp;\r\n\r\n}}\r\n\r\n&nbsp;\r\n\r\nvoid T0M1Delay(void){\r\n\r\n&nbsp;\r\n\r\nTMOD=0x01;\r\n\r\n&nbsp;\r\n\r\nTL0=0xFD;\r\n\r\n&nbsp;\r\n\r\nTH0=0x4B;\r\n\r\n&nbsp;\r\n\r\nTR0=1;\r\n\r\n&nbsp;\r\n\r\nwhile (TF0==0);\r\n\r\n&nbsp;\r\n\r\nTR0=0;\r\n\r\n&nbsp;\r\n\r\nTF0=0;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nFFFFH \u2013 4BFDH = B402H\r\n\r\n&nbsp;\r\n\r\n= 46082 + 1 = 46083\r\n\r\n&nbsp;\r\n\r\nTimer delay= 46083 \u00d7 1.085 \u03bcs = 50 ms\r\n\r\n&nbsp;\r\n\r\n<strong>Example 3<\/strong>\r\n\r\n&nbsp;\r\n\r\nWrite an 8051 C program to toggle all bits of P2 continuously every 500 ms.Use Timer 1, mode 1 to create the delay.\r\n\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/tested for DS89C420, XTAL = 11.0592 MHz\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid T1M1Delay(void);\r\n\r\n&nbsp;\r\n\r\nvoid main(void){\r\n\r\n&nbsp;\r\n\r\nunsigned char x;\r\n\r\n&nbsp;\r\n\r\nP2=0x55;\r\n\r\n&nbsp;\r\n\r\nwhile (1) {\r\n\r\n&nbsp;\r\n\r\nP2=~P2;\r\n\r\n&nbsp;\r\n\r\nfor (x=0;x&lt;20;x++)\r\n\r\n&nbsp;\r\n\r\nT1M1Delay();\r\n\r\n&nbsp;\r\n\r\n} }\r\n\r\n&nbsp;\r\n\r\nvoid T1M1Delay(void){\r\n\r\n&nbsp;\r\n\r\nTMOD=0x10;\r\n\r\n&nbsp;\r\n\r\nTL1=0xFE;\r\n\r\n&nbsp;\r\n\r\nTH1=0xA5;\r\n\r\n&nbsp;\r\n\r\nTR1=1;\r\n\r\n&nbsp;\r\n\r\nwhile (TF1==0);\r\n\r\n&nbsp;\r\n\r\nTR1=0;\r\n\r\n&nbsp;\r\n\r\nTF1=0;}\r\n\r\n&nbsp;\r\n\r\nDelay: A5FEH = 42494 in decimal\r\n\r\n&nbsp;\r\n\r\n65536 \u2013 42494 = 23042\r\n\r\n&nbsp;\r\n\r\n23042 \u00d7 1.085 \u03bcs = 25 ms and 20 \u00d7 25 ms = 500 ms.\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Example 4 and 5 provides details of 8051 C programming of timers 0 and 1 in mode 2 (8-bit auto-reload) mode. Example 6 gives details of 8051 C program to monitor SW and create the given frequencies on pin P1.7.<\/p>\r\n&nbsp;\r\n\r\n<strong>Example 4<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Write an 8051 C program to toggle only pin P1.5 continuously every 250 ms. Use Timer 0, mode 2 (8-bit auto-reload) to create the delay.<\/p>\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid T0M2Delay(void);\r\n\r\n&nbsp;\r\n\r\nsbit mybit=P1^5;\r\n\r\n&nbsp;\r\n\r\nvoid main(void){\r\n\r\n&nbsp;\r\n\r\nunsigned char x,y;\r\n\r\n&nbsp;\r\n\r\nwhile (1) {\r\n\r\n&nbsp;\r\n\r\nmybit=~mybit;\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\nfor (x=0;x&lt;250;x++)\r\n\r\n&nbsp;\r\n\r\nfor (y=0;y&lt;36;y++) \/\/we put 36, not 40\r\n\r\n&nbsp;\r\n\r\nT0M2Delay();\r\n\r\n&nbsp;\r\n\r\n}\u00a0 }\r\n\r\n&nbsp;\r\n\r\nvoid T0M2Delay(void){\r\n\r\n&nbsp;\r\n\r\nTMOD=0x02;\r\n\r\n&nbsp;\r\n\r\nTH0=-23;\r\n\r\n&nbsp;\r\n\r\nTR0=1;\r\n\r\n&nbsp;\r\n\r\nwhile (TF0==0);\r\n\r\n&nbsp;\r\n\r\nTR0=0;\r\n\r\n&nbsp;\r\n\r\nTF0=0;}\r\n\r\n&nbsp;\r\n\r\nTime Delay:\u00a0 256 \u2013 23 = 233\r\n\r\n&nbsp;\r\n\r\n23 \u00d7 1.085 \u03bcs = 25 \u03bcs and\r\n\r\n&nbsp;\r\n\r\n25 \u03bcs \u00d7 250 \u00d7 40 = 250 ms\r\n\r\n&nbsp;\r\n\r\nDue to overhead of the for loop in C, we put 36 instead of 40.\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Example 5<\/strong>\r\n\r\n&nbsp;\r\n\r\nWrite an 8051 C program to create a frequency of 2500 Hz on pin P2.7. Use Timer 1, mode 2 to create delay.\r\n\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid T1M2Delay(void);\r\n\r\n&nbsp;\r\n\r\nsbit mybit=P2^7;\r\n\r\n&nbsp;\r\n\r\nvoid main(void){\r\n\r\n&nbsp;\r\n\r\nunsigned char x;\r\n\r\n&nbsp;\r\n\r\nwhile (1) {\r\n\r\n&nbsp;\r\n\r\nmybit=~mybit;\r\n\r\n&nbsp;\r\n\r\nT1M2Delay();\r\n\r\n&nbsp;\r\n\r\n}}\r\n\r\n&nbsp;\r\n\r\nvoid T1M2Delay(void){\r\n\r\n&nbsp;\r\n\r\nTMOD=0x20;\r\n\r\n&nbsp;\r\n\r\nTH1=-184;\r\n\r\n&nbsp;\r\n\r\nTR1=1;\r\n\r\n&nbsp;\r\n\r\nwhile (TF1==0);\r\n\r\n&nbsp;\r\n\r\nTR1=0;\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\nTF1=0;}\r\n\r\n&nbsp;\r\n\r\nTime Delay:1\/2500 Hz = 400 \u03bcs\r\n\r\n&nbsp;\r\n\r\n400 \u03bcs \/2 = 200 \u03bcs\r\n\r\n&nbsp;\r\n\r\n200 \u03bcs \/ 1.085 \u03bcs = 184\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<strong>Example 6<\/strong>\r\n\r\n&nbsp;\r\n\r\nA switch is connected to pin P1.2. Write an 8051 C program to monitor SW and create the following frequencies on pin P1.7:\r\n\r\n&nbsp;\r\n\r\nSW=0: 500Hz\r\n\r\n&nbsp;\r\n\r\nSW=1: 750Hz, use Timer 0, mode 1 for both of them.\r\n\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nsbit mybit=P1^5;\r\n\r\n&nbsp;\r\n\r\nsbit SW=P1^7;\r\n\r\n&nbsp;\r\n\r\nvoid T0M1Delay(unsigned char);\r\n\r\n&nbsp;\r\n\r\nvoid main(void){\r\n\r\n&nbsp;\r\n\r\nSW=1;\r\n\r\n&nbsp;\r\n\r\nwhile (1) {\r\n\r\n&nbsp;\r\n\r\nmybit=~mybit;\r\n\r\n&nbsp;\r\n\r\nif (SW==0)\r\n\r\n&nbsp;\r\n\r\nT0M1Delay(0);\r\n\r\n&nbsp;\r\n\r\nelse\r\n\r\n&nbsp;\r\n\r\nT0M1Delay(1);\r\n\r\n&nbsp;\r\n\r\n}}\r\n\r\n&nbsp;\r\n\r\nvoid T0M1Delay(unsigned char c){\r\n\r\n&nbsp;\r\n\r\nTMOD=0x01;\r\n\r\n&nbsp;\r\n\r\nif (c==0) {\r\n\r\n&nbsp;\r\n\r\nTL0=0x67;\r\n\r\n&nbsp;\r\n\r\nTH0=0xFC;}\r\n\r\n&nbsp;\r\n\r\nelse {\r\n\r\n&nbsp;\r\n\r\nTL0=0x9A;\r\n\r\n&nbsp;\r\n\r\nTH0=0xFD;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nTR0=1;\r\n\r\n&nbsp;\r\n\r\nwhile (TF0==0);\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\nTR0=0;\r\n\r\n&nbsp;\r\n\r\nTF0=0;}\r\n\r\n&nbsp;\r\n\r\nFrequency:FC67H = 64615\r\n\r\n&nbsp;\r\n\r\n65536 \u2013 64615 = 921\r\n\r\n&nbsp;\r\n\r\n921 \u00d7 1.085 \u03bcs = 999.285 \u03bcs\r\n\r\n&nbsp;\r\n\r\n1 \/ (999.285 \u03bcs \u00d7 2) = 500 Hz.\r\n\r\n&nbsp;\r\n\r\n<strong>2.2 C programming of timers 0 and 1 as counters<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Timers can also be used as counters counting events happening outside the 8051.When it is used as a counter, it is a pulse outside of the 8051 that increments TH, TL registers. TMOD and TH, TL registers are the sameas for the timer discussed previously. Programming the timer in the lastsection also applies to programming it as a counter, except the source of the frequency. The C\/T bit in the TMOD registers decides the source of the clock for the timer.When C\/T = 1, the timer is used as acounter and gets its pulses from outsidethe 8051, the counter counts up as pulses are fed frompins 14 and 15, these pins are called T0 (timer0 input) and T1 (timer 1 input). The following figure shows the port 3 used for Timers 0 and 1 function.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-226 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-125.png\" alt=\"\" width=\"687\" height=\"208\" \/>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"size-full wp-image-227 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-126.png\" alt=\"\" width=\"561\" height=\"392\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">TCON register is a bit addressable register. The following is the function of TCON register for counters. The equivalent instruction for the TCON register is shown below.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-228 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-127.png\" alt=\"\" width=\"566\" height=\"221\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Example 7 shown below shows the 8051 C programming for counter 1 in mode 2 in an 8-bit auto reload mode to count up and display TL1 state in pin P1. The count starts at 0H.<\/p>\r\n&nbsp;\r\n\r\n<strong>Example 7<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Assume that a 1-Hz external clock is being fed into pin T1 (P3.5). Write a C program for counter 1 in mode 2 (8-bit auto reload) to count up and display the state of the TL1 count on P1. Start the count at 0H.<\/p>\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nsbit T1=P3^5;\r\n\r\n&nbsp;\r\n\r\nvoid main(void){\r\n\r\n<\/div>\r\n<div>\r\n\r\nT1=1;\r\n\r\n&nbsp;\r\n\r\nTMOD=0x60;\r\n\r\n&nbsp;\r\n\r\nTH1=0;\r\n\r\n&nbsp;\r\n\r\nwhile (1) {\r\n\r\n&nbsp;\r\n\r\ndo {\r\n\r\n&nbsp;\r\n\r\nTR1=1;\r\n\r\n&nbsp;\r\n\r\nP1=TL1;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nwhile (TF1==0);\r\n\r\n&nbsp;\r\n\r\nTR1=0;\r\n\r\n&nbsp;\r\n\r\nTF1=0;\r\n\r\n&nbsp;\r\n\r\n}}\r\n\r\n&nbsp;\r\n\r\nP1 is connected to 8 LEDs. T1 (P3.5) is connected to a 1-Hz external clock.\r\n\r\n&nbsp;\r\n\r\n<strong>Example 8<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Assume that a 1-Hz external clock is being fed into pin T0 (P3.4). Write a C program for counter 0 in mode 1 (16-bit) to count the pulses and display the state of the TH0 and TL0 registers on P2 and P1, respectively.<\/p>\r\n&nbsp;\r\n\r\n<strong>Solution:<\/strong>\r\n\r\n&nbsp;\r\n\r\n#include &lt;reg51.h&gt;\r\n\r\n&nbsp;\r\n\r\nvoid main(void){\r\n\r\n&nbsp;\r\n\r\nT0=1;\r\n\r\n&nbsp;\r\n\r\nTMOD=0x05;\r\n\r\n&nbsp;\r\n\r\nTL0=0\r\n\r\n&nbsp;\r\n\r\nTH0=0;\r\n\r\n&nbsp;\r\n\r\nwhile (1) {\r\n\r\n&nbsp;\r\n\r\ndo {\r\n\r\n&nbsp;\r\n\r\nTR0=1;\r\n\r\n&nbsp;\r\n\r\nP1=TL0;\r\n\r\n&nbsp;\r\n\r\nP2=TH0;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nwhile (TF0==0);\r\n\r\n&nbsp;\r\n\r\nTR0=0;\r\n\r\n&nbsp;\r\n\r\nTF0=0;\r\n\r\n&nbsp;\r\n\r\n} }\r\n\r\n&nbsp;\r\n\r\nThe example 8 explained above shows the 8051 C program to count up and display it\u00a0<span style=\"text-align: initial;font-size: 1em\">on TH0 and TL0 registers in pin P2 and P1 respectively. The External clock connected to T0 on P3.4 .<\/span>\r\n\r\n<\/div>\r\n<ol start=\"3\">\r\n \t<li><strong> Summary<\/strong><\/li>\r\n<\/ol>\r\n<p style=\"text-align: justify\">In this lecture how to write Embedded C Timer Programming was discussed. Counter in 8051 and basic Timer operation is also discussed. Embedded C program for Timer and counter are discussed with examples.<\/p>\r\n&nbsp;\r\n<ol start=\"4\">\r\n \t<li><strong>References<\/strong><\/li>\r\n<\/ol>\r\n<ol>\r\n \t<li>\u201cThe 8051 Microcontroller and Embedded Systems Using Assembly and CSecond Edition\u201d, Muhammad Ali Mazidi, Janice Gillispie Mazidi and Rolin D. McKinlay.<\/li>\r\n<\/ol>","rendered":"<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In this lecture timer registers in C will be discussed.Details of counters in 8051 and delay length calculation will be discussed. Timer and counter programming in Embedded C is explained with example.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1. Timers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">8051 has two timers. They are Timer 0 and Timer 1. It is independently configured to operate in a variety of modes as a timer or as an event counter.When operating as a timer, the timer\/counter runs for a programmed length of time, then issues an interrupt request. But when operating as a counter, the timer\/counter counts negative transitions on an external pin. After a preset number of counts, the counter issues an interrupt request.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.1 Timers Registers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Some of the basic timer registers are TMOD, TCON, THx and TLx.TMOD is used to select the mode of operation.TCON is used to control the timer to run or stop.The details of TCON register is shown below.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-224 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-124.png\" alt=\"\" width=\"711\" height=\"395\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-124.png 711w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-124-300x167.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-124-65x36.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-124-225x125.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-124-350x194.png 350w\" sizes=\"auto, (max-width: 711px) 100vw, 711px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em;text-align: initial\">THx and TLx registers are used to hold the count to generate desired time delays.TMOD is an 8-bit register in which lower 4 bits are for Timer 0 and upper 4 bits are for Timer 1. Lower 2 bits are used to set the timer mode while upper 2 bits is used to specify the operation.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>1.2 Timers Operation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Timer 0 and Timer 1 are 16 bit wide. It consists of registers THx and TLx (x= 0, 1) connected in cascade to form a 16-bit timer. It can be accessed as low byte and high byte. Lower byte register is referred to as TLx, while higher byte register is referred to as THx. The timer\/ counter is enabled only while the INTx is high and TRx control pin is set.Setting the run control bit (TRx) in TCON register turns the timer on by allowing the selected input to increment TLx. When TLx overflows it increments THx. But when THx overflows it sets the timer overflow flag (TFx) in TCON register. Setting the TRx does not clear the THx and TLx timer registers.BothTimer 0 and Timer 1 have four operating modes. These modes are selected by bit-pairs (M1, M0) in TMOD register. Modes 0, 1,and 2 are the same for both timers\/counters. The other basics of timer and counter is explained in 8051 Timer programming in Assembly Language in the previous chapter.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.3 Steps to generate Time Delay<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Following are the steps to generate the time delay.<\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0\u00a0\u00a0 Load the TMOD value register with mode and timer0 or 1.<\/p>\n<p>&nbsp;<\/p>\n<p>2.\u00a0\u00a0\u00a0 Load registers TL and TH with count corresponding to delay.<\/p>\n<p>&nbsp;<\/p>\n<p>3.\u00a0\u00a0\u00a0 Start the timer.<\/p>\n<p>&nbsp;<\/p>\n<p>4.\u00a0\u00a0\u00a0 Continuously check the timer flag (TF) with the \u201cJNB TFx,target\u201d instruction to see if it is raised, if it is raised(TF=1) then get out of the loop.<\/p>\n<p>&nbsp;<\/p>\n<p>5.\u00a0\u00a0\u00a0 Stop the timer.<\/p>\n<p>&nbsp;<\/p>\n<p>6.\u00a0\u00a0\u00a0 Clear the TF flag for the next round.<\/p>\n<p>&nbsp;<\/p>\n<p>7.\u00a0\u00a0\u00a0 Go back to Step 2 to load TH and TL again.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2.\u00a0 <\/strong><strong>Embedded C program for 8051 Timer 0 and Timer 1<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The general purpose registers of 8051, such as R0-R7, A and B, are under the control of C compiler.They are not accessed directly by C statements.In case of SFRs, the entire RAM space of 80 \u2013 FFH is accessible directly using 8051 C statements.We can access the time registers TH, TL, and TMOD directly using the reg51.h header file.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2.1 Access timer registers in C<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Example 1 explained below gives the details about how to access 8051 timer registers like TH, TL, and TMOD in C and it also shows how to access TR and TF bits.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 1<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Write an 8051 C program to toggle all the bits of port P1 continuously with some delay in between. Use Timer 0, 16-bit mode to generate the delay.<\/p>\n<\/div>\n<div>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void T0Delay(void);<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void){<\/p>\n<p>&nbsp;<\/p>\n<p>while (1) {<\/p>\n<p>&nbsp;<\/p>\n<p>P1=0x55;<\/p>\n<p>&nbsp;<\/p>\n<p>T0Delay();<\/p>\n<p>&nbsp;<\/p>\n<p>P1=0xAA;<\/p>\n<p>&nbsp;<\/p>\n<p>T0Delay();<\/p>\n<p>&nbsp;<\/p>\n<p>}\u00a0 }<\/p>\n<p>&nbsp;<\/p>\n<p>void T0Delay(){<\/p>\n<p>&nbsp;<\/p>\n<p>TMOD=0x01;<\/p>\n<p>&nbsp;<\/p>\n<p>TL0=0x00;<\/p>\n<p>&nbsp;<\/p>\n<p>TH0=0x35;<\/p>\n<p>&nbsp;<\/p>\n<p>TR0=1;<\/p>\n<p>&nbsp;<\/p>\n<p>while (TF0==0);<\/p>\n<p>&nbsp;<\/p>\n<p>TR0=0;<\/p>\n<p>&nbsp;<\/p>\n<p>TF0=0;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>Calculating delay:<\/p>\n<p>&nbsp;<\/p>\n<p>FFFFH \u2013 3500H = CAFFH<\/p>\n<p>&nbsp;<\/p>\n<p>= 51967 + 1 = 51968<\/p>\n<p>&nbsp;<\/p>\n<p>=51968 \u00d7 1.085 \u03bcs = 56.384 ms is the approximate delay.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2.1 Calculating delay using timer in C<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As mentioned in the previous chapter the delay length is depend on three factors. They are Crystal frequency, the number of clocks per machine cycle and the C compiler.To speed up the 8051, many recent versions of the 8051 have reduced the number of clocks per machine cycle from 12 to four, or even one. The frequency for the timer is always 1\/12th the frequency of the crystal attached to the 8051, regardless of the 8051 version. We can also use crystal frequency as the clock source. The following are the steps to find TH, TL register values. We can assume XTAL = 11.0592 MHz for 8051 systems.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Steps for finding the TH, TL register values<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 Divide the desired time delay by 1.085 \u03bcs.<\/p>\n<p>&nbsp;<\/p>\n<p>2.\u00a0 Perform 65536 \u2013 n, where n is the decimal value we got in Step 1.<\/p>\n<p>&nbsp;<\/p>\n<p>3.\u00a0\u00a0 Convert the result of Step2 to hex, where yyxx is the initial hex value to be loaded<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>into the timer\u2019s register.<\/p>\n<p>&nbsp;<\/p>\n<p>4. Set TL = xx and TH = yy.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Times 0\/1 Delay Using Mode 1 (16-bit Non-Auto-reload)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Example 2 and 3 shows 8051 C programming of the timer 0 and timer 1 in mode 1 in a 16-bit non-auto reload mode.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 2<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Write an 8051 C program to toggle only bit P1.5 continuously every 50 ms. Use Timer 0, mode 1 (16-bit) to create the delay. Test the program on the (a) AT89C51 and (b) DS89C420.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void T0M1Delay(void);<\/p>\n<p>&nbsp;<\/p>\n<p>sbit mybit=P1^5;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void){<\/p>\n<p>&nbsp;<\/p>\n<p>while (1) {<\/p>\n<p>&nbsp;<\/p>\n<p>mybit=~mybit;<\/p>\n<p>&nbsp;<\/p>\n<p>T0M1Delay();<\/p>\n<p>&nbsp;<\/p>\n<p>}}<\/p>\n<p>&nbsp;<\/p>\n<p>void T0M1Delay(void){<\/p>\n<p>&nbsp;<\/p>\n<p>TMOD=0x01;<\/p>\n<p>&nbsp;<\/p>\n<p>TL0=0xFD;<\/p>\n<p>&nbsp;<\/p>\n<p>TH0=0x4B;<\/p>\n<p>&nbsp;<\/p>\n<p>TR0=1;<\/p>\n<p>&nbsp;<\/p>\n<p>while (TF0==0);<\/p>\n<p>&nbsp;<\/p>\n<p>TR0=0;<\/p>\n<p>&nbsp;<\/p>\n<p>TF0=0;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>FFFFH \u2013 4BFDH = B402H<\/p>\n<p>&nbsp;<\/p>\n<p>= 46082 + 1 = 46083<\/p>\n<p>&nbsp;<\/p>\n<p>Timer delay= 46083 \u00d7 1.085 \u03bcs = 50 ms<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 3<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Write an 8051 C program to toggle all bits of P2 continuously every 500 ms.Use Timer 1, mode 1 to create the delay.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/tested for DS89C420, XTAL = 11.0592 MHz<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void T1M1Delay(void);<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void){<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char x;<\/p>\n<p>&nbsp;<\/p>\n<p>P2=0x55;<\/p>\n<p>&nbsp;<\/p>\n<p>while (1) {<\/p>\n<p>&nbsp;<\/p>\n<p>P2=~P2;<\/p>\n<p>&nbsp;<\/p>\n<p>for (x=0;x&lt;20;x++)<\/p>\n<p>&nbsp;<\/p>\n<p>T1M1Delay();<\/p>\n<p>&nbsp;<\/p>\n<p>} }<\/p>\n<p>&nbsp;<\/p>\n<p>void T1M1Delay(void){<\/p>\n<p>&nbsp;<\/p>\n<p>TMOD=0x10;<\/p>\n<p>&nbsp;<\/p>\n<p>TL1=0xFE;<\/p>\n<p>&nbsp;<\/p>\n<p>TH1=0xA5;<\/p>\n<p>&nbsp;<\/p>\n<p>TR1=1;<\/p>\n<p>&nbsp;<\/p>\n<p>while (TF1==0);<\/p>\n<p>&nbsp;<\/p>\n<p>TR1=0;<\/p>\n<p>&nbsp;<\/p>\n<p>TF1=0;}<\/p>\n<p>&nbsp;<\/p>\n<p>Delay: A5FEH = 42494 in decimal<\/p>\n<p>&nbsp;<\/p>\n<p>65536 \u2013 42494 = 23042<\/p>\n<p>&nbsp;<\/p>\n<p>23042 \u00d7 1.085 \u03bcs = 25 ms and 20 \u00d7 25 ms = 500 ms.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Example 4 and 5 provides details of 8051 C programming of timers 0 and 1 in mode 2 (8-bit auto-reload) mode. Example 6 gives details of 8051 C program to monitor SW and create the given frequencies on pin P1.7.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 4<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Write an 8051 C program to toggle only pin P1.5 continuously every 250 ms. Use Timer 0, mode 2 (8-bit auto-reload) to create the delay.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void T0M2Delay(void);<\/p>\n<p>&nbsp;<\/p>\n<p>sbit mybit=P1^5;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void){<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char x,y;<\/p>\n<p>&nbsp;<\/p>\n<p>while (1) {<\/p>\n<p>&nbsp;<\/p>\n<p>mybit=~mybit;<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>for (x=0;x&lt;250;x++)<\/p>\n<p>&nbsp;<\/p>\n<p>for (y=0;y&lt;36;y++) \/\/we put 36, not 40<\/p>\n<p>&nbsp;<\/p>\n<p>T0M2Delay();<\/p>\n<p>&nbsp;<\/p>\n<p>}\u00a0 }<\/p>\n<p>&nbsp;<\/p>\n<p>void T0M2Delay(void){<\/p>\n<p>&nbsp;<\/p>\n<p>TMOD=0x02;<\/p>\n<p>&nbsp;<\/p>\n<p>TH0=-23;<\/p>\n<p>&nbsp;<\/p>\n<p>TR0=1;<\/p>\n<p>&nbsp;<\/p>\n<p>while (TF0==0);<\/p>\n<p>&nbsp;<\/p>\n<p>TR0=0;<\/p>\n<p>&nbsp;<\/p>\n<p>TF0=0;}<\/p>\n<p>&nbsp;<\/p>\n<p>Time Delay:\u00a0 256 \u2013 23 = 233<\/p>\n<p>&nbsp;<\/p>\n<p>23 \u00d7 1.085 \u03bcs = 25 \u03bcs and<\/p>\n<p>&nbsp;<\/p>\n<p>25 \u03bcs \u00d7 250 \u00d7 40 = 250 ms<\/p>\n<p>&nbsp;<\/p>\n<p>Due to overhead of the for loop in C, we put 36 instead of 40.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 5<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Write an 8051 C program to create a frequency of 2500 Hz on pin P2.7. Use Timer 1, mode 2 to create delay.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void T1M2Delay(void);<\/p>\n<p>&nbsp;<\/p>\n<p>sbit mybit=P2^7;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void){<\/p>\n<p>&nbsp;<\/p>\n<p>unsigned char x;<\/p>\n<p>&nbsp;<\/p>\n<p>while (1) {<\/p>\n<p>&nbsp;<\/p>\n<p>mybit=~mybit;<\/p>\n<p>&nbsp;<\/p>\n<p>T1M2Delay();<\/p>\n<p>&nbsp;<\/p>\n<p>}}<\/p>\n<p>&nbsp;<\/p>\n<p>void T1M2Delay(void){<\/p>\n<p>&nbsp;<\/p>\n<p>TMOD=0x20;<\/p>\n<p>&nbsp;<\/p>\n<p>TH1=-184;<\/p>\n<p>&nbsp;<\/p>\n<p>TR1=1;<\/p>\n<p>&nbsp;<\/p>\n<p>while (TF1==0);<\/p>\n<p>&nbsp;<\/p>\n<p>TR1=0;<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>TF1=0;}<\/p>\n<p>&nbsp;<\/p>\n<p>Time Delay:1\/2500 Hz = 400 \u03bcs<\/p>\n<p>&nbsp;<\/p>\n<p>400 \u03bcs \/2 = 200 \u03bcs<\/p>\n<p>&nbsp;<\/p>\n<p>200 \u03bcs \/ 1.085 \u03bcs = 184<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 6<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>A switch is connected to pin P1.2. Write an 8051 C program to monitor SW and create the following frequencies on pin P1.7:<\/p>\n<p>&nbsp;<\/p>\n<p>SW=0: 500Hz<\/p>\n<p>&nbsp;<\/p>\n<p>SW=1: 750Hz, use Timer 0, mode 1 for both of them.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>sbit mybit=P1^5;<\/p>\n<p>&nbsp;<\/p>\n<p>sbit SW=P1^7;<\/p>\n<p>&nbsp;<\/p>\n<p>void T0M1Delay(unsigned char);<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void){<\/p>\n<p>&nbsp;<\/p>\n<p>SW=1;<\/p>\n<p>&nbsp;<\/p>\n<p>while (1) {<\/p>\n<p>&nbsp;<\/p>\n<p>mybit=~mybit;<\/p>\n<p>&nbsp;<\/p>\n<p>if (SW==0)<\/p>\n<p>&nbsp;<\/p>\n<p>T0M1Delay(0);<\/p>\n<p>&nbsp;<\/p>\n<p>else<\/p>\n<p>&nbsp;<\/p>\n<p>T0M1Delay(1);<\/p>\n<p>&nbsp;<\/p>\n<p>}}<\/p>\n<p>&nbsp;<\/p>\n<p>void T0M1Delay(unsigned char c){<\/p>\n<p>&nbsp;<\/p>\n<p>TMOD=0x01;<\/p>\n<p>&nbsp;<\/p>\n<p>if (c==0) {<\/p>\n<p>&nbsp;<\/p>\n<p>TL0=0x67;<\/p>\n<p>&nbsp;<\/p>\n<p>TH0=0xFC;}<\/p>\n<p>&nbsp;<\/p>\n<p>else {<\/p>\n<p>&nbsp;<\/p>\n<p>TL0=0x9A;<\/p>\n<p>&nbsp;<\/p>\n<p>TH0=0xFD;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>TR0=1;<\/p>\n<p>&nbsp;<\/p>\n<p>while (TF0==0);<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>TR0=0;<\/p>\n<p>&nbsp;<\/p>\n<p>TF0=0;}<\/p>\n<p>&nbsp;<\/p>\n<p>Frequency:FC67H = 64615<\/p>\n<p>&nbsp;<\/p>\n<p>65536 \u2013 64615 = 921<\/p>\n<p>&nbsp;<\/p>\n<p>921 \u00d7 1.085 \u03bcs = 999.285 \u03bcs<\/p>\n<p>&nbsp;<\/p>\n<p>1 \/ (999.285 \u03bcs \u00d7 2) = 500 Hz.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2.2 C programming of timers 0 and 1 as counters<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Timers can also be used as counters counting events happening outside the 8051.When it is used as a counter, it is a pulse outside of the 8051 that increments TH, TL registers. TMOD and TH, TL registers are the sameas for the timer discussed previously. Programming the timer in the lastsection also applies to programming it as a counter, except the source of the frequency. The C\/T bit in the TMOD registers decides the source of the clock for the timer.When C\/T = 1, the timer is used as acounter and gets its pulses from outsidethe 8051, the counter counts up as pulses are fed frompins 14 and 15, these pins are called T0 (timer0 input) and T1 (timer 1 input). The following figure shows the port 3 used for Timers 0 and 1 function.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-226 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-125.png\" alt=\"\" width=\"687\" height=\"208\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-125.png 687w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-125-300x91.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-125-65x20.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-125-225x68.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-125-350x106.png 350w\" sizes=\"auto, (max-width: 687px) 100vw, 687px\" \/><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-227 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-126.png\" alt=\"\" width=\"561\" height=\"392\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-126.png 561w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-126-300x210.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-126-65x45.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-126-225x157.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-126-350x245.png 350w\" sizes=\"auto, (max-width: 561px) 100vw, 561px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">TCON register is a bit addressable register. The following is the function of TCON register for counters. The equivalent instruction for the TCON register is shown below.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-228 aligncenter\" src=\"http:\/\/csp13.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/62\/2018\/07\/2-127.png\" alt=\"\" width=\"566\" height=\"221\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-127.png 566w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-127-300x117.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-127-65x25.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-127-225x88.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-content\/uploads\/sites\/62\/2018\/07\/2-127-350x137.png 350w\" sizes=\"auto, (max-width: 566px) 100vw, 566px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Example 7 shown below shows the 8051 C programming for counter 1 in mode 2 in an 8-bit auto reload mode to count up and display TL1 state in pin P1. The count starts at 0H.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 7<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Assume that a 1-Hz external clock is being fed into pin T1 (P3.5). Write a C program for counter 1 in mode 2 (8-bit auto reload) to count up and display the state of the TL1 count on P1. Start the count at 0H.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>sbit T1=P3^5;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void){<\/p>\n<\/div>\n<div>\n<p>T1=1;<\/p>\n<p>&nbsp;<\/p>\n<p>TMOD=0x60;<\/p>\n<p>&nbsp;<\/p>\n<p>TH1=0;<\/p>\n<p>&nbsp;<\/p>\n<p>while (1) {<\/p>\n<p>&nbsp;<\/p>\n<p>do {<\/p>\n<p>&nbsp;<\/p>\n<p>TR1=1;<\/p>\n<p>&nbsp;<\/p>\n<p>P1=TL1;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>while (TF1==0);<\/p>\n<p>&nbsp;<\/p>\n<p>TR1=0;<\/p>\n<p>&nbsp;<\/p>\n<p>TF1=0;<\/p>\n<p>&nbsp;<\/p>\n<p>}}<\/p>\n<p>&nbsp;<\/p>\n<p>P1 is connected to 8 LEDs. T1 (P3.5) is connected to a 1-Hz external clock.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 8<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Assume that a 1-Hz external clock is being fed into pin T0 (P3.4). Write a C program for counter 0 in mode 1 (16-bit) to count the pulses and display the state of the TH0 and TL0 registers on P2 and P1, respectively.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>#include &lt;reg51.h&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>void main(void){<\/p>\n<p>&nbsp;<\/p>\n<p>T0=1;<\/p>\n<p>&nbsp;<\/p>\n<p>TMOD=0x05;<\/p>\n<p>&nbsp;<\/p>\n<p>TL0=0<\/p>\n<p>&nbsp;<\/p>\n<p>TH0=0;<\/p>\n<p>&nbsp;<\/p>\n<p>while (1) {<\/p>\n<p>&nbsp;<\/p>\n<p>do {<\/p>\n<p>&nbsp;<\/p>\n<p>TR0=1;<\/p>\n<p>&nbsp;<\/p>\n<p>P1=TL0;<\/p>\n<p>&nbsp;<\/p>\n<p>P2=TH0;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>while (TF0==0);<\/p>\n<p>&nbsp;<\/p>\n<p>TR0=0;<\/p>\n<p>&nbsp;<\/p>\n<p>TF0=0;<\/p>\n<p>&nbsp;<\/p>\n<p>} }<\/p>\n<p>&nbsp;<\/p>\n<p>The example 8 explained above shows the 8051 C program to count up and display it\u00a0<span style=\"text-align: initial;font-size: 1em\">on TH0 and TL0 registers in pin P2 and P1 respectively. The External clock connected to T0 on P3.4 .<\/span><\/p>\n<\/div>\n<ol start=\"3\">\n<li><strong> Summary<\/strong><\/li>\n<\/ol>\n<p style=\"text-align: justify\">In this lecture how to write Embedded C Timer Programming was discussed. Counter in 8051 and basic Timer operation is also discussed. Embedded C program for Timer and counter are discussed with examples.<\/p>\n<p>&nbsp;<\/p>\n<ol start=\"4\">\n<li><strong>References<\/strong><\/li>\n<\/ol>\n<ol>\n<li>\u201cThe 8051 Microcontroller and Embedded Systems Using Assembly and CSecond Edition\u201d, Muhammad Ali Mazidi, Janice Gillispie Mazidi and Rolin D. McKinlay.<\/li>\n<\/ol>\n","protected":false},"author":2,"menu_order":18,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"class_list":["post-221","chapter","type-chapter","status-publish","hentry"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/users\/2"}],"version-history":[{"count":4,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/221\/revisions"}],"predecessor-version":[{"id":229,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/221\/revisions\/229"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapters\/221\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/media?parent=221"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/pressbooks\/v2\/chapter-type?post=221"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/contributor?post=221"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp13\/wp-json\/wp\/v2\/license?post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}