9 Programming Timers

 

In this lecture, different timers and its modes will be discussed. Importance of Timer control registers for configuring timers and its structures will be discussed. Different delay routines using timers and different timer modes will be created and shown using assembly code.

 

1. Timers

 

In 8051 microcontroller, there are two 16-bit timers, namely, Timer 0 and Timer 1. These timers can be configured to work as counter, and also independently configured to operate in a variety of modes as a timer or as an event counter. Timers & Counters in 8051 controllers are used to create Interval timing for a product, Periodic event timing, Timer base for measurements and also to generate Baud Rate for serial communication. Counters are used for event Counting using timers in 16-bit mode/ 8 bit mode. When operating as a timer, the timer/counter runs for a programmed length of time, then issues an interrupt request. When operating as a counter, the timer/counter counts negative transitions on an external pin. After a preset number of counts, the counter issues an interrupt request.

 

1.1 Operation

 

Timer consists of registers THx and TLx (x= 0, 1) connected in cascade to form a 16-bit timer i.e register TH0 and TL0 are combined together to form Timer 0 and register TH1 and TL1 are combined together to form Timer1. For controlling the timer, the run control bit (TRx) in TCON register is used which turns the timer on by allowing the selected input to increment TLx. TR0 is used to turn on Timer 0 and TR1 is used to turn on Timer 1.

 

When TLx overflows it increments THx, it means that TL is operating as a lower byte and TH is operating as a higher byte. When THx overflows it sets the timer overflow flag (TFx) in TCON register. Setting the TRx register, does not clear the THx and TLx timer registers. Timer registers can be accessed to obtain the current count or to enter preset values. They can be read at any time but TRx bit must be cleared.

 

The C/T control bit (in TCON register) is used to select whether the operation is timer or counter. If C/T bit is 1, then the operation is a counter, and if the bit is 0, then it indicates Timer operation. For timer operation, the timer register counts the divided-down peripheral clock : Frequency Of Oscillator / 12 in standard mode. For counter operation, the timer register counts the negative transitions on the Tx external input pin. Timer 0 and Timer 1 have four operating modes. Modes are selected by bit-pairs (M1, M0) in TMOD. Modes 0, 1, and 2 are the same for both timer/counters. Mode 3 is different.

 

1.3 Timer’s clock frequency and its period

 

In 8051-based system, the crystal oscillator has a frequency of 11.0592 MHz when C/T bit of TMOD is 0. Each machine cycle is made up of 12 clock cycles.

 

 

Hence for a single machine cycle, the frequency becomes 1/12 × 11.0529 MHz = 921.6 KHz. Since for a single second 921.6 × 103 work has been done. For a single machine cycle, the time taken is T = 1/921.6 KHz = 1.085 us, so the oscillator takes 1.085us for completing a single machine cycle.

 

1.4 Timer registers

 

TMOD, TCON, THx and TLx are the Timer registers. TMOD(Timer Mode) is used to select the mode of operation. TMOD is a 8-bit register. TCON(Timer control) is used to control the timer to run or stop. THx-TLx registers will hold the count to generate desired delay.

 

 

Figure 1.2 shows the Timer mode register . It contains 8 bits, where 0-3 bits corresponds to Timer 0 and 4-7 bits corresponds to timer 1. If the lower nibble is operated, then it corresponds to Timer 0; and if the upper nibble is operated, then it corresponds to Timer 1. If (M10,M00) bit pair is 00, then the timer is operated in mode 0, if the bit is 01, it indicates mode 1, if the bit is 10, then it is mode 2 and if the bit is 11 then it is mode 3. Mostly microcontroller is operated in mode1 and mode 2. The second and third bits are used for controlling the timer. If C/T0# bit is 1, then Timer 0 is operated as counter, and if 0, as a Timer. Similarly, the fourth and fifth bit pairs are used for mode selection of Timer 1. The sixth and seventh bits are used for controlling Timer 1. In short, the lower 4 bits are used for Timer 0, upper 4 bits are for Timer 1 and also lower 2 bits are used to set the timer mode, upper 2 bits to specify the operation.

 

TCON register is also 8-bits wide. The bits IT0 and IE0 corresponds to Timer 0, IT1 and IE1 corresponds to Timer 1. IT is the Interrupt type and IE is the Interrupt Edge flag. To start the Timer, set the bit TR0=1 and to stop the timer 0, clear the bit. Similarly, TR1 bit is used for starting and stopping the Timer 1. TF0 and TF1 are used for indicating the overflow in Timer 0 and Timer 1 respectively.

 

It is a 16-bit timer. Therefore, it allows values of 0000 to FFFFH to be loaded into the timer’s register TL and TH. After TH and TL are loaded with a 16-bit initial value, the timer must be started. This is done by SETB TR0 for timer 0 and SETB TR1 for timer 1. After the timer is started, it starts to count up. It counts up until it reaches its limit of FFFFH. After the timer reaches its limit and rolls over, in order to repeat the process , TH and TL must be reloaded with the original value, and TF must be reloaded to 0.

 

1.4.1 Mode 0

 

THx+TLx =13 bits (8+5) are there for Timer/counter operation. If C/Tx is 1, then the negative transition TX input is passed through the TH and TL register, and it is operated as a timer. If C/Tx is equal to 0, then clock input will be given to the registers, i.e., the clock is divided by 6 and then the operation is that of a counter. The overflow is indicated by the TFx bit in TCON register, which in turn generates the interrupt request. Mode 0 operation is similar to mode 1 and the only difference is that the number of bits in the THx and TLx register put together is 16 bits in mode1.

 

1.4.2 Mode 1

1.4.2 Mode 2

 

In mode 2, if the operation is a timer, then input is through mux. if TLx is incremented, then overflow occurs which is indicated by TCON register and the Interrupt request is generated. In this mode 2, when overflow occurs, the 8-bit values in THx will be loaded to TLx. Using these timers we can generate delay with following steps .

 

2. Steps for generating a delay

 

1.  Load the TMOD  with mode and timer 0 or 1

2.  Load registers TL and TH with count corresponding to delay

3.  Start the timer

4.   Continuously check the timer flag (TF) with the <JNB TFx, target> instruction to see if it is raised , if it is raised(TF=1) then get out of the loop.

5.  Stop the timer

6.  Clear the TF flag for the next round

7.  Go back to Step 2 to load TH and TL again

 

To generate a delay, the timer can be used in either mode 0 or mode1, but values to be loaded into TLx and THx registers to generate a specific delay time, are to be calculated.

 

2.1. Steps to calculate count to load into THx-TLx to generate desired delay

 

(assume XTAL = 11.0592 MHz)

Steps for finding the TH, TL registers values

1.  Divide the desired time delay by 1.085 us

2.  Perform 65536 – n, where n is the decimal value we got in Step 1

3.  Convert the result of Step 2 to hex, say we get yyxx. yyxx is the initial hex value to be loaded into the timer’s register.

4.  Set TL = xx and TH = yy

 

2.1.1 Generate Delay =10ms with Clock frequency= 11.0592 MHz, use Timer0,mode1

 

Given:

Time delay=10ms

clock frequency=11.0592 MHz

 

Step 1: Divide the desired time delay by 1.085 us

Count =10 ms/ 1.085 us=9216

 

Step 2: Perform 65536 – n 65536-9216=56320= DC00H

 

Step 3: Set TL = xx and TH = yy

 

Here xx=DC and yy=00,Hence,TH0=DC and TL0=00.

 

Following is the assembly code program to generate a delay of 10ms

 

MOV TMOD,#01 ;Timer 0, mode 1, 16-bitmode

 

HERE: MOV TL0,#00 ;TL0=0, the low byte

 

MOV TH0,#0DCH ;TH0=DC, the high byte

 

SETB TR0 ;Start timer 0

 

AGAIN: JNB TF0,AGAIN ;Monitor timer flag 0

 

CLR TR0 ;Stop the timer 0

 

CLR TF0 ;Clear timer 0 flag

 

The first statement in the code indicates the Timer 0 mode 1 operation(the LSB indicates Timer 0 and MSB indicates the mode). From the above calculation, the value 00 has to be loaded to the Timer TL0 and DC is loaded to TH0. Once the values are loaded, the timer 0 is started using the SETB command. The control instruction JNB TF0 is used for checking the overflow. The overflow is indicated by TF0 flag. This statement will jump to the same loop AGAIN until the overflow occurs. Thus a delay is generated for 10ms. Then if time exceeds 10ms, TR0 and TF0 flags are cleared since the system cannot do that on its own.

 

We can generate a pulse generator with the use of these timers. Following is the program to generate 5Khz pulse/clock generator.

 

2.1.2 Program to generate a square wave of 5 kHz frequency on pin P1.0, clock frequency =11.0592 MHz

 

Given:

 

Square wave frequency=5 kHz

clock frequency=11.0592 MHz

 

Step 1: Calculate the Time delay

 

T=1/f=1/5 kHz =0.2 ms

T=0.2 ms which is the period of square wave

 

T/2 =0.2/2=0.1 ms delay for high and low

 

Step 2: Divide the desired time delay by 1.085 us

Count=0.1ms/1.085 us = 921

 

Step 3: Perform 65536 – n

 

TH0-TL0= 65536-921=64615=FC67H

 

Step 4: Set TL = xx and TH = yy

 

Here xx=FC and yy=67. Hence,TH0=FCh,TL0=67h

   MOV TMOD,#01 ;Timer 0, mode 1, 16-bitmode

AGAIN: MOV TL1,#67H ;TL1=67, low byte of timer

MOV TH1,#0FCH ;TH1=FC, the high byte

SETB TR1 ;Start timer 1

 

BACK:

JNB TF1,BACK ;until timer rolls over

CPL P1.0 ; compliment P1.0

CLR TR1 ;Stop the timer 1

CLR TF1 ;Clear tim er 1 flag

SJMP AGAIN ;Reload timer

 

The first statement is used to set Timer 0 mode1 operation. Then the preset values are given to the respective TL1 and TH1 register. Once the values are loaded into the register, the Timer 1 is started. The control instruction JNB is used for checking the overflow. Then the port P1.0 is complemented for generating the square wave. Then the flags TR1 and TF1 flags are cleared.

 

2.2 Steps for generating delay in MODE 2

 

1.  Select timer in TMOD register indicating which timer (timer 0 or timer 1) is to be used, and the timer mode (mode 2) to be selected

 

2. TH register loaded with initial count value

 

3. Start timer

 

4. Continuously monitoring the timer flag (TF) with the JNB TFx,target instruction to see whether TFx is ‘1’ (high) . Get out of the loop when TF goes high

 

5. Clear the TF flag

 

6. Go back to Step4, since mode 2 is auto-reload

 

2.2.1 Creating delay for 25 cycles using mode2 timer1

 

DELAY:

MOV TMOD,#20h

MOV TH1,#-25

SETB TR1

LOOP: JNB TF1, LOOP

CLR TF1

 

Here, irrespective of the clock frequency, a delay of 25 cycles is generated. Initially timer 1 mode 2 operation is set, and then TH1 flag is set to -25. Then the timer TR1 is started. Then the control instruction is used for creating a delay of 25 cycles and the flag TF1 is cleared.

 

We can make toggling/blinking of LEDs using timers with standard delay.

 

2.2.2 Toggle LED connected at P1.0 with 5 microsec delay using timer1 and mode 2

 

MOV TMOD,#20 ;Timer 1 mode 2, 8-bit auto reload

AGAIN: MOV TL1,#-5 ;TL1=256-5, low byte of timer

MOV TH1,# -5 ;TH1=256-5, the high byte

SETB TR1 ;Start timer 1

BACK: JNB TF1,BACK ;until timer rolls over

CPL P1.0 ; compliment P1.0 toggle LED

CLR TF1 ;Stop the timer 1

SJMP BACK ; loop

 

This assembly program demonstrates the blinking of LED connected at P1.0 with 5 micosecond delay. As TH1 is loaded with “-5” it generates 5 machine cycle time delay. As one cycle takes 1 micro second for 12 MHz oscillator, this delay generates a 5 micro sec delay. So LED blinking with 5 micro sec delay is generated with above code.

 

3. Summary

 

In this lecture different timers and its modes are discussed. The importance of Timer control registers and its structures are also discussed and various steps to create different delay routines using different timers, and also in different modes are seen. Sample code for generating these delays are also demonstrated.

 

4. References

  1. The 8051 Microcontroller and Embedded Systems Using Assembly and C Second Edition Muhammad Ali Mazidi, Janice Gillispie Mazidi and Rolin D. McKinlay.

 

Supporting & Reference Materials

  1. The 8051 Microcontroller and Embedded Systems Using Assembly and C Second Edition Muhammad Ali Mazidi, Janice Gillispie Mazidi and Rolin D. McKinlay.
  2. http://what-when-how.com/8051-microcontroller/programming-8051-timers/