Microprocessor & Microcontrollers: 8051 Timer Programming in Assembly and C
Microprocessor & Microcontrollers: 8051 Timer Programming in Assembly and C
MICROCONTROLLERS
Chapter 9
8051 Timer Programming in Assembly and C
11/2/20 www.iiu.edu.pk 2
TMOD Register
Both timers 0 and 1 use the same register, called
TMOD (timer mode), to set the various timer
operation modes
TMOD is a 8-bit register
The lower 4 bits are for Timer 0
The upper 4 bits are for Timer 1
In each case,
The lower 2 bits are used to set the timer mode
The upper 2 bits to specify the operation
11/2/20 www.iiu.edu.pk 3
TMOD Register
11/2/20 www.iiu.edu.pk 4
Timer Mode Programming
11/2/20 www.iiu.edu.pk 5
Timer Mode Programming
11/2/20 www.iiu.edu.pk 6
Timer Mode Programming
11/2/20 www.iiu.edu.pk 7
TMOD Register
Example 9-1: Indicate which mode and which timer are selected for each of
the following. (a) MOV TMOD, #01H (b) MOV TMOD, #20H
(c) MOV TMOD, #12H
Solution: We convert the value from hex to binary.
(a) TMOD = 00000001, mode 1 of timer 0 is selected.
(b) TMOD = 00100000, mode 2 of timer 1 is selected.
(c) TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are selected.
Example 9-2: Find the timer’s clock frequency and its period for various
8051-based system, with the crystal frequency 11.0592 MHz when C/T bit of
TMOD is 0.
Solution: If C/T = 0, it is used as a timer for time delay generation. The clock source
for the time delay is the crystal frequency of the 8051.
1/12 × 11.0529 MHz = 921.6 KHz;
T = 1/921.6 kHz = 1.085 us
11/2/20 www.iiu.edu.pk 8
TMOD Register (GATE)
Timers of 8051 do starting and stopping by either
software or hardware control
In using software to start and stop the timer where
GATE=0
The start and stop of the timer are controlled by
way of software by the TR (timer start) bits TR0
and TR1
Setting GATE=1 starts it, and it is stopped by
Clearing GATE=0 .
The hardware way of starting and stopping the timer
by an external source is achieved by making GATE=1
in the TMOD register.
11/2/20 www.iiu.edu.pk 9
Mode 1 Programming
The following are the characteristics and operations of
mode1:
1. It is a 16-bit timer; it allows value of 0000 to FFFFH
to be loaded into the timer’s register TL and TH.
2. After TH and TL are loaded with a 16-bit initial
value, the timer must be started
This is done by TR0=1 for timer 0 and TR1=1 for
timer 1 to start.
3. After the timer is started, it starts to count up
11/2/20 www.iiu.edu.pk 10
Mode 1 Programming
When it rolls over from FFFFH to 0000, it sets high a
flag bit called TF (timer flag)
Each timer has its own timer flag: TF0 for timer 0,
and TF1 for timer 1
This timer flag can be monitored
When this timer flag is raised, one option would be to
stop the timer with TR0=0 or TR1=1 , for timer 0 and
timer 1, respectively.
4. 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
11/2/20 www.iiu.edu.pk 11
Mode 1 Programming
To generate a time delay
1. Load the TMOD value register indicating which
timer (timer 0 or timer 1) is to be used and which
timer mode (0 or 1) is Selected.
2. Load registers TL and TH with initial count value
11/2/20 www.iiu.edu.pk 12
Example 9-20: Write an 8051 C program to toggle all the bits of port P1
Mode 1 Programming
continuously with some delay in between. Use Timer 0, 16-bit mode to
generate the delay.
#include <reg51.h>
1/12 × 11.0529 MHz = 921.6 MHz;
T 1/12 × 11.0529
kHz MHz = 921.6
μs MHz;
void T0Delay(void);
= 1/921.6 = 1.085
void main(void){ T = 1/921.6 kHz = 1.085 μs
while (1) { FFFFH – 3500H = CAFFH = 51967
P1=0x55; FFFFH1–=3500H
51967+ 51968 =counts
CAFFH = 51967
51967+
51968 1 = 51968
× 1.085 countsms is the
μs = 56.384
T0Delay();
51968 × 1.085
approximate μs = 56.384 ms is the
delay
P1=0xAA; approximate delay
T0Delay();
}
}
void T0Delay(){
TMOD=0x01; // use timer0 mode 1
TL0=0x00; // initial values
TH0=0x35;
TR0=1; // start timer0 now
while (TF0==0); // wait for overflow to happen
TR0=0; // stop timer0
TF0=0; }
11/2/20 // clear overflow flag
www.iiu.edu.pk 13
Calculating Delay Length
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
11/2/20 www.iiu.edu.pk 14
Example 9-21: Write an 8051 C program to toggle only bit P1.5 continuously
Mode 1 Programming
every 50 ms. Use Timer 0, mode 1 (16-bit) to create the delay. Test the
program on the (a) AT89C51 and (b) DS89C420.
#include <reg51.h>
void T0M1Delay(void); FFFFH – 4BFDH = B402H = 46082
FFFFH
Now – 4BFDH
46082 = B402H
+ 1 = 46083 = 46082
counts
sbit mybit=P1^5; Now×46082
46083 1.085 +μs1 == 50
46083
ms counts
void main(void){ 46083 × 1.085 μs = 50 ms
while (1) { 50 ms / 1.085 μs = 46083
mybit = ~ mybit; 50 ms–/46083
65,536 1.085=μs19453
= 46083
= 4BFD
65,536
TH0 = 4B– ,46083
TL0 ==FD 19453 = 4BFD
T0M1Delay();
TH0 = 4B , TL0 = FD
}
}
void T0M1Delay(void){
TMOD=0x01; // use timer0 mode 1
TL0=0xFD;
TH0=0x4B;
TR0=1;
while (TF0==0); // wait for overflow to occure
TR0=0; // stop timer
TF0=0; // clear overflow flag
} 11/2/20 www.iiu.edu.pk 15
Example 9-25: A switch is connected to pin P1.2. Write an 8051 C program
Mode 1 Programming
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.
#include <reg51.h>
sbit mybit = P1^5; 1/500 Hz = 0.002 sec
1/500
0.002 /2Hz== 0.002
0.001sec
sbit SW = P1^7; 0.002counts
/2 == 0.001/1.085
0.001
Total μs = 921
void T0M1Delay( bit ); 65536 – 921 = 64615 = FC67 μs = 921
Total counts = 0.001/1.085
void main(void){ 65536 –, TL=67
TH=FC 921 = 64615 = FC67
SW=1; TH=FC , TL=67
while (1) { FC67H = 64615
FC67H
65536 – = 64615
64615 = 921
mybit=~mybit; 65536 – 64615 =999.285
921 μs
if (SW==0) T0M1Delay(0); 921 × 1.085 μs =
1 921 × 1.085μs
/ (999.285 μs×=2)999.285 μs
= 500 Hz
else T0M1Delay(1); 1 / (999.285 μs × 2) = 500 Hz
}
}
void T0M1Delay( bit c){
TMOD=0x01;
if (c==0) { TH0=0xFC; TL0=0x67; }
else { TH0=0xFD; TL0=0x9A; }
TR0=1;
while(TF0==0);
11/2/20 TR0=0; www.iiu.edu.pk
TF0=0; } 16
Example 9-23: Write an 8051 C program to toggle only pin P1.5 continuously
Mode 1 Programming
every 250 ms. Use Timer 0, mode 2 (8-bit auto-reload) to create the delay
#include <reg51.h> 1/12 × 11.0529 MHz = 921.6 MHz;
void T0M2Delay(void); T 1/12 × 11.0529
= 1/921.6 kHz MHz = 921.6
= 1.085 μs MHz;
sbit mybit=P1^5; T = 1/921.6 kHz = 1.085 μs
unsigned char x,y; Total counts = 200 μs /1.085 μs = 184
Total
256– 184counts
= 72 = 200 μs /1.085 μs = 184
void main(void){ 256– 184 = 72
while (1){ So TH0 = 72 = -184 = 0x48
So TH0 = 72 = -184 = 0x48
mybit=~mybit;
200μs × 5 × 250 = 250 ms
T0M2Delay(); 200μs × 5 × 250 = 250 ms
} 256 – 72 = 184
} 184256 – 72 =μs
× 1.085 184
= 200 μs
void T0M2Delay(void){ 200 μs × 250 × 5 == 200
184 × 1.085 μs 250 μs
ms
200 μs × 250 × 5 = 250 ms
TMOD=0x02; TH0=-184;
for (x=0;x<250;x++)
for (y=0;y<5;y++){
TR0=1;
while (TF0==0); TR0=0; TF0=0;
}
}
11/2/20 www.iiu.edu.pk 17
Example 9-24: Write an 8051 C program to create a frequency of 2500 Hz on
pin P2.7. Use Timer 1, mode 2 to create delay.
#include <reg51.h>
1/12 × 11.0529 MHz = 921.6 MHz;
void T1M2Delay(void);
T 1/12 × 11.0529
= 1/921.6 kHz MHz = 921.6
= 1.085 μs MHz;
sbit mybit = P2^7; T = 1/921.6 kHz = 1.085 μs
void main(void){ 1/2500 Hz = 400 μs
unsigned char x; 1/2500
400 μs /2Hz== 400
200μs
μs
400 μs /2 = 200 μs
while (1) {
mybit=~mybit; Total counts
T1M2Delay(); = Total
200 μscounts
/1.085 μs = 184 counts
256 – 184 =/1.085
= 200 μs 72 μs = 184 counts
} 256 – 184 = 72
} So TH1 = 72 = -184 = 0x48
void T1M2Delay(void){ So TH1 = 72 = -184 = 0x48
TMOD=0x20;
TH1=-184;
TR1=1;
while (TF1==0);
TR1=0;
TF1=0;
}
11/2/20 www.iiu.edu.pk 18
Example : Write an 8051 C program to create a frequency of 100 Hz and 70%
duty cycle on pin P2.7. which timer mode you would prefer ?
#include <reg51.h>
void Delay(char i){ 1/12 × 11.0529 MHz = 921.6 MHz;
T 1/12 × 11.0529
= 1/921.6 kHz MHz = 921.6
= 1.085 μs MHz;
TMOD=0x01; T = 1/921.6 kHz = 1.085 μs
TH0 = i ? 0xE6:0xF5; 1/100 Hz = 10 ms
TL0 = i ? 0xCC:0x33; 1/100for
Time Hz1== 1010 ms
ms * 0.7 = 7 ms
TR0=1; Timefor
Time for0=1=1010ms
ms* 0.3
* 0.7 = = 3 7ms
ms
Time for 0= 10 ms * 0.3 = 3 ms
while (TF0==0);
TR0=0; Total counts
Total
For 1 = counts
7 ms /1.085 μs = 6452 counts
TF0=0;
For 0 = 3 7ms
For 1 = ms/1.085
/1.085μsμs= =2765counts
6452 counts
} For 0 = 3So
6450>255 msmode
/1.0852 μs = 2765counts
is diffucult to use
6450>255
We’ll So mode
use mode 1. 2 is diffucult to use
sbit mybit = P2^7; We’ll
void main(void){ TH TL =use mode
65,536 1. = 59084 = 0xE6CC
– 6452
THTL
TH TL= =65,536
65,536– –2765
6452= =62771
59084= =0xF533
0xE6CC
while (1) { TH TL = 65,536 – 2765 = 62771 = 0xF533
mybit = 1; Delay(1);
mybit = 0; Delay(0);
}
}
11/2/20 www.iiu.edu.pk 19
Example : Write an 8051 C program to create a delay of one second using
timer 1.
#include <reg51.h>
void OneSec(){
unsigned char i;
TMOD = 0x10;
for (i = 0 ; i<20 ; i++){
TH1 = 0x4B; 1/12 × 11.0529 MHz = 921.6 MHz;
TL1 = 0xFD; T 1/12 × 11.0529
= 1/921.6 kHz MHz = 921.6
= 1.085 μs MHz;
T = 1/921.6 kHz = 1.085 μs
TR1 = 1;
Max delays
while (TF1==0); InMax delays
mode 2 = 256*1.085 μs = 277.76 μs
TR1 = 0; TF1 = 0; In mode 1 2= =65536*1.085
In mode 256*1.085 μs
μs = =71.11
277.76
msμs
} In mode 1 = 65536*1.085 μs = 71.11 ms
} Suppose we use delay of 50 ms
sbit tick = P2^7; 1 Suppose
sec / 50 mswe= use
1000delay of 50
ms / 50 msms
= 20 loops
1 sec / 50 ms = 1000 ms / 50 ms = 20 loops
void main(void){
Counts = 50 ms / 1.085 μs = 46,083 counts
while (1) { Counts = 50 ms / 1.085 μs = 46,083 counts
P2++; OneSec(); We’ll use mode 1.
} We’ll
TH TL =use mode
65,536 1.
– 46,083 = 19,453 = 0x4BFD
TH TL = 65,536 – 46,083 = 19,453 = 0x4BFD
}
11/2/20 www.iiu.edu.pk 20
Example 9-26: 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.
#include <reg51.h>
void main(void){
T1=1;
TMOD=0x60;
TH1=0;
TR1=1;
while (1)
P1=TL1;
}
11/2/20 www.iiu.edu.pk 21
Example 9-27: 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.
#include <reg51.h>
void main(void){
T0=1;
TMOD=0x05;
TL0=0;
TH0=0;
TR0=1;
while(1){
P1=TL0;
P2=TH0;
}
}
11/2/20 www.iiu.edu.pk 22
THE END OF
CHAPTER 9
11/2/20 www.iiu.edu.pk 23