18 Programming Timer in Embedded C

 

In this lecture timer registers in C will be discussed.Details of counters in 8051 and delay length calculation will be discussed. Timer and counter programming in Embedded C is explained with example.

 

1. Timers

 

8051 has two timers. They are Timer 0 and Timer 1. It is independently configured to operate in a variety of modes as a timer or as an event counter.When operating as a timer, the timer/counter runs for a programmed length of time, then issues an interrupt request. But 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 Timers Registers

 

Some of the basic timer registers are TMOD, TCON, THx and TLx.TMOD is used to select the mode of operation.TCON is used to control the timer to run or stop.The details of TCON register is shown below.

 

 

THx and TLx registers are used to hold the count to generate desired time delays.TMOD is an 8-bit register in which lower 4 bits are for Timer 0 and upper 4 bits are for Timer 1. Lower 2 bits are used to set the timer mode while upper 2 bits is used to specify the operation.

 

1.2 Timers Operation

 

Timer 0 and Timer 1 are 16 bit wide. It consists of registers THx and TLx (x= 0, 1) connected in cascade to form a 16-bit timer. It can be accessed as low byte and high byte. Lower byte register is referred to as TLx, while higher byte register is referred to as THx. The timer/ counter is enabled only while the INTx is high and TRx control pin is set.Setting the run control bit (TRx) in TCON register turns the timer on by allowing the selected input to increment TLx. When TLx overflows it increments THx. But when THx overflows it sets the timer overflow flag (TFx) in TCON register. Setting the TRx does not clear the THx and TLx timer registers.BothTimer 0 and Timer 1 have four operating modes. These modes are selected by bit-pairs (M1, M0) in TMOD register. Modes 0, 1,and 2 are the same for both timers/counters. The other basics of timer and counter is explained in 8051 Timer programming in Assembly Language in the previous chapter.

 

1.3 Steps to generate Time Delay

 

Following are the steps to generate the time delay.

 

1.    Load the TMOD value register with mode and timer0 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.

 

2.  Embedded C program for 8051 Timer 0 and Timer 1

 

The general purpose registers of 8051, such as R0-R7, A and B, are under the control of C compiler.They are not accessed directly by C statements.In case of SFRs, the entire RAM space of 80 – FFH is accessible directly using 8051 C statements.We can access the time registers TH, TL, and TMOD directly using the reg51.h header file.

 

2.1 Access timer registers in C

 

Example 1 explained below gives the details about how to access 8051 timer registers like TH, TL, and TMOD in C and it also shows how to access TR and TF bits.

 

 

Example 1

 

Write an 8051 C program to toggle all the bits of port P1 continuously with some delay in between. Use Timer 0, 16-bit mode to generate the delay.

Solution:

 

#include <reg51.h>

 

void T0Delay(void);

 

void main(void){

 

while (1) {

 

P1=0x55;

 

T0Delay();

 

P1=0xAA;

 

T0Delay();

 

}  }

 

void T0Delay(){

 

TMOD=0x01;

 

TL0=0x00;

 

TH0=0x35;

 

TR0=1;

 

while (TF0==0);

 

TR0=0;

 

TF0=0;

 

}

 

Calculating delay:

 

FFFFH – 3500H = CAFFH

 

= 51967 + 1 = 51968

 

=51968 × 1.085 μs = 56.384 ms is the approximate delay.

 

2.1 Calculating delay using timer in C

 

As mentioned in the previous chapter the delay length is depend on three factors. They are Crystal frequency, the number of clocks per machine cycle and the C compiler.To speed up the 8051, many recent versions of the 8051 have reduced the number of clocks per machine cycle from 12 to four, or even one. The frequency for the timer is always 1/12th the frequency of the crystal attached to the 8051, regardless of the 8051 version. We can also use crystal frequency as the clock source. The following are the steps to find TH, TL register values. We can assume XTAL = 11.0592 MHz for 8051 systems.

 

Steps for finding the TH, TL register values

 

1.  Divide the desired time delay by 1.085 μs.

 

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

 

3.   Convert the result of Step2 to hex, where yyxx is the initial hex value to be loaded

 

into the timer’s register.

 

4. Set TL = xx and TH = yy.

 

Times 0/1 Delay Using Mode 1 (16-bit Non-Auto-reload)

 

Example 2 and 3 shows 8051 C programming of the timer 0 and timer 1 in mode 1 in a 16-bit non-auto reload mode.

 

Example 2

 

Write an 8051 C program to toggle only bit P1.5 continuously every 50 ms. Use Timer 0, mode 1 (16-bit) to create the delay. Test the program on the (a) AT89C51 and (b) DS89C420.

 

Solution:

 

#include <reg51.h>

 

void T0M1Delay(void);

 

sbit mybit=P1^5;

 

void main(void){

 

while (1) {

 

mybit=~mybit;

 

T0M1Delay();

 

}}

 

void T0M1Delay(void){

 

TMOD=0x01;

 

TL0=0xFD;

 

TH0=0x4B;

 

TR0=1;

 

while (TF0==0);

 

TR0=0;

 

TF0=0;

 

}

 

FFFFH – 4BFDH = B402H

 

= 46082 + 1 = 46083

 

Timer delay= 46083 × 1.085 μs = 50 ms

 

Example 3

 

Write an 8051 C program to toggle all bits of P2 continuously every 500 ms.Use Timer 1, mode 1 to create the delay.

 

Solution:

 

//tested for DS89C420, XTAL = 11.0592 MHz

 

#include <reg51.h>

 

void T1M1Delay(void);

 

void main(void){

 

unsigned char x;

 

P2=0x55;

 

while (1) {

 

P2=~P2;

 

for (x=0;x<20;x++)

 

T1M1Delay();

 

} }

 

void T1M1Delay(void){

 

TMOD=0x10;

 

TL1=0xFE;

 

TH1=0xA5;

 

TR1=1;

 

while (TF1==0);

 

TR1=0;

 

TF1=0;}

 

Delay: A5FEH = 42494 in decimal

 

65536 – 42494 = 23042

 

23042 × 1.085 μs = 25 ms and 20 × 25 ms = 500 ms.

 

Example 4 and 5 provides details of 8051 C programming of timers 0 and 1 in mode 2 (8-bit auto-reload) mode. Example 6 gives details of 8051 C program to monitor SW and create the given frequencies on pin P1.7.

 

Example 4

 

Write an 8051 C program to toggle only pin P1.5 continuously every 250 ms. Use Timer 0, mode 2 (8-bit auto-reload) to create the delay.

 

Solution:

 

#include <reg51.h>

 

void T0M2Delay(void);

 

sbit mybit=P1^5;

 

void main(void){

 

unsigned char x,y;

 

while (1) {

 

mybit=~mybit;

 

for (x=0;x<250;x++)

 

for (y=0;y<36;y++) //we put 36, not 40

 

T0M2Delay();

 

}  }

 

void T0M2Delay(void){

 

TMOD=0x02;

 

TH0=-23;

 

TR0=1;

 

while (TF0==0);

 

TR0=0;

 

TF0=0;}

 

Time Delay:  256 – 23 = 233

 

23 × 1.085 μs = 25 μs and

 

25 μs × 250 × 40 = 250 ms

 

Due to overhead of the for loop in C, we put 36 instead of 40.

 

 

Example 5

 

Write an 8051 C program to create a frequency of 2500 Hz on pin P2.7. Use Timer 1, mode 2 to create delay.

 

Solution:

 

#include <reg51.h>

 

void T1M2Delay(void);

 

sbit mybit=P2^7;

 

void main(void){

 

unsigned char x;

 

while (1) {

 

mybit=~mybit;

 

T1M2Delay();

 

}}

 

void T1M2Delay(void){

 

TMOD=0x20;

 

TH1=-184;

 

TR1=1;

 

while (TF1==0);

 

TR1=0;

 

TF1=0;}

 

Time Delay:1/2500 Hz = 400 μs

 

400 μs /2 = 200 μs

 

200 μs / 1.085 μs = 184

 

 

Example 6

 

A switch is connected to pin P1.2. Write an 8051 C program to monitor SW and create the following frequencies on pin P1.7:

 

SW=0: 500Hz

 

SW=1: 750Hz, use Timer 0, mode 1 for both of them.

 

Solution:

 

#include <reg51.h>

 

sbit mybit=P1^5;

 

sbit SW=P1^7;

 

void T0M1Delay(unsigned char);

 

void main(void){

 

SW=1;

 

while (1) {

 

mybit=~mybit;

 

if (SW==0)

 

T0M1Delay(0);

 

else

 

T0M1Delay(1);

 

}}

 

void T0M1Delay(unsigned char c){

 

TMOD=0x01;

 

if (c==0) {

 

TL0=0x67;

 

TH0=0xFC;}

 

else {

 

TL0=0x9A;

 

TH0=0xFD;

 

}

 

TR0=1;

 

while (TF0==0);

 

TR0=0;

 

TF0=0;}

 

Frequency:FC67H = 64615

 

65536 – 64615 = 921

 

921 × 1.085 μs = 999.285 μs

 

1 / (999.285 μs × 2) = 500 Hz.

 

2.2 C programming of timers 0 and 1 as counters

 

Timers can also be used as counters counting events happening outside the 8051.When it is used as a counter, it is a pulse outside of the 8051 that increments TH, TL registers. TMOD and TH, TL registers are the sameas for the timer discussed previously. Programming the timer in the lastsection also applies to programming it as a counter, except the source of the frequency. The C/T bit in the TMOD registers decides the source of the clock for the timer.When C/T = 1, the timer is used as acounter and gets its pulses from outsidethe 8051, the counter counts up as pulses are fed frompins 14 and 15, these pins are called T0 (timer0 input) and T1 (timer 1 input). The following figure shows the port 3 used for Timers 0 and 1 function.

 

 

TCON register is a bit addressable register. The following is the function of TCON register for counters. The equivalent instruction for the TCON register is shown below.

 

 

Example 7 shown below shows the 8051 C programming for counter 1 in mode 2 in an 8-bit auto reload mode to count up and display TL1 state in pin P1. The count starts at 0H.

 

Example 7

 

Assume that a 1-Hz external clock is being fed into pin T1 (P3.5). Write a C program for counter 1 in mode 2 (8-bit auto reload) to count up and display the state of the TL1 count on P1. Start the count at 0H.

 

Solution:

 

#include <reg51.h>

 

sbit T1=P3^5;

 

void main(void){

T1=1;

 

TMOD=0x60;

 

TH1=0;

 

while (1) {

 

do {

 

TR1=1;

 

P1=TL1;

 

}

 

while (TF1==0);

 

TR1=0;

 

TF1=0;

 

}}

 

P1 is connected to 8 LEDs. T1 (P3.5) is connected to a 1-Hz external clock.

 

Example 8

 

Assume that a 1-Hz external clock is being fed into pin T0 (P3.4). Write a C program for counter 0 in mode 1 (16-bit) to count the pulses and display the state of the TH0 and TL0 registers on P2 and P1, respectively.

 

Solution:

 

#include <reg51.h>

 

void main(void){

 

T0=1;

 

TMOD=0x05;

 

TL0=0

 

TH0=0;

 

while (1) {

 

do {

 

TR0=1;

 

P1=TL0;

 

P2=TH0;

 

}

 

while (TF0==0);

 

TR0=0;

 

TF0=0;

 

} }

 

The example 8 explained above shows the 8051 C program to count up and display it on TH0 and TL0 registers in pin P2 and P1 respectively. The External clock connected to T0 on P3.4 .

  1. Summary

In this lecture how to write Embedded C Timer Programming was discussed. Counter in 8051 and basic Timer operation is also discussed. Embedded C program for Timer and counter are discussed with examples.

 

  1. References
  1. “The 8051 Microcontroller and Embedded Systems Using Assembly and CSecond Edition”, Muhammad Ali Mazidi, Janice Gillispie Mazidi and Rolin D. McKinlay.