8051 TimerCounter Lec note 7 hsabaghianb kashanu ac
8051 Timer/Counter Lec note 7 hsabaghianb @ kashanu. ac. ir Microprocessors 7 -1
Timers /Counters Programming q 8051 has 2 timers/counters v timer/counter 0 v timer/counter 1 1. Timer is used as time delay generator v Clock source : crystal frequency 2. An event counter v Clock source : External input Pulse v For example : Ø number of people passing through an entrance Ø number of wheel rotations Ø any other event that can be converted to pulses hsabaghianb @ kashanu. ac. ir Microprocessors 7 -2
Timer hsabaghianb @ kashanu. ac. ir Microprocessors 7 -3
Timer q initialize value of registers q Start timer (counts up) q Input from internal system clock (machine cycle) q When the registers overflow to 0 , TF is set 8051 Set Timer 0 P 2 P 1 TH 0 to LCD TL 0 hsabaghianb @ kashanu. ac. ir Microprocessors 7 -4
Counter q Counting the number of events v Shows the number of events on registers v External input : T 0 input pin (P 3. 4) for Counter 0 v External input : T 1 input pin (P 3. 5) for Counter 1 (Note : We use Tx to denote T 0 or T 1) 8051 TH 0 TL 0 a switch hsabaghianb @ kashanu. ac. ir T 0 P 1 to LCD P 3. 4 Microprocessors 7 -5
Registers Used in Timer/Counter q TH 0, TL 0, TH 1, TL 1 q TMOD (Timer mode register) q TCON (Timer control register) q You can see Appendix H (pages 413 -415) for details. q Since 8052 has 3 timers/counters, the formats of these control registers are different. v. T 2 CON (Timer 2 control register), TH 2 and TL 2 used for 8052 only. hsabaghianb @ kashanu. ac. ir Microprocessors 7 -6
Basic Registers of the Timer q Both timer 0 and timer 1 are 16 bits wide. v. These registers stores Øthe time delay as a timer Øthe number of events as a counter v. Timer 0: TH 0 & TL 0 ØTimer 0 high byte, timer 0 low byte v. Timer 1: TH 1 & TL 1 ØTimer 1 high byte, timer 1 low byte v. Each 16 -bit timer can be accessed as two separate registers of low byte and high byte. hsabaghianb @ kashanu. ac. ir Microprocessors 7 -7
Timer Registers TH 0 TL 0 D 15 D 14 D 13 D 12 D 11 D 10 D 9 D 8 D 7 D 6 D 5 D 4 D 3 D 2 D 1 D 0 Timer 0 TH 1 TL 1 D 15 D 14 D 13 D 12 D 11 D 10 D 9 D 8 D 7 D 6 D 5 D 4 D 3 D 2 D 1 D 0 Timer 1 hsabaghianb @ kashanu. ac. ir Microprocessors 7 -8
TMOD Register q Timer mode register = TMOD MOV TMOD, #21 H v. An 8 -bit register Ø lower 4 bits : Timer 0 Mode setting (0000 : not used) Ø upper 4 bits : Timer 1 Mode setting (0000 : not used) v. Not bit-addressable (MSB) GATE C/T M 1 Timer 1 hsabaghianb @ kashanu. ac. ir M 0 GATE C/T M 1 Timer 0 (LSB) M 0 Microprocessors 7 -9
TMOD Register GATE C/T 0 : Timer/counter counts only while TRx control pin is set. 1 : Timer/counter counts only while TRx control pin is set and INTx pin is high 0 : Timer operation (clock : Machine cycle) 1 : Counter operation (clock : Tx input pin) (MSB) GATE (LSB) C/T M 1 Timer 1 hsabaghianb @ kashanu. ac. ir M 0 GATE C/T M 1 Timer 0 Microprocessors 7 -10
Gate q Every timer has a mean of starting and stopping. v. GATE=0 ØInternal control ØThe start and stop of the timer are controlled by the software (Set/clear the TR) v. GATE=1 ØExternal control ØThe hardware way of starting and stopping the timer by software and an external source. ØTimer/counter is enabled only while the INT pin is high and the TR control pin is set (TR). hsabaghianb @ kashanu. ac. ir Microprocessors 7 -11
M 1, M 0 q M 0 and M 1 select the timer mode for timers 0 & 1. M 1 M 0 Mode 0 0 1 1 1 0 2 1 1 3 hsabaghianb @ kashanu. ac. ir Operating Mode 13 -bit timer mode 8 -bit THx + 5 -bit TLx (x= 0 or 1) 16 -bit timer mode 8 -bit THx + 8 -bit TLx 8 -bit auto reload timer/counter; THx holds a value which is to be reloaded into TLx each time it overflows. Split timer mode Microprocessors 7 -12
Example 9 -3 Find the value for TMOD if we want to program timer 0 in mode 2, use 8051 XTAL for the clock source, and use instructions to start and stop the timer. Solution: timer 1 timer 0 TMOD= 0000 0010 Timer 1 is not used. Timer 0, mode 2, C/T = 0 to use XTAL clock source (timer) gate = 0 to use internal (software) start and stop method. hsabaghianb @ kashanu. ac. ir Microprocessors 7 -13
Timer modes hsabaghianb @ kashanu. ac. ir Microprocessors 7 -14
TCON Register (1/2) q Timer control register: TMOD v. Upper nibble : timer/counter v. Lower nibble : interrupts q TR (run control bit) v. TR 0 : Timer/counter 0 v. TR 1: Timer/counter 1. v. Turn timer/counter on/off. ØTR=0: off (stop) ØTR=1: on (start) (MSB) TF 1 TR 1 Timer 1 hsabaghianb @ kashanu. ac. ir TF 0 TR 0 Timer 0 IE 1 IT 1 IE 0 for Interrupt (LSB) IT 0 Microprocessors 7 -15
TCON Register (2/2) q TF (timer flag, control flag) v. TF 0 : timer/counter 0 v. TF 1 : timer/counter 1. v. TF is like a carry. Originally, TF=0. When TH-TL roll over to 0000 from FFFFH, the TF is set to 1. ØTF=0 : not reach ØTF=1: reach ØIf we enable interrupt, TF=1 will trigger ISR. (MSB) TF 1 TR 1 Timer 1 hsabaghianb @ kashanu. ac. ir TF 0 TR 0 Timer 0 IE 1 IT 1 IE 0 for Interrupt (LSB) IT 0 Microprocessors 7 -16
Timer Control Instructions For timer 0 SETB TR 0 CLR TR 0 = = SETB TCON. 4 CLR TCON. 4 SETB TF 0 CLR TF 0 = = SETB TCON. 5 CLR TCON. 5 SETB TR 1 CLR TR 1 = = SETB TCON. 6 CLR TCON. 6 SETB TF 1 CLR TF 1 = = SETB TCON. 7 CLR TCON. 7 For timer 1 TCON: Timer/Counter Control Register TF 1 TR 1 hsabaghianb @ kashanu. ac. ir TF 0 TR 0 IE 1 IT 1 IE 0 IT 0 Microprocessors 7 -17
Timer Mode 1 q In following, we all use timer 0 as an example. q 16 -bit timer (TH 0 and TL 0) q TH 0 -TL 0 is incremented continuously when TR 0 is set to 1. And the 8051 stops to increment TH 0 -TL 0 when TR 0 is cleared. q The timer works with the internal system clock. In other words, the timer counts up each machine cycle. q When the timer (TH 0 -TL 0) reaches its maximum of FFFFH, it rolls over to 0000, and TF 0 is raised. q Programmer should check TF 0 and stop the timer 0. hsabaghianb @ kashanu. ac. ir Microprocessors 7 -18
Steps of Mode 1 (1/3) 1. Choose mode 1 timer 0 v MOV TMOD, #01 H 2. Set the original value to TH 0 and TL 0 v MOV TH 0, #FFH v MOV TL 0, #FCH 3. You had better to clear the flag TF 0 v CLR TF 0 4. Start the timer. v SETB TR 0 hsabaghianb @ kashanu. ac. ir Microprocessors 7 -19
Steps of Mode 1 (2/3) 5. The 8051 starts to count up by incrementing the TH 0 -TL 0. v. TH 0 -TL 0= FFFCH, FFFDH, FFFEH, FFFFH, 0000 H TR 0=1 Start timer TH 0 TR 0=0 TL 0 Stop timer FFFC FFFD FFFE FFFF 0000 TF = 0 TF = 1 TF hsabaghianb @ kashanu. ac. ir Monitor TF until TF=1 Microprocessors 7 -20
Steps of Mode 1 (3/3) 6. When TH 0 -TL 0 rolls over from FFFFH to 0000, the 8051 set TF 0=1. TH 0 -TL 0= FFFEH, FFFFH, 0000 H (Now TF 0=1) 7. Keep monitoring the timer flag (TF) to see if it is raised. AGAIN: JNB TF 0, AGAIN 8. Clear TR 0 to stop the process. CLR TR 0 9. Clear the TF flag for the next round. CLR TF 0 hsabaghianb @ kashanu. ac. ir Microprocessors 7 -21
Mode 1 Programming XTAL oscillator ÷ 12 C/T = 0 Timer overflow flag TH TR hsabaghianb @ kashanu. ac. ir TL TF goes high when FFFF TF 0 Microprocessors 7 -22
Timer Delay Calculation for XTAL = 11. 0592 MHz (a) q q q in hex (FFFF – YYXX + 1) × 1. 085 s where YYXX are TH, TL initial values respectively. Notice that values YYXX are in hex. (b) in decimal q Convert YYXX values of the TH, TL register to decimal to get a NNNNN decimal number q then (65536 – NNNNN) × 1. 085 s hsabaghianb @ kashanu. ac. ir Microprocessors 7 -23
Example 9 -4 (1/3) q square wave of 50% duty on P 1. 5 q Timer 0 is used ; each loop is a half clock MOV TMOD, #01 ; Timer 0, mode 1(16 -bit) HERE: MOV TL 0, #0 F 2 H ; Timer value = FFF 2 H MOV TH 0, #0 FFH CPL P 1. 5 ACALL DELAY P 1. 5 SJMP HERE 50% whole clock hsabaghianb @ kashanu. ac. ir Microprocessors 7 -24
Example 9 -4 (2/3) ; generate delay using timer 0 DELAY: SETB TR 0 ; start the timer 0 AGAIN: JNB TF 0, AGAIN CLR TR 0 ; stop timer 0 CLR TF 0 ; clear timer 0 flag RET FFF 2 FFF 3 FFF 4 FFFF 0000 TF 0 = 0 TF 0 = 1 hsabaghianb @ kashanu. ac. ir Microprocessors 7 -25
Example 9 -4 (3/3) Solution: In the above program notice the following steps. 1. TMOD = 0000 0001 is loaded. 2. FFF 2 H is loaded into TH 0 – TL 0. 3. P 1. 5 is toggled for the high and low portions of the pulse. 4. The DELAY subroutine using the timer is called. 5. In the DELAY subroutine, timer 0 is started by the “SETB TR 0” instruction. 6. Timer 0 counts up with the passing of each clock, which is provided by the crystal oscillator. As the timer counts up, it goes through the states of FFF 3, FFF 4, FFF 5, FFF 6, FFF 7, FFF 8, FFF 9, FFFA, FFFB, FFFC, FFFFD, FFFE, FFFFH. One more clock rolls it to 0, raising the timer flag (TF 0 = 1). At that point, the JNB instruction falls through. 7. Timer 0 is stopped by the instruction “CLR TR 0”. The DELAY 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). hsabaghianb @ kashanu. ac. ir Microprocessors 7 -26
Example 9 -9 (1/2) q This program generates a square wave on pin P 1. 5 Using timer 1 q Find the frequency. (dont include the overhead of instruction delay) q XTAL = 11. 0592 MHz MOV AGAIN: MOV SETB BACK: JNB CLR CPL CLR SJMP hsabaghianb @ kashanu. ac. ir TMOD, #10 H TL 1, #34 H TH 1, #76 H TR 1 TF 1, BACK TR 1 P 1. 5 TF 1 AGAIN ; timer 1, mode 1 ; timer value=3476 H ; start ; stop ; next half clock ; clear timer flag 1 ; reload timer 1 Microprocessors 7 -27
Example 9 -9 (2/2) Solution: FFFFH – 7634 H + 1 = 89 CCH = 35276 clock count Half period = 35276 × 1. 085 s = 38. 274 ms Whole period = 2 × 38. 274 ms = 76. 548 ms Frequency = 1/ 76. 548 ms = 13. 064 Hz. Note Mode 1 is not auto reload then the program must reload the TH 1, TL 1 register every timer overflow if we want to have a continuous wave. hsabaghianb @ kashanu. ac. ir Microprocessors 7 -28
Find Timer Values q Assume that XTAL = 11. 0592 MHz. q And we know desired delay q how to find the values for the TH, TL ? 1. 2. 3. 4. Divide the delay by 1. 085 s and get n. Perform 65536 –n Convert the result of Step 2 to hex (yyxx ) Set TH = yy and TL = xx. hsabaghianb @ kashanu. ac. ir Microprocessors 7 -29
Example 9 -12 (1/2) q Assuming XTAL = 11. 0592 MHz, q write a program to generate a square wave of 50 Hz frequency on pin P 2. 3. Solution: 1. 2. 3. 4. 5. The period of the square wave = 1 / 50 Hz = 20 ms. The high or low portion of the square wave = 10 ms / 1. 085 s = 9216 65536 – 9216 = 56320 in decimal = DC 00 H in hex. TL 1 = 00 H and TH 1 = DCH. hsabaghianb @ kashanu. ac. ir Microprocessors 7 -30
Example 9 -12 (2/2) MOV AGAIN: MOV SETB BACK: JNB CLR CPL CLR SJMP hsabaghianb @ kashanu. ac. ir TMOD, #10 H TL 1, #00 TH 1, #0 DCH TR 1 TF 1, BACK TR 1 P 2. 3 TF 1 AGAIN ; timer 1, mode 1 ; Timer value = DC 00 H ; start ; stop ; clear timer flag 1 ; reload timer since ; mode 1 is not ; auto-reload Microprocessors 7 -31
Generate a Large Time Delay q The size of the time delay depends on two factors: v They crystal frequency v The timer’s 16 -bit register, TH & TL q The largest time delay is achieved by making TH=TL=0. q What if that is not enough? q Next Example show to achieve large time delay hsabaghianb @ kashanu. ac. ir Microprocessors 7 -32
Example 9 -13 Examine the following program and find the time delay in seconds. Exclude the overhead due to the instructions in the loop. MOV TMOD, #10 H MOV R 3, #200 AGAIN: MOV TL 1, #08 MOV TH 1, #01 SETB TR 1 BACK: JNB TF 1, BACK CLR TR 1 CLR TF 1 DJNZ R 3, AGAIN Solution: TH – TL = 0108 H = 264 in decimal 65536 – 264 = 65272. One of the timer delay = 65272 × 1. 085 s = 70. 820 ms Total delay = 200 × 70. 820 ms = 14. 164024 seconds hsabaghianb @ kashanu. ac. ir Microprocessors 7 -33
Timer Mode 0 q Mode 0 is exactly like mode 1 except that it is a 13 bit timer instead of 16 -bit. v 8 -bit TH 0 v 5 -bit TL 0 q The counter can hold values between 0000 to 1 FFF in TH 0 -TL 0. v 213 -1= 2000 H-1=1 FFFH q We set the initial values TH 0 -TL 0 to count up. q When the timer reaches its maximum of 1 FFFH, it rolls over to 0000, and TF 0 is raised. hsabaghianb @ kashanu. ac. ir Microprocessors 7 -34
Timer Mode 2 q 8 -bit timer. v It allows only values of 00 to FFH to be loaded into TH 0. q Auto-reloading q TL 0 is incremented continuously when TR 0=1. q next example: 200 µs delay on timer 0. q See Examples 9 -14 to 9 -16 hsabaghianb @ kashanu. ac. ir Microprocessors 7 -35
Steps of Mode 2 (1/2) 1. Chose mode 2 timer 0 MOV TMOD, #02 H 2. Set the original value to TH 0. MOV TH 0, #38 H 3. Clear the flag to TF 0=0. CLR TF 0 4. After TH 0 is loaded with the 8 -bit value, the 8051 gives a copy of it to TL 0=TH 0=38 H 5. Start the timer. SETB TR 0 hsabaghianb @ kashanu. ac. ir Microprocessors 7 -36
Steps of Mode 2 (2/2) 6. The 8051 starts to count up by incrementing the TL 0. v TL 0= 38 H, 39 H, 3 AH, . . 7. When TL 0 rolls over from FFH to 00, the 8051 set TF 0=1. Also, TL 0 is reloaded automatically with the value kept by the TH 0. v v TL 0= FEH, FFH, 00 H (Now TF 0=1) The 8051 auto reload TL 0=TH 0=38 H. Clr TF 0 Go to Step 6 (i. e. , TL 0 is incrementing continuously). q Note that we must clear TF 0 when TL 0 rolls over. Thus, we can monitor TF 0 in next process. q Clear TR 0 to stop the process. v Clr TR 0 hsabaghianb @ kashanu. ac. ir Microprocessors 7 -37
Timer 1 Mode 2 with internal Input XTAL oscillator ÷ 12 C/T = 0 TL 1 TF 1 overflow flag reload TR 1 TH 1 TF goes high when FF hsabaghianb @ kashanu. ac. ir 0 Microprocessors 7 -38
Example 9 -15 q Find the frequency of a square wave generated on pin P 1. 0. Solution: MOV TMOD, #2 H ; Timer 0, mode 2 MOV TH 0, #0 AGAIN: MOV R 5, #250 ; count 250 times ACALL DELAY CPL P 1. 0 SJMP AGAIN DELAY: SETB BACK: JNB CLR DJNZ RET TR 0 TF 0, BACK TR 0 TF 0 R 5, DELAY ; start ; wait until TL 0 ovrflw auto-reload ; stop ; clear TF T = 2 (250 × 256 × 1. 085 s) = 138. 88 ms, and frequency = 72 Hz. hsabaghianb @ kashanu. ac. ir Microprocessors 7 -39
Example 9 -16 Assuming that we are programming the timers for mode 2, find the value (in hex) loaded into TH for each of the following cases. (a) MOV TH 1, #-200 (b) MOV TH 0, #-60 (c) MOV TH 1, #-3 (d) MOV TH 1, #-12 (e) MOV TH 0, #-48 Solution: Some 8051 assemblers provide this way. -200 = -C 8 H 2’s complement of – 200 = 100 H – C 8 H = 38 H Decimal -200 = - C 8 H - 60 = - 3 CH - 3 - 12 2’s complement (TH value) 38 H C 4 H FDH F 4 H - 48 D 0 H hsabaghianb @ kashanu. ac. ir Microprocessors 7 -40
Example 9 -17 (1/2) Find (a) the frequency of the square wave generated in the following code (b) the duty cycle of this wave. Solution: “MOV TH 0, #-150” uses 150 clocks. The DELAY subroutine = 150 × 1. 085 s = 162 s. The high portion is twice tat of the low portion (66% duty cycle). The total period = high portion + low portion T= 325. 5 s + 162. 25 s = 488. 25 s Frequency = 2. 048 k. Hz. hsabaghianb @ kashanu. ac. ir Microprocessors 7 -41
Example 9 -17 (2/2) MOV TMOD, #2 H MOV TH 0, #-150 AGAIN: SETB P 1. 3 ACALL DELAY CLR P 1. 3 ACALL DEALY SJMP AGAIN DELAY: SETB BACK: JNB CLR RET hsabaghianb @ kashanu. ac. ir TR 0 TF 0, BACK TR 0 TF 0 ; Timer 0, mode 2 ; Count=150 high period low period ; start ; stop ; clear TF Microprocessors 7 -42
Counter q These timers can also be used as counters counting events happening outside the 8051. q When the timer is used as a counter, it is a pulse outside of the 8051 that increments the TH, TL. q When C/T=1, the counter counts up as pulses are fed from v. T 0: timer 0 input (Pin 14, P 3. 4) v. T 1: timer 1 input (Pin 15, P 3. 5) hsabaghianb @ kashanu. ac. ir Microprocessors 7 -43
Port 3 Pins Used For Timers 0 and 1 Pin Port Pin Function Description 14 15 P 3. 4 P 3. 5 T 0 T 1 Timer/Counter 0 external input Timer/Counter 1 external input (MSB) GATE C/T=1 M 1 Timer 1 hsabaghianb @ kashanu. ac. ir (LSB) M 0 GATE C/T=1 M 1 Timer 0 Microprocessors 7 -44
Timer/Counter selection hsabaghianb @ kashanu. ac. ir Microprocessors 7 -45
Counter Mode 1 q 16 -bit counter (TH 0 and TL 0) q TH 0 -TL 0 is incremented when TR 0 is set to 1 and an external pulse (in T 0) occurs. q When the counter (TH 0 -TL 0) reaches its maximum of FFFFH, it rolls over to 0000, and TF 0 is raised. q Programmers should monitor TF 0 continuously and stop the counter 0. q Programmers can set the initial value of TH 0 -TL 0 and let TF 0=1 as an indicator to show a special condition. (ex: 100 people have come). hsabaghianb @ kashanu. ac. ir Microprocessors 7 -46
Timer 0 with External Input (Mode 1) Timer 0 external input Pin 3. 4 C/T = 1 TR 0 hsabaghianb @ kashanu. ac. ir overflow flag TH 0 TL 0 TF 0 goes high when FFFF 0 Microprocessors 7 -47
Counter Mode 2 q 8 -bit counter. It allows only values of 00 to FFH to be loaded into TH 0 q Auto-reloading • TL 0 is incremented if TR 0=1 and external pulse occurs. hsabaghianb @ kashanu. ac. ir Microprocessors 7 -48
Example 9 -18 (1/2) Assuming that clock pulses are fed into pin T 1, write a program for counter 1 in mode 2 to count the pulses and display the state of the TL 1 count on P 2. Solution: MOV SETB AGAIN: SETB BACK: MOV JNB CLR SJMP TMOD, #01100000 B TH 1, #0 P 3. 5 TR 1 A, TL 1 P 2, A TF 1, Back TR 1 TF 1 AGAIN hsabaghianb @ kashanu. ac. ir ; mode 2, counter 1 ; make T 1 input port ; start ; display in P 2 ; overflow ; stop ; make TF=0 ; keep doing it Microprocessors 7 -49
Example 9 -18 (2/2) q Timer 1 as an event counter fed into pin 3. 5. q “SETB P 3. 5” make P 3. 5 an input port by making it high 8051 P 2 is connected to 8 LEDs and input T 1 to pulse. P 2 T 1 hsabaghianb @ kashanu. ac. ir to LEDs P 3. 5 Microprocessors 7 -50
Example 9 -19 (1/3) Assume that a 1 -Hz frequency pulse is connected to input pin 3. 4. Write a program to display counter 0 on an LCD. Set the initial value of TH 0 to -60. Solution: Note that on the first round, it starts from 0 and counts 256 events, since on RESET, TL 0=0. To solve this problem, load TH 0 with -60 at the beginning of the program. 8051 P 1 1 Hz clock hsabaghianb @ kashanu. ac. ir T 0 to LCD P 3. 4 Microprocessors 7 -51
Example 9 -19 (2/3) ACALL LCD_SET_UP MOV TMOD, #00000110 B MOV TH 0, #-60 SETB P 3. 4 AGAIN: SETB TR 0 BACK: MOV A, TL 0 ACALL CONV ACALL DISPLY JNB TF 0, BACK CLR TR 0 CLR TF 0 SJMP AGAIN hsabaghianb @ kashanu. ac. ir ; initialize the LCD ; Counter 0, mode 2 ; make T 0 as input ; starts the counter ; every 60 events ; convert in R 2, R 3, R 4 ; display on LCD ; loop if TF 0=0 ; stop Microprocessors 7 -52
Example 9 -19 (3/3) ; converting 8 -bit binary to ASCII CONV: MOV DIV ORL MOV MOV ORL MOV RET B, #10 AB R 2, B B, #10 AB A, #30 H R 4, A A, B A, #30 H R 3, A A, R 2 A, #30 H R 2, A hsabaghianb @ kashanu. ac. ir ; divide by 10 ; save low digit ; divide by 10 once more ; make it ASCII R 4 R 3 R 2 ; ACALL LCD_DISPLAY here Microprocessors 7 -53
A Digital Clock q Example 9 -19 shows a simple digital clock. v If we feed an external square wave of 60 Hz frequency into the timer/counter, we can generate the second, the minute, and the hour out of this input frequency and display the result on an LCD. q You might think that the use of the instruction “JNB TF 0, target” to monitor the raising of the TF 0 flag is a waste of the microcontroller’s time. v The solution is the use of interrupt. See Chapter 11. v In using interrupts we can do other things with the 8051. v When the TF flag is raised it will inform us. hsabaghianb @ kashanu. ac. ir Microprocessors 7 -54
GATE=1 in TMOD q All discuss so far has assumed that GATE=0. v. The timer is stared with instructions “SETB TR 0” and “SETB TR 1” for timers 0 and 1, respectively. q If GATE=1, we can use hardware to control the start and stop of the timers. v. INT 0 (P 3. 2, pin 12) starts and stops timer 0 v. INT 1 (P 3. 3, pin 13) starts and stops timer 1 v. This allows us to start or stop the timer externally at any time via a simple switch. hsabaghianb @ kashanu. ac. ir Microprocessors 7 -55
GATE (external control) q Timer 0 must be turned on by “SETB TR 0” q If GATE=1 count up if v INT 0 input is high v. TR 0=1 q If GATE=0 count up if v. TR 0=1 hsabaghianb @ kashanu. ac. ir Microprocessors 7 -56
Timer SFR Register hsabaghianb @ kashanu. ac. ir Microprocessors 7 -57
Timer 2 Control Register hsabaghianb @ kashanu. ac. ir Microprocessors 7 -58
Timer 2 Capture Mode hsabaghianb @ kashanu. ac. ir Microprocessors 7 -59
Timer 2 Auto Reload hsabaghianb @ kashanu. ac. ir Microprocessors 7 -60
- Slides: 60