8051 Timers 8051 Micro-controller by Ravinder Nath Rajotiya - December 3, 20210 Share on Facebook Share Send email Mail Print Print Table of Contents Toggle Timers in 8051Use of timers:Registers associated with timer operation are:Format of TMOD and TCON registersTimer Mode of Operations Hardware circuit of timers in different modesUsing the timersFinding the count value for Setting up the timer ExamplesProgramming example: Timers in 8051 There are two timer in 8051, Timer-0, Timer-1;— Use of timers: To produce precise delay timing Counting the events Generating baud rate Registers associated with timer operation are: TMOD 89h TCON 88h TH0 8Ch TL0 8Ah TH1 8Dh TL1 8Bh Format of TMOD and TCON registers TMOD SFR (89h) Timer-1 Timer-0 D7 D6 D5 D4 D3 D2 D1 D0 Gate C’/T M1 M0 Gate C’/T M1 M0 1: COUNT only if INT1 (P3.3) input pin is high 0-count regardless of INT1 1- Count pulses on pin T1(P3.5) 0- Count every machine cycle 00 Mode-0; 13 bit timer 01- Timer 16 bit 10 mode-2; 8-bit auto reload 11- Split timer mode 1: COUNT only if INT0 (P3.3) input pin is high 0-count regardless of INT0 1 Count pulses on pin T1(P3.5) Count eevery machine cycle 00 Mode-0; 13 bit timer 01- Timer 16 bit 10 mode-2; 8-bit auto reload 11- Split timer mode Timer Control SFR Timer Control TCON SFR (88h) Bits D7 D6 D5 D4 D3 D2 D1 D0 Bit Address 8F 8E 8D 8C 8B 8A 89 88 Bit Name TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 Description Timer-1 Overflow Flag Timer-1 run control, set to 1 by to run timer-1; ‘0’ to stop Timer-0 Overflow Flag Timer-0run control, set to 1 by to run timer-0; ‘0’ to stop Ext-1 interrupt edge detect bit Interrupt type-1 control bit, set to ‘1’ by software for edge triggering and cleared for level triggering Ext-0 interrupt edge detect bit Interrupt type-0 control bit, set to ‘1’ by software for edge triggering and cleared for level triggering Timer Mode of Operations M1 M0 Mode Description 0 0 0 13-bit timer, 5 bit of the low order byte and all bits of high order byte. With 13-bits a counter can count 8192; 0 –to- 8191 counts 0 1 1 16-bit timer: With 16-bit the timer can count 65536; 0-to-65535 counts or from any initial value –to- 65535. 1 0 2 8-bit auto reload: In this mode timer operate as an 8-bit timer and can count 256 (0-to- 255 or from any initial value to 255). TH1/0 is used to hold the valu for auto-reload operation and TH1/TL0 is used for counting. The initial value from TH1/0 will be reloaded in TL1/0 when the timer overflows. 1 1 3 Split timer- Split timer mode is applicable only for timer-0. The timer-0 will essentially become two separate 8-bit timers ie. TL0 becomes Timer-0, and TH0 becomes timer-1. In this mode timer-1 (Th1 and TL1) cannot be started or stopped since these bits are now linked to TH0-timer-1 of split mode. In applications requiring two separate timers and baud rate generators; the real timer-1 becomes the baud rate generator and split mode timers TH0 and TL0 can count from 0 to 255. Hardware circuit of timers in different modes Figure: 8051 Timer hardware circuit Using the timers To use the timers, we need to go through the following steps Algorithm Decide what mode of operation is required Initialize TMOD register Write timer value in timer registers TH1/TL1 OR TH0/TL0 Start the timer by setting the bit TR1 OR TR0 Check TF1 or TF0 to proceed with the programmed i/o or interrupt i/o mode of operation. Programmed mode i/o checks the condition of flags JNB TF1/TF0 repeat itself else stop the counter by clearing the TR1/0. Whereas in interrupt i/o requires IE flag to set to take care of interrupt and also to write an ISR Finding the count value for Setting up the timer Let Fosc = 8051 oscillator frequency Ftimer = 8051 Timer clock frequency = Fosc/ 12 ; TPosc (Pulse time for Osc Freq) = 12/Fosc Fout = Output Frequency Require TPout = 1/Fout Delay Time TD = TPout / TPosc = TPosc / (12/Fosc) Timer value = Max Count – Timer Delay count Examples Example-1: Calculate the timer value to generate a square wave of 10KHz frequency, if oscillator frequency of 8051 is 11.0592MHz. Solution: Timer freq = Osc Freq / 12 = 11.0592/12 = 0.9216MHz Timer Pulse time = 1/0.9216M = 1.085us; so the timer will count one unit in 1.085us. Output frequency = 10KHz pulse Time = 1/10,000 = .0001 = 100us; For square wave this pulse remain high for ½ the time i.e 50us and off for 50us In 50us; the timer will count 50/1.085 = 46 If timer is operated with 8-bit auto reload, then the initialization value of timer = 255 – 46 = 209 Example-2: calculate the initialization value for timer-1 to generate a time delay of 20ms, if 8051 is operating at 11.0592 MHz oscillator. Solution: Osc Freq = 11.0592MHz so, the timer frequency = 11.0592/12 = 0.9216MHz Timer freq Pulse time = 1/0.9216M = 1.085us Delay time required = 20ms Timer count = 20ms / 1.085us = 18,432 Timer initialization value = 65535 – 18432 = 47,103 In Hexadecimal 47103 = B7FF So the timer register (TH/TL) should be set to B7FFh Example-3: calculate the initialization value for timer-1 to generate a time delay of 20ms, if 8051 is operating at 11.0592 MHz oscillator. Osc Freq = 11.0592MHz so, the timer frequency = 11.0592/12 = 0.9216MHz Timer freq Pulse time = 1/0.9216M = 1.085us Delay time required = 10us Timer count = 10us / 1.085us = 9.216 round to 9 Timer initialization value (mode-1, 8-bit) = 255 – 9 = 246 In Hexadecimal 246 = 0xF6 So the timer register (TH/TL) should be set to 0xF6 Programming example: Write an assembly language program to generate a 2 KHz square wave frequency using timer-0 of 8051. Assume oscillator frequency to be 11.0592MHz Solution: Frequency required= 2 KHz, Pulse time = 1/2K = 500us; For square wave this time is further divided by 2, because for half the time pulse goes high and another half the time the pulse remain low.. So the pulse time = 500us/2 =250us Osc freq= 11.0592MHz, timer frequency = 11.0592MHz/12 = 0.9216MHz; timer pulse time = 1/.9216MHz = 1.085us. Count value = delay required/timer pulse time = 250us / 1.085us = 230.4 rounding to 230 Initialization value = TL0 =00; TH0 = 255-230 = 25 Program using programmed i/o (Testing TF0) Program using interrupt I/O MOV TMOD, #02h ; initialize timer mode CLR TF0 ; clear overflow flag CLR P1.0 ; output 0 on P1.0 MOV TH0, #25D ; initialize count SETB TR0 ; start timer LOOP : JNB TF0, LOOP ; poll the TF0 CLR TF0 ; clear overflow flag CPL P1.0 ; complement bit at P1.0 LJMP LOOP ; jump to loop to poll the flag again ORG 000Bh ; timer-0 ISR CLR TF0 ; clear overflow flag CPL P1.0 ; toggle P1.0 RETI ORG 0000 ; main program SJMP 0030h ; jump to main program location MOV TMOD, #02h ; initialize TMOD MOV TH0, #25D ; initialize count MOV IE, #82h ; initialize IE register SETB TR0 ; start the timer-0 LOOP : SJMP LOOP ; Share on Facebook Share Send email Mail Print Print