The timer is an important application in Embedded systems, it maintains the timing of an operation in sync with a system clock or an external clock. The timer has so many applications such as measure time generating delays, they can also be used for generating baud rates.
A normal delay function might be used for loop iterating for a few 1000 cycles. But these types of delays need not be accurate and fundamentally it is not a good programming practice. So, TIMER/COUNTER is a software designed to count the time interval between events. It counts the cycle of the peripheral clock or an externally supplied clock.
Timer in 8051
The AT89S8253 has three timers/counters marked T0, T1 and T2. Timers T0 and T1 completely fall under the 8051 Standard. Their main purpose is to measure time and count external events. Besides, they are used for generating clock pulses that can be used in serial communication, so-called Baud Rate.
The microcontroller can also generate/measure the required time delays by running loops, but the timer/counter relieves the CPU from that redundant and repetitive task, allowing it to allocate maximum processing time for other tasks.
Below table provides the details of the 8051 Timers.
The timer is nothing but a simple binary counter that can be configured to count clock pulses(Internal/External). Once it reaches the Max value, it will reset to zero setting up an Overflow flag and generates the interrupt if enabled.
Timer T0 & T1
Timers T0 and T1 completely fall under the 8051 Standard. They are 16 bit wide as shown. This can be accessed as two 8-bit registers THx and TLx, representing a low and a high byte of 16-bit register.
Formula used to calculate values in these two registers is very simple:
Fosc = 16Mhz
Delay = 1ms
Timer count can be determined as below:
Tick = (1/(Fosc/12)
Tick = 12/Fosc
For Fosc = 16Mhz. The tick time will be Tick = 12/16M = 0.75us
Now the Timer value for the required delay can be calculated as below.
Delay = TimerCount *Tick
Count = (Delay/Tick) = (1ms/0.75us) = 1333
Timer Register = Max Timer count – Count
Timer Register = 2^16 – 1333 = 65536 -1333 = 64203 = 0xFACB
Since the timer T0 is virtually 16-bit register, the largest value it can store is 65535. In case of exceeding this value, the timer will be automatically cleared and counting starts from 0. This condition is called an overflow. Two registers TMOD and TCON are closely connected to this timer and control its operation.
Timer 2 is a 16-bit timer/counter installed only in new versions of the 8051 families. Unlike timers T0 and T1, this timer consists of 4 registers. Two of them, TH2 and TL2, are connected serially in order to form a larger 16-bit timer register. Like timers 0 and 1, it can operate either as a timer or as an event counter. Another two registers, RCAP2H and RCAP2L, are also serially connected and operate as capture registers. They are used to temporarily store the contents of the counter register. The main advantage of this timer compared to timers 0 and 1 is that all read and swap operations are easily performed using one instruction. Similar to T0 and T1, it has four different modes of operation,
- Timer T2 in Capture mode
- Timer T2 in auto-reload mode
- Timer T2 as a baud rate generator
- Timer T2 as a clock generator
Register configuration for the Timer
TCON register is also one of the registers whose bits are directly in control of timer operation. Only 4 bits of this register are used for this purpose, while rest of them is used for interrupt control.
- TF1 bit is automatically set when the Timer 1 overflow.
- TR1 bit enables the Timer 1.
- 1 – Timer 1 is enabled.
- 0 – Timer 1 is disabled.
- TF0 bit is automatically set when the Timer 0 overflow.
- TR0 bit enables the timer 0.
- 1 – Timer 0 is enabled.
- 0 – Timer 0 is disabled.
This register contains bits controlling the operation of timer 0 & 1. To select the operating mode and the timer/counter operation of the timers we use TMOD register. Timer 0 and timer 1 are two timer registers in 8051. Both of these registers use the same register called TMOD to set various timer operation modes.
- TMOD is an 8-bit register.
- The lower 4 bits are for Timer 0.
- The upper 4 bits are for Timer 1.
- In each case,
- The lower 2 bits are used to set the timer mode.
- The upper 2 bits to specify the operation.
Bits of this register have the following function:
- GATE enables and disables Timer by means of a signal brought to the INTx pin:
- 1 – Timer operates only if the INTx bit is set.
- 0 – Timer operates regardless of the logic state of the INTx bit.
- C/T selects pulses to be counted up by the timer/counter:
- 1 – Timer counts pulses brought to the Tx(Timer) pin.
- 0 – Timer counts pulses from the internal oscillator.
- M1, M0 These two bits select the operational mode Timer.
TIMER MODE 0 (13 bit mode)
MODE 0 is a 13 bit mode. In this mode the THx acts as an 8 bit timer & TLx acts as a 5 bit timer. The TLx counts up to 31 & then resets to 00 & increment THx by 1. Suppose you load 0 in the timer then the timer will overflow in 2^13 i.e. 8192 machine cycles.
TIMER MODE 1 (16-bit mode)
MODE 1 is similar to MODE 0 except it is a 16-bit mode. In this mode, the THx & TLx both acts as an 8-bit timer. The TLx counts up to 255 & then resets to 00 & increment THx by 1. Since this is a full 16-bit timer we can get a maximum of 2^16 i.e. 65536 Machine cycle before the timer overflows.
TIMER MODE 2 (8-bit mode)
In this Mode TLx acts as the timer & THx contains the Reload Value i.e. THx is loaded in TLx every time it overflows i.e. when TLx reaches 255 & is incremented then instead of resetting it to 0 it will be reset to the value stored in THx. This mode is very commonly used for generating baud rate used in serial communication.
TIMER MODE 3 (Split Mode)
Timer mode “3” is known as a split-timer mode. Timers 0 and 1 may be programmed to be in mode 0, 1, or 2 independently of a similar mode for the other timer. But in mode 3 the timers do not operate independently if mode 3 is chosen for timer 0. When Timer 0 is placed in mode 3, it essentially becomes two separate 8-bit timers. Timer 0 is TL0 and Timer 1 is TH0. Both timers count from 0 to 255 and overflow back to 0. All the bits that are related to Timer 1 will now be tied to TH0. Now placing timer 1 in mode 3 causes it to stop counting, the control bit TR1 and the Timer 1 flag TF1 are now used by timer 0. So even if you use Timer 1 in Mode 0, 1 or 2 you won’t be able to START or STOP the timer & no INTERRUPT will be generated by Timer 1. The real Timer 1 will be incremented every machine cycle no matter what.
- TMOD = 00000001, mode 1 of timer 0 is selected.
- TMOD = 00100000, mode 2 of timer 1 is selected.
- TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are selected.
T2CON (Timer/Counter 2 Control Register)
This register contains bits controlling the operation of timer 2. TF2 bit is automatically set on timer 2 overflow. In order to detect the next overflow, this bit must be cleared from within the program. If bits RCLK and TCLK are set, overflow has no effect on the TF2 bit. EXF2 bit is automatically set when a capture or a reload is caused by a negative transition on the T2EX pin. It generates an interrupt (if enabled), unless the DCEN bit of the T2CON register is set. The EXF2 bit must be cleared from within the program. RCLK is receive clock bit which determines which timer is to be used as receive clock for serial port:
- 1 – T2 is used as receive clock for serial port.
- 0 – T1 is used as receive clock for serial port.
TCLK is transmit clock bit which determines which timer is to be used as transmit clock for the serial port:
- 1 – T2 is used as transmit clock for the serial port.
- 0 – T1 is used as transmit clock for the serial port.
EXEN2 is timer 2 external enable bit used to include the T2EX pin in timer 2 operation:
- 1 – Signal on the T2EX pin affects timer 2 operation.
- 0 – Signal on the T2EX pin is ignored.
TR2 is timer 2 run control bit used to enable/disable timer 2:
- 1 – Timer 2 enabled.
- 0 – Timer 2 disabled.
C/T2 is timer/counter 2 select bit used to select pulses to be counted by counter/timer 2:
- 1 – 16-bit register (T2H and T2L) counts pulses on the C/T2 pin (counter).
- 0 – 16-bit register (T2H and T2L) counts pulses from the oscillator (timer).
CP/RL2 is timer 2 capture/reload bit used to define transfer direction:
- 1 – If EXEN=1, pulse on the T2EX pin will cause a number to be transferred from counter to capture register.
- 0 – Under the same condition, the signal on the T2EX pin will cause a number to be transferred from capture to counter register.
- Register configuration for 1ms delay generation
TMOD = 0x01; // Set timer0 in mode 1
TL0 = 0xCB; // Timer value for 1us delay
TH0 = 0xFA;
TR0 = 1; // Enable Timer 0
while(!TF0); // Wait till timer count overflow
TF0 = 0; // Reset the overflow flag
TR0 = 0; // Disable Timer 0
Complete Firmware example for 8051 Timer: Square wave generation
In the below example, a timer0 is configured to run to generate a square wave when initialized. This is a perfect example of how to generate delay using 8051 Timer port.