80bI fimer/counfer M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -2 2 Timers /Counters Programming The 80bI hos Z fimers/counfers: fimer/counfer 0 fimer/counfer I They con be used os I. The timer is used os o fime deIoy generofor. The cIock source is fhe internuI crysfoI frequency of fhe 80bI. Z. An evenf counter. ternuI input from inpuf pin fo counf fhe number of evenfs on regisfers. These cIock puIses coId represenf fhe number of peopIe possing fhrough on enfronce, or fhe number of wheeI rofofions, or ony ofher evenf fhof con be converfed fo puIses. M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -3 3 M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -4 4 Timer $ef fhe inifioI voIue of regisfers $forf fhe fimer ond fhen fhe 80bI counfs up. Inpuf from infernoI sysfem cIock (mochine cycIe) When fhe regisfers equoI fo 0 ond fhe 80bI sefs o bif fo denofe fime ouf to LCD P1 1 TL0 TH0 P2 Set Timer 0 M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- - Counter ounf fhe number of evenfs $how fhe number of evenfs on regisfers ExfernoI inpuf from T0 inpuf pin (P3.4) for ounfer 0 ExfernoI inpuf from TI inpuf pin (P3.b) for ounfer I ternuI input from Tx inpuf pin. We use Tx fo denofe T0 or TI. T0 to LCD P3.4 P1 1 a switch TL0 TH0 M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -6 6 #egisters Used in Timer/Counter TH0, TL0, THI, TLI TMOD (Timer mode regisfer) TOM (Timer confroI regisfer) M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -7 7 TMOD #egister Timer mode regisfer: % MOV TMOD,#21H An 8-bif regisfer $ef fhe usoge mode for fwo fimers $ef Iower 4 bifs for Timer 0 ($ef fo 0000 if nof used) $ef upper 4 bifs for Timer I ($ef fo 0000 if nof used) Mof bif-oddressobIe TE C/T M1 M0 TE C/T M1 M0 Timer 1 Timer 0 (MSB) (LSB) M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- - ate Every fimer hos o meon of sforfing ond sfopping. 0ATE~0 nternuI confroI The sforf ond sfop of fhe fimer ore confroIIed by woy of 8oftwure, $ef/cIeor fhe TP for sforf/sfop fimer. $ET8 TP0 LP TP0 0ATE~I ternuI confroI The hordwore woy of sforfing ond sfopping fhe fimer by 8oftwure ond un eternuI 8ource. Timer/counfer is enobIed onIy whiIe fhe IMT pin is high ond fhe TP confroI pin is sef (TP). TE C/T M1 M0 TE C/T M1 M0 Timer 1 Timer 0 (MSB) (LSB) M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -9 9 igure 9-3. TMOD #egister % Timer or counfer seIecfed cIeored for fimer operofion (inpuf from infernoI sysfem cIock). $ef for counfer operofion (inpuf from Tx inpuf pin). TE C/T M1 M0 TE C/T M1 M0 Timer 1 Timer 0 (MSB) (LSB) MI M0 Mode Operofing Mode 0 0 -it timer mode 8-bif THx + b-bif TLx (x~ 0 or I) 0 I -it timer mode 8-bif THx + 8-bif TLx I 0 Z -it uuto reIoud 8-bif oufo reIood fimer/counfer, THx hoIds o voIue which is fo be reIooded info TLx eoch fime if overfIows. I I $pIif fimer mode M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -1 1 Timer modes M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -11 11 TCON #egister (1/2) Timer control register: TMOD Upper nibble Ior timer/counter, lower nibble Ior interrupts TR (run control bit) T#0 Ior Timer/counter 0; T#1 Ior Timer/counter 1. T# is set by programmer to turn timer/counter on/oII. T#0: oII (stop) T#1: on (start) T1 T#1 T0 T#0 IE1 IT1 IE0 IT0 Timer 1 Timer0 Ior Interrupt (MSB) (LSB) M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -12 12 TCON #egister (2/2) % (fimer fIog, confroI fIog) TF0 for fimer/counfer 0, TFI for fimer/counfer I. TF is Iike o corry. OriginoIIy, TF~0. When TH-TL roII over fo 0000 from FFFFH, fhe TF is sef fo I. TF~0 : nof reoch TF~I: reoch If we enobIe inferrupf, TF~I wiII frigger I$P. T1 T#1 T0 T#0 IE1 IT1 IE0 IT0 Timer 1 Timer0 Ior Interrupt (MSB) (LSB) M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -13 13 Equivalent Instructions for the Timer Control Register For timer $ETB TR $ETB TCON.4 CLR TR CLR TCON.4 $ETB TF $ETB TCON. CLR TF CLR TCON. For timer 1 $ETB TR1 $ETB TCON.6 CLR TR1 CLR TCON.6 $ETB TF1 $ETB TCON.7 CLR TF1 CLR TCON.7 TF1 IT IE IT1 IE1 TR TF TR1 TCON: Timer/Counter Control #egister M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -14 14 Timer Mode I In foIIowing, we oII use fimer 0 os on exompIe. -it fimer (TH0 ond TL0) TH0-TL0 is incremenfed confinuousIy when TP0 is sef fo I. And fhe 80bI sfops fo incremenf TH0-TL0 when TP0 is cIeored. The fimer works wifh fhe infernoI sysfem cIock. In ofher words, fhe fimer counfs up eoch mochine cycIe. When fhe fimer (TH0-TL0) reoches ifs moximum of FFFFH, if roIIs over fo 0000, ond TF0 is roised. Progrommer shouId check TF0 ond sfop fhe fimer 0. M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -1 1 $feps of Mode I (I/3) I. hoose mode I fimer 0 MOV TMOD,#01H Z. $ef fhe originoI voIue fo TH0 ond TL0. MOV TH0,#FFH MOV TL0,#FCH 3. You hod beffer fo cIeor fhe fIog fo monifor: TF0~0. CLR TF0 4. $forf fhe fimer. $TB TR0 M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -16 16 Steps oI Mode 1 (2/3) b. The 80bI sforfs fo counf up by incremenfing fhe TH0-TL0. TH-TL FFFCH,FFFDH,FFFEH,FFFFH,H C D E 0000 T 0 T 0 T 0 T 0 T 1 TH0 TL0 Start timer Stop timer Monitor TF untiI TF=1 TR0=1 TR0=0 TF M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -17 17 Steps oI Mode 1 (3/3) 6. When TH0-TL0 rolls over from FFFFH to 0000, the 8051 set TF0=1. TH0-TL0= FFFH, FFFFH, 0000H (Now TF0=1) 7. Keep monitoring the timer flag (TF) to see if it is raised. AGAIN: JNB TF0, AGAIN 8. Clear TR0 to stop the process. CLR TR0 9. Clear the TF flag for the next round. CLR TF0 M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -1 1 Mode I Progromming TAL oscillator l12 T# TH TL T Timer overIlow Ilag C/T T goes high when 0 M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -19 19 Timer Delay Calculation Ior XTL 11.0592 MHz (a) in hex (FFFF - YYXX + 1) k 3s here YYXX are TH, TL initial values respectively. Notice that values YYXX are in hex. (b) in decimaI Convert YYXX values of the TH, TL register to decimal to get a NNNNN decimal number then (65536 - NNNNN) k1.085 3s M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -2 2 ExompIe 9-4 (I/3) squore wove of b07 dufy on PI.b Timer 0 is used ;each loop is a half clock MOV TMOD,#01 ;Timer 0,mode 1(16-bit) HR: MOV TL0,#0F2H ;Timer value = FFF2H MOV TH0,#0FFH CPL P1.5 ACALL DLAY $JMP HR 50 50 whole clock !1.5 M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -21 21 ExompIe 9-4 (Z/3) ;generate delay using timer 0 DLAY: $TB TR0 ;start the timer 0 AGAIN:JNB TF0,AGAIN CLR TR0 ;stop timer 0 CLR TF0 ;clear timer 0 flag RT 2 3 4 0000 T0 0 T0 0 T0 0 T0 0 T0 1 M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -22 22 ExompIe 9-4 (3/3) $olution: In the above program notice the Iollowing steps. 1. TMOD 1 is loaded. 2. FFF2His loaded into TH0 TL0. 3. P1.5 is toggled Ior the high and low portions oI the pulse. 4. The DEL subroutine using the timer is called. 5. In the DEL subroutine, timer 0 is started by the '$ETB TR instruction. 6. Timer 0 counts up with the passing oI each clock, which is provided by the crystal oscillator. s the timer counts up, it goes through the states oI 3, 4, 5, 6, , , 9, , B, C, D, E, H. One more clock rolls it to 0, raising the timer Ilag (TF 1). t that point, the JNB instruction Ialls through. . Timer 0 is stopped by the instruction 'CLR TR. The DEL subroutine ends, and the process is repeated. Notice that to repeat the process, we must reload the TL and TH registers, and start the timer again (in the main program). M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -23 23 ExompIe 9-9 (I/Z) This progrom generofes o squore wove on pin PI.b Using fimer I Find fhe frequency.(donf incIude fhe overheod of insfrucfion deIoy) XTAL ~ II.0b9Z MH; MOV TMOD,#10H ;timer 1, mode 1 AGAIN:MOV TL1,#34H ;timer value=3476H MOV TH1,#76H $TB TR1 ;start BACK: JNB TF1,BACK CLR TR1 ;stop CPL P1.5 ;next half clock CLR TF1 ;clear timer flag 1 $JMP AGAIN ;reload timer1 M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -24 24 ExompIe 9-9 (Z/Z) $4Iuti4n: FFFFH - 7634H + 1 = 89CCH = 35276 clock count HaIf peri4d = 35276 k1.085 3s = 38.274 ms h4Ie peri4d = 2 k38.274 ms = 76.548 ms Frequency = 1/ 76.548 ms = 13.064 Hz. 4te ode 1 is not auto reload then the program must reload the TH1, TL1 register every timer overflo if e ant to have a continuous ave. M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -2 2 Find Timer VoIues Assume fhof XTAL ~ II.0b9Z MH; . And we know desired deIuy how fo find fhe voIues for fhe TH,TL 7 1. Divide the deIay by 3s and get n 2. Perform -n 3. Convert the result of Step 2 to hex (yyxx ) 4. Set TH = yy and TL = xx. M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -26 26 Example 9-12 (1/2) Assuming XTAL ~ II.0b9Z MH;, wrife o progrom fo generofe o squore wove of b0 H; frequency on pin PZ.3. $oIufion: 1. The period of the square ave = / Hz = 2 ms 2. The high or lo portion of the square ave = ms. ms / 3s = 92 - 92 = 2 in decimal = H in hex. 5. TL1 = 00H and TH1 = DCH. M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -27 27 ExompIe 9-IZ (Z/Z) MOV TMOD,#10H ;timer 1, mode 1 AGAIN: MOV TL1,#00 ;Timer value = DC00H MOV TH1,#0DCH $TB TR1 ;start BACK: JNB TF1,BACK CLR TR1 ;stop CPL P2.3 CLR TF1 ;clear timer flag 1 $JMP AGAIN ;reload timer since ;mode 1 is not ;auto-reload M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -2 2 0enerofe o Lorge Time DeIoy The si;e of fhe fime deIoy depends on fwo focfors: They crysfoI frequency The fimer's Io-bif regisfer, TH & TL The Iorgesf fime deIoy is ochieved by moking TH~TL~0. Whof if fhof is nof enough7 Mexf ExompIe show how fo ochieve Iorge fime deIoy M_NoIIodcIun uIoo.con M_NoIIodcIun uIoo.con Microprocessors Microprocessors 1 1- -29 29 ExompIe 9-I3 Exomine fhe foIIowing progrom ond find fhe fime deIoy in seconds. ExcIude fhe overheod due fo fhe insfrucfions in fhe Ioop. MOV TMOD,#10H MOV R3,#200 AGAIN: MOV TL1,#08 MOV TH1,#01 $TB TR1 BACK: JNB TF1,BACK CLR TR1 CLR TF1 DJNZ R3,AGAIN $oIution: TH - TL ~ 0I08H ~ Zo4 in decimoI obb3o - Zo4 ~ obZ7Z. One of fhe fimer deIoy ~ obZ7Z kI.08b 3s ~ 70.8Z0 ms TofoI deIoy ~ Z00 k70.8Z0 ms ~ I4.Io40Z4 seconds