Timers actually counts the cycles that are generated by the crystal oscillator(clock) connected to pin 18(XTAL1) & 19(XTAL2) of 89c51 microcontroller. To understand the working of timer’s you should first know about the registers that are associated with timers.
8051 Microcontroller Timer registers
- (Timer Control register)
- TMOD (Timer Mode register)
- TH0/TL0 (Timer 0, 16-bit register, High bits goes to TH0, Low bits goes to TL0)
- TH1/TL1 (Timer 1, 16-bit register, High bits goes to TH1, Low bits goes to TL1)
TH0/TL0(Timer-0) = TH1/TL1(Timer-1)
TCON(Timer control) Register of 8051 Microcontroller
- TR0/TR1: Timer 0/1 (Run control flag)
- TF0/TF1: Timer 0/1 (Timer overflow flag)
Lower Four bits are not shown in the above figure. These bits are
- IT0/IT1: Timer Interrupts. when IT0=1 or IT1=1 It specifies interrupt on falling edge and when IT0=0 or IT1=0 it specifies interrupt on rising edge.
- IE0/IE1: Used for external Interrupts.
TMOD(Timer mode) Register of 8051 Microcontroller
- T1M0-T1M1—-T0M1-T0M0: Selects the mode of the Timer. We can use timer as 13-bit counter or 16-bit counter by setting the above TMOD register bits. Other two modes are 8-bit auto reload and split timer mode. In auto reload mode once the counter reaches maximum value it automatically reloads or start from 1.
- C/T: Selects timer as external counter or Internal timer. If C/T is 1 timer as a counter is selected. External triggers on port#3 pin#4 are counted. If C/T = 0 timer as a time delay generator is selected. It increments on every machine cycle and we can calculate our desired delay by multiplying the number of machine cycles with frequency of clock.
- Gate0-Gate1: When Gate is 1, Timer runs only when INT0-INT1 Pin of 89c51 is high. This bit represents timer interuupts and runs on a condition if interrupt-0 and interrupt-1 are high/occured.
SFR’s(Special Function Registers) of 8051 Timer Registers
89c51 Microcontroller register configuration examples
Timer clock cycle duration for 89c51 microcontroller is given by
6 clock cycles represents one timer clock cycle =
Timer frequency=oscillator frequency/6
Timer frequency = 1 / Timer clock cycle duration
1 / Timer clock cycle duration = oscillator frequency/6
So by rearranging the variables, timer clock cycle comes out to be
Timer Clock Cycle Duration = 6/oscillator frequency
Timer clock cycle duration is the time in which one cycle is completed by the timer. Divide the delay needed by the Timer clock cycle duration. For example I need a delay of 20 ms, Divide 20 ms by Timer Clock Cycle duration. Now negate the result of previous calculation from 65535, which is the maximum count by the timer with out over flow. Now transform the obtained decimal value in hexadecimal, and load the two high bytes in TH and low bytes in TL.
Divide the time reload value by 2 to achieve exact 20 ms delay value. It comes out to be 14336. Convert it in to Hexadecimal Format. Load the upper timer register with High byte and lower timer register with Low byte.
8051 Microcontroller Delay generation Steps
2. Load values for TLx and THx registers.
3. Run the timer by initializing TRx with 1 TR=1.
4. Keep monitoring the timer flag (TFx). Tf it is raised Stop the timer by making TRx=0.
5. Clear the TFx flag for the next count.
Simple Delay function
TMOD = 0x01; // Timer 0 is in use. 16-bit Timer Mode is selected.
TL0 = 0x00; // Load value for TLx register
TH0 = 0x38; // Load value for THx register
TR0 = 1; // Run Timer-0
while(!TF0) // Poll TFx
TR0 = 0; // If TF=1 stop the timer by making TR=0
TF0 = 0; // Make TF=0 for next counting
Some Projects Created using same Delay method shown above
Download PDF Document for 8051(89c51,89c52) Microcontroller Timers Registers
Filed Under: Knowledge Share, Microcontroller Projects