One of the standout features of Timer/Counter 0 is its ability to generate interrupts. When it overflows, it can trigger an interrupt, allowing the microcontroller to respond immediately, making it invaluable for real-time applications.
Beyond these functionalities, Timer/Counter 0's integration into the AT89C51 ensures efficient power management, allowing it to run or halt based on the system's requirements. Whether it's used in precision timing control, event counting, or generating PWM signals, Timer/Counter 0 in the AT89C51 remains a cornerstone in the realm of embedded systems, offering developers a blend of flexibility and precision.
Belonging to the 8051 family, the AT89C51 is a low-power, high-performance microcontroller. With its in-built ROM, RAM, timers, serial ports, and versatile I/O ports, it serves as the brain for numerous embedded system applications. But today, we're focusing on one specific aspect: Timer/Counter 0.
At its core, Timer/Counter 0 can operate in two primary modes: as a timer to generate delays or as a counter to count external events. Think of it as a stopwatch. In 'timer mode,' it measures the passage of time. In 'counter mode,' it keeps track of the number of times an external event occurs.
Diving into Timer Modes
The AT89C51 offers multiple timer modes -
Mode 0
An 8-bit timer. Envision it as a short race where the finish line comes up quickly.
Mode 1
A 16-bit timer. This is akin to a marathon, providing a more extended range for timing operations.
Mode 2
An 8-bit auto-reload timer. Imagine a looped track where, after each lap, the race restarts automatically.
Mode 3
This mode splits Timer/Counter0 into two separate 8-bit timers/counters. Think of it as having two stopwatches instead of one.
Counter Modes: Counting External Events
When used as a counter, Timer/Counter0 counts the number of external pulses fed into it. It's like a turnstile at a stadium, counting the number of people entering the venue.
Control Registers
There are two important registers to control Timer 0, the TMOD (Timer Mode) and TCON (Timer Control) registers.
TMOD (Timer Mode Control Register)
The TMOD register is an 8-bit register used for selecting the operation mode of Timer 0.
Bit Number |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
Symbol |
GATE1 |
C/T1# |
M11 |
M10 |
GATE0 |
C/T0# |
M01 |
M00 |
Description |
External input for Timer 1 |
Timer or Counter Selector for Timer 1 |
Timer 1 Mode Select bit 1 |
Timer 1 Mode Select bit 0 |
External input for Timer 0 |
Timer or Counter Selector for Timer 0 |
Timer 0 Mode Select bit 1 |
Timer 0 Mode Select bit 0 |
TCON (Timer Control Register)
The TCON register is another 8-bit register used to control Timer 0.
Bit Number |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
Symbol |
TF1 |
TR1 |
TF0 |
TR0 |
IE1 |
IT1 |
IE0 |
IT0 |
Description |
Timer 1 Overflow Flag |
Timer 1 Run Control Bit |
Timer 0 Overflow Flag |
Timer 0 Run Control Bit |
External Interrupt 1 Edge Flag |
External Interrupt 1 Type Control Bit |
External Interrupt 0 Edge Flag |
External Interrupt 0 Type Control Bit |
TH0 (Timer 0 High Byte Register)
The TMOD register is an 8-bit register used for holding the high counting value of the timer.
Bit Number |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
Symbol |
Bit 7 |
Bit 6 |
Bit 5 |
Bit 4 |
Bit 3 |
Bit 2 |
Bit 1 |
Bit 0 |
Description |
- |
- |
- |
- |
- |
- |
- |
- |
TL0 (Timer 0 Low Byte Register)
The TMOD register is another 8-bit register used for holding the lower 8 bits of counting value of the timer.
Bit Number |
7 |
6 |
5 |
4 |
3 |
2 |
1 |
0 |
Symbol |
Bit 7 |
Bit 6 |
Bit 5 |
Bit 4 |
Bit 3 |
Bit 2 |
Bit 1 |
Bit 0 |
Description |
- |
- |
- |
- |
- |
- |
- |
- |
Configuration Steps
a. Set the Timer Mode
You need to decide whether you want Timer 0 to work in Timer mode or Counter mode. This is done by setting or clearing the C/T0 bit of the TMOD register.
b. Choose the Operating Mode
Timer 0 can be configured in one of the four modes
Mode 0
13-bit Timer mode
Mode 1
16-bit Timer mode
Mode 2
8-bit auto-reload mode
Mode 3
Split Timer mode
This is set using the M1 and M0 bits of the TMOD register.
c. Loading the Timer Registers -
If you're not using the auto-reload mode, load the TH0 and TL0 registers with the required initial value.
d. Start/Stop the Timer
To start the timer, set the TR0 bit of the TCON register. To stop it, clear the TR0 bit.
Flags and Interrupts
The TF0 bit in the TCON register will be set when Timer 0 overflows. This can be used to generate an interrupt if interrupts are enabled.
IE Register
EA |
- |
- |
ES |
ET1 |
EX1 |
ET0 |
EX0 |
Symbol |
Position |
Function |
EA |
IE.7 |
Disables all interrupts. If EA=0, no interrupt is acknowledged. If EA = 1, each interrupt source is individually enabled or disabled by setting or clearing its enable bit. |
– |
IE.6 |
Reserved |
– |
IE.5 |
Reserved |
ES |
IE.4 |
Serial Port interrupt enable bit |
ET1 |
IE.3 |
Timer 1 interrupt enable bit |
EX1 |
IE.2 |
External interrupt 1 enable bit |
ET0 |
IE.1 |
Timer 0 interrupt enable bit |
EX0 |
IE.0 |
External interrupt 0 enable bit |
User software should never write 1s to reserved bits, because they may be used in future AT89 products.