8051 Timers Timers are the peripherals of the 8051 Microcontroller. Timers are peripherals in any of the controllers. This peripheral is mainly used in time based applications. A timer is a specialized type of clock. It can be used to control the sequence of an event or process. The 8051 comes equipped with two timers (Timer 0 and Timer 1). These timers are 16 bit registers.
General Functions of the Timer Keeping time and/or calculating the amount of time between the events. Counting the Events themselves, or Generating baud rate for the serial port. Note: While generating delay this circuitry worked as Timer. While counting the number of events occurred at the particular time this circuitry worked as counter.
Baud Rate A number related to the speed of data transmission in a system. The rate indicates the number of electrical oscillations per second that occurs within a data transmission. The higher the baud rate, the more bits per second that are transferred.
For Timer: (Internal Pulses Working) Crystal Oscillator / 12 Circuitry TIMER “X” 11.0592 MHz f = 921.6 KHz Timer will get clock frequency from the crystal oscillator that is 11.0592 MHz. This frequency is given as input the circuitry that is called /12 (Divide by 12) circuitry. Because one machine cycle is nothing but 12 clock cycles.
For Counter : (External Pulses Working) Timer 0 T1 Timer 1 T0 and T1 is the external input. So counter will get external input from the T0 and T1
SFRs relating to Timers SFR Name Description SFR Address TH0 TIMER 0 Higher byte 8Ch TL0 TIMER 0 Lower byte 8Ah TH1 TIMER 1 Higher byte 8Dh TL1 TIMER 1 Lower byte 8Bh TCON TIMER Control 88h TMOD TIMER Mode 89h
TMOD Register If we program lower nibble, Timer 0 will get affected. Gate C/T M1 M0 Timer 1 Timer 0 If we program lower nibble, Timer 0 will get affected. If we program higher nibble, Timer 1 will get affected.
Gate: 0 – Software Control 1 – H/W Control (Instruction with Interrupt Signal) C / T: 0 – Timer 1- Counter
Mode Bits Information M0 and M1 Description 13 bit Timer 1 16 bit Timer 8 bit Auto Load Split Timer
TCON Register LSB MSB Interrupts Timers TF1 TR1 TF0 TR0 IE1 IT1 IE0
External Interrupt 1 Edge Flag IT1 External Interrupt 1 Type Flag IE0 Bit Names Description TF1 Timer 1 Overflow Flag TR1 Timer Run Control bit TF0 Timer 0 Overflow Flag TR0 Timer 0 Run Control Bit IE1 External Interrupt 1 Edge Flag IT1 External Interrupt 1 Type Flag IE0 External Interrupt 0 Edge Flag IT0 External Interrupt 0 Type Flag
Hardware Configuration of Timers
Timer Mode Explanations Mode 0 : TLX = 5 bit, THX = 8 bit Timer Address : 0000H to 1FFFH TLX THX TFX Interrupt
Timer Address : 0000H to FFFFH Mode 1 : TLX = 8 bit, THX = 8 bit Timer Address : 0000H to FFFFH TLX THX TFX Interrupt
TLx = 8 bits THx = for storing Value, 00H to FFH Mode 2 : Auto Reload (It is 8 – bit Mode), Because we can supposed to use only TLX 8 – bit only. TLx = 8 bits THx = for storing Value, 00H to FFH We have to initialize the TL register with some value. TH register is initialized with auto reload value. Interrupt TLX TFX Reload TLx
Mode 3 : TL0 = Timer 0 and TH0 = Timer 1 Timer 1 = 16 bit Timer Interrupt TL0 TF0 Interrupt TH0 TF1
Time Delay Calculation If we want to generate a time delay of 10 microseconds (or) 1 milliseconds. Step 1: Divide the required time delay by 1.085 Microseconds. Why 1.085 Microseconds? 11.0582MHz / 12 = 9.216KHz So, T = 1/f = 1/9.216KHz = 1.085Microseconds.
Time Delay Calculation Step 2: (Maximum value of Timer + 1)10 – (n)10= (Z)10. Maximum value of timer is depending on the mode of a Timer. In case of Mode 0 : Maximum value is = 1FFF. In case of Mode 1 : Maximum Value is = FFFF. Note : FFFF + 1 = 65536.
Time Delay Calculation Step 3: Convert (Z)10 to (YYXX)10 It will be loaded into TH and TL with YY & XX Respectively. TH = YY. TL = XX.
Example: Generate a time a time delay of 250microsecods. Assume Timer mode is 1. 1) (n)10 = 250microsecods/1.085microseconds = 230microseconds. 2) (z)10 = (FFFF+1) – (n)10 = 65536 – 230 = 65306. 3) Hexadecimal Value of 65306 is FF1AH. YY = FF XX = 1A TH = FFH and TL = 1AH