Micro Controller
Micro Controller
PROGRAMMING TIMERS
The 89S52 has three timers/counters, they can be used
either as
Timers to generate a time delay or as Event counters
to count events happening outside the microcontroller
Both Timer 0 and Timer 1 are 16 bits wide
Since 89S52 has an 8-bit architecture, each 16-bits
timer is accessed as two separate registers
g
of low
byte and high byte
F
Function
ti
Pi
Pin
P3.0
RxD
10
P3.1
TxD
11
P3.2
INT0
12
P3.3
INT1
13
P3.4
T0
14
P3.5
T1
15
P3.6
WR
16
6
P3.7
RD
17
Serial
communications
External interrupts
p
Timers
Read/Write signals of
external memories
TH0
TL0
TH1
TL1
(LSB)
C/T
M1
Timer1
M0
GATE
C/T
M1
Timer0
M0
Gatingg control
when set (GATE=1)
Timer/counter is enable
only while the INTx
pin is high and the TRx
TR
control pin is set
When cleared
(GATE=0)
the timer is enabled
whenever the TRx
control bit is set
M1
M0
Mode
Operating
p
g Mode
8-bit
8
bit auto reload
8-bit auto reload timer/counter; THx holds a
value which is to be reloaded TLx each time
it overfolws
Solution:
We convert the value from hex to binary. From Figure 9-3 we have:
(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.
Timer 0, mode 2
C/T = 0 to use XTAL
clock source
gate = 0 to use
internal (software) start
and stop method.
PROGRAMMING TIMERS IN C
Accessing Timer Registers
Solution 3:
#include <regx52
<regx52.h>
h>
void T0Delay(void);
void main(void) {
while (1) {
P1_4=0;
T0Delay();
y();
P1_4=1;
T0Delay();
}
}
PROGRAMMING TIMERS IN C
Accessing Timer Registers
Solution 3:
void T0Delay(){
TMOD=0x01;
TL0=0xF2;
TH0=0xFF;
TH0
0xFF;
TR0=1;
while (TF0==0);
TR0=0;
TR0
0;
TF0=0;
}
PROGRAMMING TIMERS
Mode 1 Programming Steps to Mode 1 Program (cont)
Example 3 (cont)
1. The T0Delay() function using the timer is called.
2. In the T0Delay() function, timer 0 is started by the TR0=1
instruction.
3 Timer 0 counts up with the passing of each clock
3.
clock, which is
provided by the crystal oscillator. As the timer counts up, it
goes through
g
g the states of FFF3,, FFF4,, FFF5,, FFF6,, FFF7,,
FFF8, FFF9, FFFA, FFFB, and so on until it reaches FFFFH.
One more clock rolls it to 0, raising the timer flag (TF0=1). At
that point, the condition in while loop is false.
PROGRAMMING TIMERS
Mode 1 Programming Steps to Mode 1 Program (cont)
Example 3 (cont)
4 Timer 0 is stopped by the instruction TR0=0
4.
TR0=0. The
T0Delay() function ends, and the process is repeated.
Notice that to repeat the process, we must reload the TL
and TH registers, and start the process is repeated
PROGRAMMING TIMERS
Mode 1Programming Steps to Mode 1 Program (cont)
Example 3 (cont)
The number of counts for the roll over is FFFFH FFF2H = 0DH
(13 decimal). However, we add one to 13 because of the extra
clock
l k needed
d d when
h it rolls
ll over from
f
FFFF to
t 0 and
d raise
i the
th TF
flag. This gives 14 1.085us = 15.19us for half the pulse. For
th entire
the
ti period
i d it is
i T = 2 15.19us
15 19
= 30.38us
30 38
as the
th time
ti
delay generated by the timer.
PROGRAMMING TIMERS IN C
Example 6
Write an 89S52 C program to toggle all bits of P2 continuously
every 500 ms
ms. Use Timer 1
1, mode 1 to create the delay
delay.
Solution:
#include <reg51.h>
void T1M1Delay(void);
void main(void){
unsigned char x;
P2=0x55;
while (1) {
P2=~P2;
P2
P2
for (x=0;x<20;x++)
T1M1Delay();}
y();}
}
PROGRAMMING TIMERS IN C
PROGRAMMING TIMERS IN C
Times 0/1 Delay Using Mode 2 (8-bit Auto-reload)
Example 8
Write an 89S52 C p
program
g
to toggle
gg only
yp
pin P1.5 continuously
y
every 250 ms. Use Timer 0, mode 2 (8-bit auto-reload) to create
the delay.
PROGRAMMING TIMERS IN C
Times 0/1 Delay Using Mode 2 (8-bit Auto-reload)
Example 8
Solution:
#i l d <
#include
<regx52.h>
52 h>
void T0M2Delay(void);
sbit mybit=P1^5;
void main(void){
unsigned char x,y;
TMOD=0x02;
TH0=-23;
while (1) {
y
y
mybit=~mybit;
for (x=0;x<250;x++)
for (y=0;y<40;y++)
T0M2Delay();
y();
}
}
PROGRAMMING TIMERS IN C
Times 0/1 Delay Using Mode 2 (8-bit Auto-reload)
Example 8
Solution:
void T0M2Delay(void){
TR0=1;
while
hil (TF0
(TF0==0);
0)
TR0=0;
TF0=0;
}
PROGRAMMING TIMERS IN C
Example 9
Write an 89S52 C program to create a frequency of 2500
Hz on pin P1.6 Use Timer 1, mode 2 to create delay.
PROGRAMMING TIMERS IN C
Example 9 (cont)
Solution:
#include <regx52.h>
void T1M2Delay(void);
sbit mybit=P1^6;
mybit=P1 6;
void main(void){
unsigned char x;
TMOD=0x20;
TH1=-184;
while (1) {
mybit=~mybit;
T1M2Delay();
}
1/2500 H
Hz = 400 s
400 s /2 = 200 s
200 s / 1.085 s = 184
PROGRAMMING TIMERS IN C
COUNTER PROGRAMMING
Timers can also be used as counters counting events
happening outside the 89S52
When it iis used
Wh
d as a counter,
t it iis a pulse
l outside
t id off
the 89S52 that increments the TH, TL registers
P
Programming
i th
the timer
ti
in
i the
th last
l t section
ti also
l applies
li tto
programming it as a counter
Except the source of the frequency
Port
Function Description
14
P3.4
T0
15
P3.5
T1
COUNTER PROGRAMMING
C/T Bit in TMOD Register (cont)
PROGRAMMING TIMERS IN C
C Programming of Timers as Counters
Example 10
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
0H.
PROGRAMMING TIMERS IN C
C Programming of Timers as Counters
Example Solution 10:
#include <regx52.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 } }
TF1=0;}
PROGRAMMING TIMERS IN C
C Programming of Timers as Counters (cont)
Example 11
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,
P1 respectively.
respectively
PROGRAMMING TIMERS IN C
C Programming of Timers as Counters (cont)
Example 11 (cont)
Solution:
#include <regx52
<regx52.h>
h>
void main(void){
T0=1;
TMOD 0x05;
TMOD=0x05;
TL0=0
TH0=0;
while ((1)) {
do {
TR0=1;
P1=TL0;
P2=TH0;
}while (TF0==0);
TR0=0;
TF0=0;}}
PROGRAMMING TIMERS IN C
Example 12
A switch is connected to pin P1.2.
P1 2 Write an 89S52 C
program to monitor SW and create the following
q
on p
pin P1.7:
frequencies
SW=0: 500Hz
SW=1: 750Hz,,
use Timer 0, mode 1 for both of them.
PROGRAMMING TIMERS IN C
Example 12 (cont)
Solution:
#include
#incl
de <reg
<regx52.h>
52 h>
sbit mybit=P1^7;
sbit SW=P1^2;
void T0M1Delay(unsigned char);
void main(void){
SW=1;
while (1) {
mybit=~mybit;
if (SW==0)
T0M1Delay(0);
else
T0M1Delay(1);
}
}
.....
PROGRAMMING TIMERS IN C
Example 12 (cont)
void T0M1Delay(unsigned char c){
TMOD=0x01;
FC67H = 64615
if (c==0) {
65536 64615 = 921
TL0=0x67;;
921 1.085 s = 999.285 s
TH0=0xFC;
1 / (999.285 s 2) = 500 Hz
}
else {
TL0=0x9A;
TH0=0xFD;
}
TR0 1
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}