CPEG300 Embedded System Design Lecture 8 Timer Hamad Bin Khalifa University, Spring 2018
Review 8051 port and port schematic Internal read/write data path Serial communication vs. parallel communication Data frame (start, stop, polarity, baud rate) UART I2C Data pattern, half-duplex SPI Full-duplex, how data are exchanged between master and slave 2
The Function of a Timer Timers are used for: interval timing, event counting or baud rate generation In interval timing applications, a timer is programmed to overflow at a regular interval and the following: o Set the timer overflow flag or o Generate an interrupt The flag or interrupt is used to synchronise the program to perform an action such as checking the state of inputs and updating the DAC output or ADC input This can also be used to generate waveforms at set frequencies Event counting is used to determine the number of occurrences of an event, rather than to measure the elapsed time between events. In this case, the timer functions as a counter. An event is any external stimulus that provides a high-to-low transition at the selected input pin The timers can also function as the baud rate generators for the 8051 s internal serial ports (UART) Baud rate is the bit rate of the serial port (the time period of a bit) 3
Timer Schematic Timer can also be used for counter; Use the internal clock and a DFF-chain; 4
Timer Resources in 8051 It has two timers; Timer 0 and Timer 1. 4 registers control the timer: TH, TL, TMOD, TCON The timers are 16-bit registers. Since 8051 is 8 bit microcontroller, each 16 bit register can be accessed as lower byte register (TL) and higher byte register (TH). These registers are accessed like other registers (A, B, R0, R1 etc.) in 8051 s SFR bank. MOV TL0, #4FH; MOV R5, TH0 Timer 1 Register Timer 0 Register TH1 (8 bit) TL1 (8 bit) TH0 (8 bit) TL0 (8 bit) 5
Control Registers - TMOD TMOD: Timer / counter mode (TMOD), in 8051 SFR 89H: MSB LSB GATE C / /T M1 M0 GATE C / /T M1 M0 Timer 1 TMOD Register Timer 0 An 8-bit register. Set the usage mode for two timers. o Set lower 4 bits for Timer 0 (Set to 0000 if not used) o Set upper 4 bits for Timer 1 (Set to 0000 if not used) Not bit-addressable. 6
Control Registers - TMOD TMOD: MSB LSB GATE C / /T M1 M0 GATE C / /T M1 M0 Timer 1 TMOD Register Timer 0 GATE Gating (start) control. If GATE=0, the timer starts/stops whenever TRx control bit is set/cleared. If GATE=1, timer/counter is enabled only if the INT0/1 pin is high and the TRx control pin is set. C/ /T Timer or counter selection: 0 for timer operation (input from internal system clock). 1 for counter operation (input from Tx input pin, count the number of pulses). M1 Mode bit 1 M0 Mode bit 0 7
Timer Mode Control M1 M0 Mode Operation 0 0 0 0 1 1 1 0 2 1 1 3 13-bit counter/timer mode 8-bit THx + 5-bit TLx (x=0/1) 16-bit counter/timer 8-bit THx + 8-bit TLx (x=0/1) 8-bit counter/timer with auto-reload THx holds a value which is to be reloaded into TLx each time it overflows Split timer mode Two 8-bit counter/timers (Timer 0 only) Example: if we want to program timer 0 in mode 2, use 8051 clock as the clock source, and only use instructions to start and stop the timer. TMOD= 0000 0010 (timer 1 not used, C/ /T=0, GATE=0) 8
Control Registers TCON TCON: Timer / counter control register (TCON) in 8051 SFR 88H: MSB LSB TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 Timer/Counter TCON Register Interrupts TR (run control bit) o TR is set by program to turn timer/counter on/off. TR=0: off (stop); TR=1: on (start) TF (timer flag, control flag) o TF is like a carry. Originally, TF=0. When TH-TL roll over from FFFFH to 0000H, the TF is set to 1. o TF=0 : not reach; TF=1: reach o If we enable interrupt, TF=1 will trigger ISR and be cleared automatically. 9
Timer Control Schematic 10
Timer Operation In the following, we use timer 0, timer mode 1 (16-bit) as an example: TH0-TL0 is incremented continuously when TR0 is set to 1. And the 8051 stops to increment TH0-TL0 when TR0 is cleared. The timer works with the internal system clock. In other words, the timer counts up in each machine cycle. When the timer (TH0-TL0) reaches its maximum of FFFFH, it rolls over to 0000, and TF0 is set to 1. Program should then check TF0 and stop the timer 0. 11
Timer Programming Sequence (1/3) Step 1: Choose mode 1 of timer 0 MOV TMOD, #01H Step 2: Set the original value to TH0 and TL0 MOV TH0, #FFH MOV TL0, #FCH Step 3: It s better to clear the timer flag to monitor: CLR TF0 (TCON is bit-addressable) Step 4: Start the timer: SETB TR0 Step 5: The 8051 starts to count up by incrementing the TH0-TL0. TH0-TL0 = FFFCH,FFFDH,FFFEH,FFFFH,0000H 12
Timer Programming Sequence (2/3) TR0=1 Start timer TH0 TL0 Stop timer TR0=0 FFFC FFFD FFFE FFFF 0000 TF = 0 TF = 0 TF = 0 TF = 0 TF = 1 TF Monitor TF until TF=1 Step 6: When TH0-TL0 rolls over from FFFFH to 0000, the 8051 set TF0=1. TH0-TL0= FFFEH, FFFFH, 0000H (Now TF0=1) 13
Timer Programming Sequence (3/3) Step 7: Keep monitoring the timer flag (TF) to see if it is raised. AGAIN: JNB TF0, AGAIN Step 8: Clear TR0 to stop the process. CLR TR0 Step 9: Clear the TF flag for the next round. CLR TF0 XTAL oscillato r 12 C/ /T = 0 Timer overflow flag TH TL TF TR TF goes high when FFFF à 0 14
Timer Delay Calculation In different modes, the timer/counter have different delay/count tolerance (Tcy is one machine cycle): In mode 0: maximum counts = 2 13 = 8192; maximum delay = 8192 Tcy In mode 1: maximum counts = 2 16 = 65536; maximum delay = 65536 Tcy In mode 2: maximum counts = 2 8 = 256; maximum delay = 256 Tcy In mode 3: Timer 0 is divided into 2 8-bit timers, maximum counts = 2 8 = 256; maximum delay = 256 Tcy For example: In hex: The initial value in TH-TL is FFE0H in mode 1, the actual delay is (FFFF-FFE0+1) x Tcy In decimal: the initial value in TH-TL is NNNNN, the actual delay is (65536-NNNNN) x Tcy 15
Example #1: Pulse Generation Generate a square wave with 50% duty cycle on P1.5, use Timer 0. ;each loop is a half clock MOV TMOD, #01 ;Timer 0,mode 1(16-bit, internal start/stop) HERE: MOV TL0, #0F2H ;Timer value = FFF2H MOV TH0, #0FFH CPL P1.5 ACALL DELAY P1.5 SJMP HERE 50% 50% ;generate delay using timer 0 DELAY: SETB TR0 ;start the timer 0 whole clock AGAIN: JNB TF0, AGAIN ;polling method, check the bit continuously CLR TR0 ;stop timer 0 CLR TF0 ;clear timer 0 flag RET 16
Example #1: Pulse Generation In the above program notice the following steps. 1. TMOD = 0000 0001 is loaded. 2. FFF2H is loaded into TH0 TL0. 3. P1.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 TR0. 6. Timer 0 counts up with each machine cycle: As the timer counts up, it goes through the states of FFF3, FFF4, FFF5, FFF6, FFF7, FFF8, FFF9, FFFA, FFFB, FFFC, FFFFD, FFFE, FFFFH. One more clock rolls it to 0, raising the timer flag (TF0 = 1). At that point, the JNB instruction falls through. 7. Timer 0 is stopped by the instruction CLR TR0. 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). 17
Example #2: Accurate Delay Design Same as example #1, if the oscillator clock f osc is 6 MHz and we have to control the frequency of the wave to be 1 KHz, how should we modify the program? Still use timer 1: Step 1: calculate the initial value to be loaded: o One machine cycle Tcy = 12 *1/f osc = 2 us o Since the target frequency is 1 KHz, its period is 1 ms, the target delay is 500 us o Initial value = maximum count target delay/tcy = 65536-500us/2us = 65286 o Convert the value to hex and load to TH0, TL0: FFH, 06H Change the code in example #1: MOV TL0,#06H MOV TH0,#0FFH ;Timer value = FF06H 18
Example #2: Accurate Delay Design Use interrupt for programming: ORG 0000H AJMP MAIN ORG 000BH ;Timer/counter 0 IVT IT0P: MOV TL0,#06H ;Reload data MOV TH0,#0FFH CPL P1.5 ;Inverse P1.5 RETI ORG 0100H MAIN: MOV TMOD,#01H ;Timer 0 mode 1 MOV TL0,#06H ;Initial value MOV TH0,#0FFH SETB TR1 ;Start timer MOV IE,#FFH ;Enable timer interrupt SJMP $ ;Wait for interrupt 19
Example #3: Counter If there s a continuous pulse on the Timer/Counter T1 pin (P3.5), please count the number of pulses N. If N>30000, turn on the LED connected to P2.0. Solution: Step 1: since 30000 counts is required, Mode 1 (16b counter) should be used. The signal is connected to T1, use Timer 1 Initial value calculation: o 65536 30000 = 35536 = 8AD0H o TH1 = 8AH; TL1=0D0H It works in counter mode: TMOD = 50H Program use polling or interrupt method: 20
Example #3: Counter ORG 0000 AJMP MAIN ORG 0030H MAIN: MOV TMOD,#50H ;Use timer 1, mode 1 and in counter mode MOV TH1,#8AH ;Initial value MOV TL1,#0D0H CLR P2.0 SETB TR1 ;Start counting WAIT: JNB TF1,WAIT ;polling the overflow bit CLR TF1 SETB P2.0 ;Light up LED END Timer 0 external input Pin 3.4 C/ /T = 1 TR0 Overflow flag TH0 TL0 TF0 TF0 goes high when FFFF à 0 21
Example #4: Generate a Large Time Delay The size of the time delay depends on two factors: The crystal frequency and the timer s 16-bit register, TH & TL The largest time delay is achieved by making TH=TL=0. What if that is not enough? Examine the following program and find the time delay in seconds (use 11.0592 MHz clock). Exclude the overhead due to the instructions in the loop. AGAIN: BACK: MOV TMOD,#10H MOV R3,#200 MOV TL1,#08 MOV TH1,#01 SETB TR1 JNB TF1,BACK CLR TR1 CLR TF1 DJNZ R3,AGAIN Solution: TH TL = 0108H = 264 in decimal 65536 264 = 65272. One of the timer delay = 65272 1.085 us = 70.820ms. Total delay = 200 70.820ms = 14.164024 seconds. 22
Example #5: Pulse Width Measurement Use GATE bit to measure the pulse duration on INT0/INT1. INT1 T pw ORG 0100H MAIN: MOV TMOD, #90H ;Timer 1 GATE=1 MOV TL0, #00H MOV TH0, #00H JB P3.3, $ ;Wait for P3.3=0 (the input on INT0/INT1 is inversed) SETB TR1 ;Start T1 JNB P3.3, $ ;Wait for P3.3=1 JB P3.3, $ ;Wait for P3.3=0 CLR TR1 ;Stop T1 counting MOV 30H, TL1 ;Save T1 counting MOV 31H, TH1 23
Example #6: Real Time Clock To design a real time clock showing second, minute and hour Method to design 1 second delay: As the maximum delay in Mode 1 is 65.536 ms, we can design a 50 ms delay and use a counter to count for 20 times of the timer overflow. Allocate 4 cells in the RAM to record ms, s, min, hour. o e.g., 43H à ms, 42H à s, 41H à min, 40H à hour Practice to write the program to implement the digital clock 24
Use Timer/Counter as External Interrupt Source Usage: Configure the timer/counter in counting mode and set the THx, TLx to its maximum. A falling edge on Tx will increase the counter by 1. The counter will set the overflow flag TFx=1 and send an interrupt request to the MCU. à Together with the 2 external interrupt resources, 8051 can then be configured to handle 36 interrupt resources. 25
Appendix: Example of Using Timer Mode 2 1. Chose mode 2 timer 0 MOV TMOD,#02H 2. Set the original value to TH0. MOV TH0,#38H 3. Clear the flag to TF0=0. CLR TF0 4. After TH0 is loaded with the 8-bit value, the 8051 gives a copy of it to TL0. TL0=TH0=38H 5. Start the timer. SETB TR0 6. The 8051 starts to count up by incrementing the TL0. TL0= 38H, 39H, 3AH,... 7. When TL0 rolls over from FFH to 00, the 8051 set TF0=1. Also, TL0 is reloaded automatically with the value kept by the TH0. TL0= FEH, FFH, 00H (Now TF0=1) The 8051 auto reload TL0=TH0=38H. CLR TF0 Go to Step 6 (i.e., TL0 is incrementing continuously). Note that we must clear TF0 when TL0 rolls over. Thus, we can monitor TF0 in next process. Clear TR0 to stop the process. CLR TR0 26
Appendix: Read Timer/Counter Data If THx, TLx need to be read during the execution of the program, error may occur if TLx is generating a carry to THx. Correct reading method: Read THx, then read TLx, then read THx again. If the two THx readings are the same, then the reading is correct. Otherwise, read the data again. RDTIME: MOV A,TH0 ;Read (TH0) MOV R0,TL0 ;Read (TL0) CJNE A,TH0, RDTIME ;Compare the two readings of (TH0) ;Read again if not the same MOV R1,A ;Store (TH0) in R1 RET 27