8051 Timers and Serial Port: Module - 4
8051 Timers and Serial Port: Module - 4
Module – 4
8051 Timers and Serial Port
Timers/Counters are used generally for
Time reference
Creating delay
Wave form properties measurement
Periodic interrupt generation
The 8051 has two 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
Timer in 8051 is used as timer, counter and baud rate generator. Timer always counts up irrespective
of whether it is used as timer, counter, or baud rate generator: Timer is always incremented by the
microcontroller. The time taken to count one digit up is based on master clock frequency.
TMOD Register
Both timers 0 and 1 use the same register, called TMOD (timer mode), to set the various
timer operation modes
TMOD is an 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.
TIMER MODES
Timers can operate in four different modes. They are as follows
Timer Mode-0: In this mode, the timer is used as a 13-bit UP counter as follows.
The lower 5 bits of TLX and 8 bits of THX are used for the 13-bit count. Upper 3 bits of TLX are
ignored. When the counter rolls over from all 0's to all 1's, TFX flag is set and an interrupt is
generated. The input pulse is obtained from the previous stage. If TR1/0 bit is 1 and Gate bit is 0,
the counter continues counting up. If TR1/0 bit is 1 and Gate bit is 1, then the operation of the
counter is controlled by input. This mode is useful to measure the width of a given pulse fed to input.
Timer Mode-1: This mode is similar to mode-0 except for the fact that the Timer operates in 16-bit
mode.
Mode 1:
Load the TMOD value register indicating which timer (0 or 1) is to be used and which timer
mode is selected.
Load registers TL and TH with initial count values.
Start the timer by the instruction “SETB TR0” for timer 0 and “SETB TR1” for timer 1.
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.
Stop the timer with the instructions “CLR TR0” or “CLR TR1”, for timer 0 and timer
1, respectively.
Clear the TF flag for the next round with the instruction “CLR TF0” or “CLR TF1”, for
timer 0 and timer 1, respectively.
Go back to step 2 to load TH and TL again.
Mode 0:
The programming techniques mentioned here are also applicable to counter/timer mode 0. The only
difference is in the number of bits of the initialization value.
Mode 2:
Load the TMOD value register indicating which timer (0 or 1) is to be used; select timer
mode 2.
Load TH register with the initial count value. As it is an 8-bit timer, the valid range is from
00 to FFH.
Start the timer.
Keep monitoring the timer flag (TFx) with the “JNB TFx, target” instruction to see if it is
raised. Get out of the loop when TFx goes high.
Clear the TFx flag.
Go back to step 4, since mode 2 is auto-reload.
Example 4-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. 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.
MIT MYSORE | DEPT. OF ELECTRONICS & COMMUNICATION ENGG. 65
18EC46 MICROCONTROLLER | MODULE4:8051 TIMERS AND SERIAL PORT
(c) TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are selected
Example 4-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.
Example 4-3
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 time delay. Analyze the program
7. Timer 0 is stopped by the instruction CLR TR0. The DELAY subroutine 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
Example 4-4
In Example 9-4, calculate the amount of time delay in the DELAY subroutine generated by the timer.
Assume XTAL = 11.0592 MHz.
Solution:
The timer works with a clock frequency of 1/12 of the XTAL frequency; therefore, we have 11.0592
MHz / 12 = 921.6 kHz as the timer frequency. As a result, each clock has a period of T
=1/921.6kHz = 1.085us. In other words, Timer 0 counts up each 1.085 us resulting in delay =
number of counts × 1.085us.
The number of counts for the roll over is FFFFH – FFF2H = 0DH (13decimal). However, we add
one to 13 because of the extra clock needed when it rolls over from FFFF to 0 and raise the TF flag.
This gives 14× 1.085us = 15.19us for half the pulse. For the entire period it is T = 2× 15.19us =
30.38us as the time delay generated by the timer.
Example 4-5
The following program generates a square wave on P1.5 continuously using timer 1 for a time delay.
Find the frequency of the square wave if XTAL = 11.0592 MHz. In your calculation do not include
the overhead due to Instructions in the loop.
MOV TMOD,#10 ;Timer 1, mod 1 (16-bitmode)
AGAIN: MOV TL1,#34H ;TL1=34H, low byte of timer
MOV TH1,#76H ;TH1=76H, high byte timer
SETB TR1 ;start the timer 1
BACK: JNB TF1,BACK ;till timer rolls over
CLR TR1 ;stop the timer 1
CPL P1.5 ;comp. p1. to get hi, lo
CLR TF1 ;clear timer flag 1
SJMP AGAIN ;is not auto-reload
Solution:
Since FFFFH – 7634H = 89CBH + 1 = 89CCH and 89CCH = 35276 clock count and 35276
× 1.085 us = 38.274 ms for half of the square wave. The frequency = 13.064Hz. Also notice that the
high portion and low portion of the square wave pulse are equal. In the above calculation, the
overhead due to all the instruction in the loop is not included.
Example 4-6
Write a program to continuously generate a square wave of 2 kHz frequency on pin P1.5 using timer
1. Assume the crystal oscillator frequency to be 12 MHz.
The period of the square wave is T = 1/(2 kHz) = 500 µs. Each half pulse = 250 µs.
The value n for 250 µs is: 250 µs /1 µs = 250 65536 - 250 = FF06H. TL = 06H and TH = 0FFH.
Example4-6
Write a program segment that uses timer 1 in mode 2 to toggle P1.0 once whenever the counter
reaches a count of 100. Assume the timer clock is taken from external source P3.5 (T1).
The TMOD value is 60H The initialization value to be loaded into TH1 is 256 - 100 = 156 = 9CH
SERIAL COMMUNICATION
The 8051 microcontroller is parallel device that transfers eight bits of data simultaneously over eight
data lines to parallel I/O devices. Parallel data transfer over a long is very expensive. Hence, a serial
communication is widely used in long distance communication. In serial data communication, 8-bit
data is converted to serial bits using a parallel in serial out shift register and then it is transmitted over
a single data line. The data byte is always transmitted with least significant bit first.
Communication Links
1. Simplex communication link: In simplex transmission, the line is dedicated for transmission.
The transmitter sends and the receiver receives the data.
2. Half duplex communication link: In half duplex, the communication link can be used for
either transmission or reception. Data is transmitted in only one direction at a time.
3. Full duplex communication link: If the data is transmitted in both ways at the same time, it is
a full duplex i.e. transmission and reception can proceed simultaneously. This communication
link requires two wires for data, one for transmission and one for reception.
2. Asynchronous Serial data transmission: In this, different clock sources are used for
transmitter and receiver. In this mode, data is transmitted with start and stop bits. A
transmission begins with start bit, followed by data and then stop bit. For error checking
purpose parity bit is included just prior to stop bit. In Asynchronous serial data
communication a single byte is transmitted at a time.
Baud rate: The rate at which the data is transmitted is called baud or transfer rate. The baud rate is
the reciprocal of the time to send one bit. In asynchronous transmission, baud rate is not equal to
number of bits per second. This is because; each byte is preceded by a start bit and followed by parity
and stop bit. For example, in synchronous transmission, if data is transmitted with 9600 baud, it
means that 9600 bits are transmitted in one second. For bit transmission time = 1 second/ 9600 =
0.104 ms.
2. SCON register: The contents of the Serial Control (SCON) register are shown below. This
register contains mode selection bits, serial port interrupt bit (TI and RI) and also the ninth data
bit for transmission and reception (TB8 and RB8).
SM0, SM1:They determine the framing of data by specifying the number of bits per character, and
the start and stop bits
3. PCON register: The SMOD bit (bit 7) of PCON register controls the baud rate in
asynchronous mode transmission
RS-232 standards: To allow compatibility among data communication equipment made by various
manufactures, an interfacing standard called RS232 was set by the Electronics Industries Association
(EIA) in 1960. Since the standard was set long before the advent of logic family, its input and output
voltage levels are not TTL compatible. In RS232, a logic one (1) is represented by -3 to -25V and
referred as MARK while logic zero (0) is represented by +3 to +25V and referred as SPACE. For
this reason to connect any RS232 to a microcontroller system we must use voltage converters such as
MAX232 to convert the TTL logic level to RS232 voltage levels and vice-versa. MAX232 IC chips
are commonly referred as line drivers. In RS232 standard we use two types of connectors. DB9
connector or DB25 connector.
Example 4.7
With XTAL = 11.0592 MHz, find the TH1 value needed to have thefollowing baud rates. (a) 9600
(b) 2400 (c) 1200
Solution:
The machine cycle frequency of 8051 = 11.0592 / 12 = 921.6 kHz,and 921.6 kHz / 32 = 28,800 Hz
is frequency by UART to timer 1 toset baud rate.
(a) 28,800 / 3 = 9600 where -3 = FD (hex) is loaded into TH1
(b) 28,800 / 12 = 2400 where -12 = F4 (hex) is loaded into TH1
(c) 28,800 / 24 = 1200 where -24 = E8 (hex) is loaded into TH1
Example 4.8
Write a program for the 8051 to transfer letter ‘A’ serially at 4800- baud rate, 8 bit data, 1 stop bit
continuously.
Solution:
MOV TMOD, #20H ;timer 1,mode 2(auto reload)
MOV TH1, #-6 ;4800 baud rate
MOV SCON, #50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
AGAIN: MOV SBUF, #”A” ;letter “A”to transfer
HERE: JNB TI, HERE ;wait for the last bit
CLR TI ;clear TI for next char
SJMP AGAIN ;keep sending A
Example 4.9
Write a program for the 8051 to transfer “YES” serially at 9600 baud, 8-bit data, 1 stop bit, do this
continuously
MOV TMOD, #20H ;timer 1,mode 2(auto reload)
MOV TH1, #-3 ;9600 baud rate
MOV SCON, #50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
AGAIN: MOV A, #”Y” ;transfer “Y”
ACALL TRANS
MOV A, #”E” ;transfer “E”
ACALL TRANS
MOV A, #”S” ;transfer “S”
ACALL TRANS
SJMP AGAIN ;keep doing it serial data transfer subroutine
TRANS: MOV SBUF,A ;load SBUF
HERE: JNB TI,HERE ;wait for the last bit
CLR TI ;get ready for next byte
RET
Example 4.10
Write a C program for 8051 to transfer the letter “A” serially at 4800 baud continuously. Use 8-bit data and 1
stop bit.
Solution:
#include <reg51.h>
void main(void)
{
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFA; //4800 baud rate
SCON=0x50;
TR1=1;
while (1) {
SBUF=‘A’; //place value in buffer
while (TI==0);
TI=0;
}
}
Example 4.11
Write an 8051 C program to transfer the message “YES” serially at 9600 baud, 8-bit data, 1 stop bit.
Do this continuously.
Solution:
#include <reg51.h>
void SerTx(unsigned char);
void main(void)
{
TMOD=0x20; //use Timer 1, mode 2
TH1=0xFD; //9600 baud rate
SCON=0x50;
TR1=1; //start timer
While (1) {
SerTx(‘Y’);
SerTx(‘E’);
SerTx(‘S’);
}
}
void SerTx(unsigned char x)
{
SBUF=x; //place value in buffer
While (TI==0); //wait until transmitted
TI=0;
}