Timer Programming: The 8051 Microcontroller and Embedded Systems: Using Assembly and C
Timer Programming: The 8051 Microcontroller and Embedded Systems: Using Assembly and C
Timer Programming: The 8051 Microcontroller and Embedded Systems: Using Assembly and C
TH1 TL1
Timer1 Timer0
(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
PROGRAMMING
Timer1 Timer0
TIMERS
If C/T = 0, it is used
as a timer for time Example 9-2
delay generation. Find the timer’s clock frequency and its period for various 8051-based system,
The clock source for with the crystal frequency 11.0592 MHz when C/T bit of TMOD is 0.
the time delay is the
Solution:
crystal frequency of
XTAL
the 8051 ÷12
oscillator
XTAL TH TL TF
÷12
oscillator
TF goes high Overflow
C/T = 0 TR when FFFF 0 flag
3. (cont’)
When it rolls over from FFFFH to 0000, it sets
PROGRAMMING high a flag bit called TF (timer flag)
TIMERS – Each timer has its own timer flag: TF0 for
timer 0, and TF1 for timer 1
– This timer flag can be monitored
Mode 1 When this timer flag is raised, one option
Programming would be to stop the timer with the
(cont’) instructions CLR TR0 or CLR TR1, 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
XTAL TH TL TF
÷12
oscillator
TF goes high Overflow
C/T = 0 TR when FFFF 0 flag
To generate a time delay
PROGRAMMING 1. Load the TMOD value register indicating
TIMERS which timer (timer 0 or timer 1) is to be
used and which timer mode (0 or 1) is
selected
Mode 1
Programming 2. Load registers TL and TH with initial count
value
Steps to Mode 1 3. Start the timer
Program 4. Keep monitoring the timer flag (TF) with
the JNB TFx,target instruction to see
if it is raised
Get out of the loop when TF becomes high
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
Example 9-4
PROGRAMMING In the following program, we create a square wave of 50% duty cycle (with
equal portions high and low) on the P1.5 bit. Timer 0 is used to generate the
TIMERS time delay. Analyze the program
DELAY:
PROGRAMMING SETB TR0 ;start the timer 0
AGAIN: JNB TF0,AGAIN ;monitor timer flag 0
TIMERS ;until it rolls over
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
Mode 1 RET
Programming 4. The DELAY subroutine using the timer is called.
5. In the DELAY subroutine, timer 0 is started by the SETB TR0 instruction.
Steps to Mode 1 6.Timer 0 counts up with the passing of each clock, which is provided by the
crystal oscillator. As the timer counts up, it goes through the states of FFF3,
Program FFF4, FFF5, FFF6, FFF7, FFF8, FFF9, FFFA, FFFB, and so on until it
(cont’) reaches FFFFH. One more clock rolls it to 0, raising the timer flag (TF0=1).
At that point, the JNB instruction falls through.
XTAL Overflow
÷12 TL TF
oscillator flag
TR Reload
TF goes high
C/T = 0
when FF 0
TH
To generate a time delay
PROGRAMMING
1. Load the TMOD value register indicating
TIMERS which timer (timer 0 or timer 1) is to be
used, and the timer mode (mode 2) is
Mode 2 selected
Programming 2. Load the TH registers with the initial
count value
Steps to Mode 2
Program 3. Start timer
4. Keep monitoring the timer flag (TF) with
the JNB TFx,target instruction to see
whether it is raised
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
Timers can also be used as counters
COUNTER
PROGRAMMING
counting events happening outside the
8051
When it is used as a counter, it is a pulse
outside of the 8051 that increments the
TH, TL registers
TMOD and TH, TL registers are the same
as for the timer discussed previously
Programming the timer in the last
section also applies to programming it
as a counter
Except the source of the frequency
The C/T bit in the TMOD registers
COUNTER
PROGRAMMING
decides the source of the clock for the
timer
C/T Bit in When C/T = 1, the timer is used as a
TMOD Register counter and gets its pulses from outside
the 8051
The counter counts up as pulses are fed from
pins 14 and 15, these pins are called T0 (timer
0 input) and T1 (timer 1 input)
C/T = 1
TR Reload
TF goes high
when FF 0
TH
TCON (timer control) register is an 8-
COUNTER
PROGRAMMING
bit register
TCON: Timer/Counter Control Register
TCON
Register TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Tx Pin
Pin 3.4 or 3.5 C/T = 1
Gate TR
INT0 Pin
Pin 3.2 or 3.3
Example 9-20
Write an 8051 C program to toggle all the bits of port P1 continuously
PROGRAMMING with some delay in between. Use Timer 0, 16-bit mode to
generate the delay.
TIMERS IN C
Solution:
#include <reg51.h>
Accessing void T0Delay(void);
Timer Registers void main(void){
while (1) {
P1=0x55;
T0Delay();
P1=0xAA;
T0Delay();
}
}
void T0Delay(){
TMOD=0x01;
TL0=0x00; FFFFH – 3500H = CAFFH
TH0=0x35; = 51967 + 1 = 51968
TR0=1;
while (TF0==0); 51968 1.085 s = 56.384 ms is the
TR0=0; approximate delay
TF0=0;
}
Example 9-22
Write an 8051 C program to toggle all bits of P2 continuously every
PROGRAMMING 500 ms. Use Timer 1, mode 1 to create the delay.
TIMERS IN C Solution:
//tested for DS89C420, XTAL = 11.0592 MHz
#include <reg51.h>
Times 0/1 void T1M1Delay(void);
void main(void){
Delay Using unsigned char x;
P2=0x55;
Mode 1 (16-bit while (1) {
Non Auto- P2=~P2;
for (x=0;x<20;x++)
reload) T1M1Delay();
(cont’) }
}
void T1M1Delay(void){
TMOD=0x10;
TL1=0xFE; A5FEH = 42494 in decimal
TH1=0xA5; 65536 – 42494 = 23042
TR1=1;
while (TF1==0); 23042 1.085 s = 25 ms and
TR1=0; 20 25 ms = 500 ms
TF1=0;
}
Example 9-25
PROGRAMMING A switch is connected to pin P1.2. Write an 8051 C program to
TIMERS IN C 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.
Times 0/1 Solution:
Delay Using #include <reg51.h>
sbit mybit=P1^5;
Mode 1 (16-bit sbit SW=P1^7;
Non Auto- void T0M1Delay(unsigned char);
void main(void){
reload) SW=1;
(cont’) while (1) {
mybit=~mybit;
if (SW==0)
T0M1Delay(0);
else
T0M1Delay(1);
}
}
.....
Example 9-25
PROGRAMMING
.....
TIMERS IN C
void T0M1Delay(unsigned char c){
TMOD=0x01;
Times 0/1 if (c==0) {
TL0=0x67;
Delay Using TH0=0xFC;
FC67H = 64615
65536 – 64615 = 921
Mode 1 (16-bit }
else { 921 1.085 s = 999.285 s
Non Auto- TL0=0x9A;
1 / (999.285 s 2) = 500 Hz
TH0=0xFD;
reload) }
(cont’) TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}
Example 9-23
Write an 8051 C program to toggle only pin P1.5 continuously every
PROGRAMMING 250 ms. Use Timer 0, mode 2 (8-bit auto-reload) to create the
delay.
TIMERS IN C
Solution:
#include <reg51.h>
Times 0/1 void T0M2Delay(void);
sbit mybit=P1^5;
Delay Using void main(void){ Due to overhead of the for loop
unsigned char x,y;
Mode 2 (8-bit while (1) { in C, we put 36 instead of 40
Auto-reload) 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; 256 – 23 = 233
TR0=1; 23 1.085 s = 25 s and
while (TF0==0);
TR0=0; 25 s 250 40 = 250 ms
TF0=0;
}
Example 9-24
PROGRAMMING Write an 8051 C program to create a frequency of 2500 Hz on pin
TIMERS IN C P2.7. Use Timer 1, mode 2 to create delay.
Solution:
Times 0/1 #include <reg51.h>
void T1M2Delay(void);
Delay Using sbit mybit=P2^7;
void main(void){
Mode 2 (8-bit unsigned char x;
Auto-reload) while (1) {
mybit=~mybit;
(cont’) T1M2Delay();
}
}
void T1M2Delay(void){
TMOD=0x20; 1/2500 Hz = 400 s
TH1=-184;
TR1=1; 400 s /2 = 200 s
while (TF1==0); 200 s / 1.085 s = 184
TR1=0;
TF1=0;
}
Example 9-26
PROGRAMMING Assume that a 1-Hz external clock is being fed into pin T1 (P3.5).
TIMERS IN C 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.
C Programming Solution:
#include <reg51.h>
of Timers as sbit T1=P3^5;
void main(void){
Counters T1=1;
TMOD=0x60;
TH1=0;
while (1) {
do {
TR1=1;
P1=TL1;
}
while (TF1==0);
TR1=0;
TF1=0;
}
}
Example 9-27
Assume that a 1-Hz external clock is being fed into pin T0 (P3.4).
PROGRAMMING Write a C program for counter 0 in mode 1 (16-bit) to count the pulses
TIMERS IN C and display the state of the TH0 and TL0 registers on P2 and P1,
respectively.
C Programming Solution:
#include <reg51.h>
of Timers as void main(void){
T0=1;
Counters TMOD=0x05;
(cont’) TL0=0
TH0=0;
while (1) {
do {
TR0=1;
P1=TL0;
P2=TH0;
}
while (TF0==0);
TR0=0;
TF0=0;
}
}
SERIAL COMMUNICATION
Computers transfer data in two ways:
BASICS OF
SERIAL Parallel
COMMUNICA- Often 8 or more lines (wire conductors) are
used to transfer data to a device that is only a
TION few feet away
Serial
To transfer to a device located many meters
away, the serial method is used
The data is sent one bit at a time
D7
Serial data communication uses two
BASICS OF methods
SERIAL
Synchronous method transfers a block of
COMMUNICA- data at a time
TION
(cont’)
Asynchronous method transfers a single
byte at a time
It is possible to write software to use
either of these methods, but the
programs can be tedious and long
There are special IC chips made by many
manufacturers for serial communications
UART (universal asynchronous Receiver-
transmitter)
USART (universal synchronous-asynchronous
Receiver-transmitter)
If data can be transmitted and received,
BASICS OF it is a duplex transmission
SERIAL If data transmitted one way a time, it is
COMMUNICA- referred to as half duplex
TION If data can go both ways at a time, it is full
duplex
Half- and Full- This is contrast to simplex transmission
Duplex
Transmission Simplex Transmitter Receiver
Transmitter Receiver
Half Duplex
Receiver Transmitter
Transmitter Receiver
Full Duplex
Receiver Transmitter
A protocol is a set of rules agreed by
BASICS OF both the sender and receiver on
SERIAL How the data is packed
COMMUNICA- How many bits constitute a character
TION When the data begins and ends
Asynchronous serial data
Start and Stop communication is widely used for
Bits character-oriented transmissions
Each character is placed in between start
and stop bits, this is called framing
Block-oriented data transfers use the
synchronous method
The start bit is always one bit, but the
stop bit can be one or two bits
The start bit is always a 0 (low) and the
BASICS OF stop bit(s) is 1 (high)
SERIAL
COMMUNICA- ASCII character “A” (8-bit binary 0100 0001)
TION
ground
DTR (data terminal ready)
BASICS OF When terminal is turned on, it sends out
SERIAL signal DTR to indicate that it is ready for
COMMUNICA- communication
TION DSR (data set ready)
When DCE is turned on and has gone
RS232 Pins through the self-test, it assert DSR to
indicate that it is ready to communicate
RTS (request to send)
When the DTE device has byte to transmit,
it assert RTS to signal the modem that it
has a byte of data to transmit
CTS (clear to send)
When the modem has room for storing the
data it is to receive, it sends out signal CTS
to DTE to indicate that it can receive the
data now
DCD (data carrier detect)
BASICS OF The modem asserts signal DCD to inform
SERIAL the DTE that a valid carrier has been
COMMUNICA- detected and that contact between it and
TION the other modem is established
RI (ring indicator)
RS232 Pins An output from the modem and an input to
(cont’) a PC indicates that the telephone is ringing
It goes on and off in synchronous with the
ringing sound
8051
A line driver such as the MAX232 chip is
CONNECTION required to convert RS232 voltage
TO RS232 levels to TTL levels, and vice versa
8051 has two pins that are used
specifically for transferring and
receiving data serially
These two pins are called TxD and RxD and
are part of the port 3 group (P3.0 and P3.1)
These pins are TTL compatible; therefore,
they require a line driver to make them
RS232 compatible
We need a line driver (voltage
8051 converter) to convert the R232’s signals
CONNECTION to TTL voltage levels that will be
TO RS232 acceptable to 8051’s TxD and RxD pins
MAX232 Vcc
C3
+
MAX232 requires
16 2 four capacitors
+
1 MAX232
C1
3 6 8051
4 C4
+
C2
+
MAX232
5 P3.1 11 11
TxD 14 2 5
T1in T1out
11 14
13 3
R1out R1in P3.0 10 12
12 13
RxD
T2in T2out
10 7
R2out R2int
DB-9
9 8
14 MAX233 11
12
15
8051
16
17 MAX233
10 P3.1 11 2
TxD 5 2 5
T1in T1out
2 5
4 3
R1out R1in P3.0 10 3
3 4
RxD
T2in T2out
1 18
DB-9
R2out R2int
20 19
IN C Solution:
#include <reg51.h>
void main(void){
Transmitting unsigned char mybyte;
TMOD=0x20; //use Timer 1, mode 2
and Receiving TH1=0xFA; //4800 baud rate
Data SCON=0x50;
//start timer
TR1=1;
(cont’) while (1) { //repeat forever
while (RI==0); //wait to receive
mybyte=SBUF; //save value
P1=mybyte; //write value to port
RI=0;
}
}