Wednesday, March 23, 2016

8051 Timer Programming

The 8051 has two timers/counters. They can be used either as timers to generate a time delay or as counters to count events happening outside the microcontroller.

The two timers, Timer 0 and Timer 1, are 16-bit wide. Both timers 0 and 1 use the same register, called TMOD, to set the various timer operation modes. There are three modes: 0, 1, 2 and 3. Mode 0 is a 13-bit timer, mode 1 is a 16-bit timer, mode 2 is an 8-bit timer and mode 3 is split timer mode.

C/T bit in the TMOD register is used to decide whether the timer is used as a delay generator or an event counter. If C/T = 0, it is used as a timer for time delay generation. The clock source for the time delay is the crystal frequency of the 8051.

The timers in the 8051 can be controlled by both software and hardware. The start and stop of the timer are controlled by way of software by the TR (timer start) bits TRO and TR1. Software instructions start and stop the timers as long as GATE = 0 in the TMOD register. The hardware way of starting and stopping the timer by an external source is achieved by making GATE = 1 in the TMOD register.

Mode 1:
  • It is a 16-bit timer; therefore, it allows values of 0000 to FFFFH to be loaded into the timer’s registers TL and TH.
  • After TH and TL are loaded with a 16-bit initial value, the timer must be started. This is done by “SETB TRO” for Timer 0 and “SETB TR1″for Timer 1.
  • After the timer is started, it starts to count 1. up. It counts up until it reaches its limit of FFFFH. When it rolls over from FFFFH to 0000, it sets high a flag bit called TF (timer flag). This timer flag can be monitored. When this timer flag is raised, one option would be to stop the timer with the instructions “CLR TRO” or “CLR TR1″, for Timer 0 and Timer 1, respectively. Again, it must be noted that each timer has its own timer flag: TFO for Timer 0, and TF1 for Timer 1.
  • After the timer reaches its limit and rolls over, in order to repeat the process the registers TH and TL must be reloaded with the original value, and TF must be reset to 0.
ORG 0H
MAIN :MOV TMOD, #01H; Timer 0, mode 1
START : MOV TL0, #0F2H ; load F2 in to TL0
MOV TH0, #0FFH ; load FF in to TH0
CPL P1.5 ; toggle P1.5
ACALL DELAY
SJMP START
; delay using timer 0
ORG 100H
DELAY:SETB TR0 ; start timer 0
LOOP :JNB TF0, LOOP ; monitor timer flag 0 until it rolls over
CLR TR0 ; stop timer
CLR TF0 ; clear timer 0 flag
RET
END


Delay calculation for XTAL = 11.0592 MHz is Delay = (FFFF – YYXX + 1) * 1.085 µs where YYXX are TH, TL initial values respectively. Notice that values YYXX are in hex.

Timer delay = (FFFF – FFF2 + 1) * 1.085 µs = 15.19 µs

To get the largest delay we make TL and TH both 0. This will count up from 0000 to FFFFH and then roll over to zero.
Largest delay = (65536 – 0) * 1.085 µs = 71106.56 µs = 71.1065 ms.

For known delay, to find the values needed for the TH, TL registers,
  • Divide the desired time delay by 1.085 µs.
  • Perform 65536 – n, where n is the decimal value we got in Step 1
  • Convert the result of Step 2 to hex, where yyxx is the initial hex value to be loaded into the timer’s registers
  • Set TL = xx and TH = yy.
To get values for 50ms,
50 ms / 1.085 µs = 46082
65536 – 46082 = 19454 = 4BFEH. Therefore, we have TH = 4B and TL = FE

ORG 0H
MAIN : MOV TMOD, #01H ; Timer 0, mode 1
START :CPL P1.5 ; toggle P1.5
MOV R2, #20 ; count for multiple delay
LOOP1 : MOV TL0, #4BH ; load 4B in to TL0
MOV TH0, #0FEH ; load FE in to TH0
ACALL DELAY
DJNZ R2, LOOP1
SJMP START
; delay using timer 0
ORG 100H
DELAY : SETB TR0 ; start timer 0
LOOP2 : JNB TF0, LOOP2 ; monitor timer flag 0 until it rolls over
CLR TR0 ; stop timer
CLR TF0 ; clear timer 0 flag
RET
END


Mode 0:
Mode 0 is exactly like mode 1 except that it is a 13-bit timer instead of 16-bit. The 13-bit counter can hold values between 0000 to 1FFFH in TH – TL. Therefore, when the timer reaches its maximum of 1FFH, it rolls over to 0000, and TF is raised.

Mode 2:
  • It is an 8-bit timer; therefore, it allows only values of 00 to FFH to be loaded into the timer’s register TH.
  • After TH is loaded with the 8-bit value, the 8051 gives a copy of it to TL. Then the timer must be started. This is done by the instruction “SETB TR0” for Timer 0 and “SETB TR1” for Timer 1.
  • After the timer is started, it starts to count up by incrementing the TL register. It counts up until it reaches its limit of FFH. When it rolls over from FFH to 00, it sets high the TF (timer flag). If we are using Timer 0, TFO goes high; if we are using Timer 1, TF1 is raised.
  • When the TL register rolls from FFH to 0 and TF is set to 1, TL is reloaded automatically with the original value kept by the TH register. To repeat the process, we must simply clear TF and let it go without any need by the programmer to reload the original value. This makes mode 2 an auto-reload, in contrast with mode 1 in which the programmer has to reload TH and TL.
In auto-reload, TH is loaded with the initial count and a copy of it is given to TL. This reloading leaves TH unchanged, still holding a copy of the original value.

ORG 0H
MAIN :MOV TMOD, #02H ; Timer 0, mode 2
MOV TH0, #0H; load 00 in to TH0
START :MOV R2, #250 ; count for multiple delay
LOOP :ACALL DELAY ; call delay subroutine for 250 times
DJNZ R2, LOOP
CPL P1.5 ; toggle P1.5
SJMP START
; delay using timer 0
ORG 100H
DELAY : SETB TR0 ; start timer 0
LOOP1 : JNB TF0, LOOP1 ; monitor timer flag 0 until it rolls over
CLR TR0 ; stop timer
CLR TF0 ; clear timer 0 flag
RET
END


Delay calculation for XTAL = 11.0592 MHz is Delay = (FF – XX) * 1.085 µs where XX is TH initial value. Notice that value XX is in hex.

Timer delay = (FF – 0) * 1.085 µs = 277.76 µs



Related topics:
8051 Loop   |   8051 Delay   |   8051 IO Port Programming   |   8051 Counter   |   8051 Serial Port Programming   |   8051 Programming Timer Interrupt   |   8051 Programming Serial Interrupt   |   8051 Programming External Interrupt

List of topics: 8051

No comments:

Post a Comment