AT89C51 Timer/Counter 1
Timer/Counter 1 in the AT89C51 microcontroller operates similarly to Timer/Counter 0. It is a 16-bit register that can be used as a timer to generate a time delay or as a counter to count external events. This register is split into two 8-bit registers: TH1 (Timer 1 high byte) and TL1 (Timer 1 low byte).
Usage as a Timer
When Timer 1 is used as a timer, it increments every machine cycle. Just like with Timer 0, a machine cycle in AT89C51 is 12 oscillator periods, so the time delay generated by the timer is dependent on the oscillator frequency.
Usage as a Counter
When Timer 1 is used as an event counter, it increments on the falling edge (0 level) of the input pulse applied to the T1 pin.
Control Registers
As with Timer 0, Timer 1 is also controlled by the TMOD (Timer Mode) and TCON (Timer Control) registers.
TMOD Register
The TMOD register is used to select the mode of operation for Timer 1. The specific bits related to Timer 1 are:
- GATE1 (bit 7): When this bit is set, Timer 1 will only run if the INT1 pin is high and the TR1 bit is set. If GATE1 is cleared, Timer 1 will run whenever TR1 is set, regardless of the state of INT1.
- C/T1 (bit 6): This bit is used to select between timer and counter mode for Timer 1. When cleared, Timer 1 functions as a timer. When set, Timer 1 functions as an event counter.
- M1.1 and M0.1 (bits 5 and 4): These bits are used to select the mode of Timer 1.
TCON Register
The TCON register also has control bits for Timer 1:
- TF1 (bit 7): This is the overflow flag for Timer 1. When Timer 1 overflows, this bit is set by the microcontroller. It can be cleared by software.
- TR1 (bit 6): This bit is used to start or stop Timer 1. When set, Timer 1 will start incrementing. When cleared, Timer 1 will stop.
Modes of Operation
Just like Timer 0, Timer 1 can be configured to operate in one of four modes (Mode 0 to Mode 3), determined by the M1.1 and M0.1 bits of the TMOD register. These modes were explained in previous responses.
Programming Timer 1 involves similar steps to Timer 0:
1. Select the timer mode in the TMOD register.
2. Load the initial values in TH1 and TL1 registers.
3. Start the timer by setting TR1 in the TCON register.
4. If you are using interrupts, enable them and write the ISR (Interrupt Service Routine).
5. Continuously monitor the TF1 bit in the TCON register to know when the timer has overflowed (if you're not using interrupts).
Utilizing Timer 1 and its diverse modes of operation can provide a range of functions in your projects, such as generating precise time delays, measuring the duration of an event, or counting external events.