+ Module 4 - Timers - Serial Communication - New
+ Module 4 - Timers - Serial Communication - New
Timer Programming
The 8051 has two timers: Timer 0 and Timer 1. They can be used either as timers or as event counters.
These registers can be accessed like any other register, such as A, B, RO, Rl, R2, etc.
For example, the instruction “MOV TL0 , #4FH” moves the value 4FH into TL0,
Timer 1 Registers
Timer I is also 16 bits, and its 16-bit register is split into two bytes, referred to as TLl (Timer I low byte) and
TH1 (Timer 1 high byte). These registers are accessible in the same way as the registers of Timer 0.
TMOD (timer mode) register
Both timers 0 and 1 use the same register, called TMOD, to set the various timer operation modes. TMOD is
an 8-bit register in which the lower 4 bits are used for Timer 0 and the upper 4 bits for Timer 1. In each case,
the lower 2 bits are used to set the timer mode and the upper 2 bits to specify the operation. The Bit format of
TMOD register is as shown below.
M1, MO
M0and M 1 selects the timer mode. There are three modes: 0, 1, and 2.
Mode 0 is a 13-bit timer,
Mode 1 is a 16-bit timer, and
Mode 2 is an 8-bit timer.
C/T (clock/timer)
This bit in the TMOD register is used to decide whether the timer is used as a delay generator or an event
counter. 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..
Example -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 values from hex to binary. From Figure 9-3 we have:
1. TMOD = 00000001, mode 1 of Timer 0 is selected.
2. TMOD = 00100000, mode 2 of Timer 1 is selected.
1. TMOD = 00010010, mode 2 of Timer 0, and mode 1 of
Timer 1 are selected.
Clock source for timer
The crystal frequency attached to the 8051 is the source of the clock for the timer. This means that the crystal
frequency attached to the 8051 decides the speed at which the 8051 timer ticks. The frequency for the timer is
always 1 / 12th the frequency of the crystal attached to the 8051.
Example -2
GATE
The other bit of the TMOD register is the GATE bit. Every timer has a means of starting and stopping. Some
timers do this by software, some by hardware, and some have both software and hardware controls.
The start and stop of the timer are controlled by way of software by the TR (timer start) bits TR0 and
TR1. This is achieved by the instructions “SETB TR1″ and “CLR TR1″ for Timer 1, and “SETB TR0” and
“CLR TR0” for Timer 0. The SETB instruction starts it, and it is stopped by the CLR instruction. These
instructions start and stop the timers as long as GATE = 0 in the TMOD register.
The hardware way of starting and stopping the timer by an external source is achieved by making
GATE = 1 in the TMOD register.
Example - 3
Find the value for TMOD if we want to program Timer 0 in mode 2, use 8051 XTAL for the clock source,
and use instructions to start and stop the timer.
Mode 1 programming
The following are the characteristics and operations of mode 1:
1. It is a 16-bit timer; therefore, it allows values of 0000 to FFFFH to be loaded into the timer’s
registers 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
“SETB TR0” for Timer 0 and “SETB TR1″ for Timer 1.
3. After the timer is started, it starts to count up. It counts up until it reaches its limit of FFFFH. When
it rolls over from FFFFH to 0000, it sets high a timer flag. This timer flag can be monitored. When
this timer flag is raised, one option would be to stop the timer with the 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 the registers TH and
TL must be reloaded with the original value, and TF must be reset to 0.
Example -4
In the following program, we are creating a square wave of 50% duty cycle (with equal portions high and
low) on the PI.5 bit. Timer 0 is used to generate the time delay. Analyze the program.
Solution:
To repeat the process, we must reload the TL and TH registers and start the timer again.
Example -5
In Example 4, calculate the amount of time delay in the DELAY subroutine generated by the timer. Assume
that 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.6 kHz = 1.085 (is Timer 0 counts up each 1.085 us
resulting in delay = number of counts x 1.085 us.
The number of counts for the rollover is FFFFH – FFF2H = ODH (13 decimal). However, we add
one to 13 because of the extra clock needed when it rolls over from FFFF to 0 and raises the TF flag.
This gives 14 x 1.085 us = 15.19 us for half the pulse. For the entire period T = 2 x 15.19 (as = 30.38
(is gives us the time delay generated by the timer.
Example -6
In Example 6, calculate the frequency of the square wave generated on pin PI. 5.
Solution:
In the time delay calculation of Example -5, we did not include the overhead due to instructions in the loop.
To get a more accurate timing, we need to add clock cycles due to the instructions in the loop.
Example -7
Find the delay generated by Timer 0 in the following code, using both of the methods. Do not include the
overhead due to instructions.
1. (FFFF-B83E + 1) = 47C2H= 18370 in decimal and 18370 x 1.085 fis= 19.93145 ms.
2. Since TH – TL = B83EH = 47166 (in decimal) we have 65536 – 47166 = 18370. This means that the
timer counts from B83EH to FFFFH.. This plus rolling over to 0 goes through a total of 18370 clock
cycles, where each clock is 1.085 \ls in duration. Therefore, we have 18370 x 1.085 (is = 19.93145 ms as
the width of the pulse.
Example -8
The following program generates a square wave on pin PL5 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.
In the above program notice the target of SJMP. In mode 1, the program must reload the TH, TL register
every time if we want to have a continuous wave. Now the calculation. Since FFFFH – 7634H = 89CBH + 1
= 89CCH and 89CCH = 35276 clock count. 35276 x 1.085 us = 38.274 ms for half of the square wave. The
entire square wave length is 38.274 x 2 = 76.548 ms and has a frequency = 13.064 Hz.
Also notice that the high and low portions of the square wave pulse are equal. In the above calculation, the
overhead due to all the instructions in the loop is not included.
Finding values to be loaded into the timer
To calculate the values to be loaded into the TL and TH registers
Assuming XTAL = 11.0592 MHz from we can use the following steps for finding the TH, TL registers’
values.
1. Divide the desired time delay by 1.085 us.
2. Perform 65536 – n, where n is the decimal value we got in Step 1.
3. Convert the result of Step 2 to hex, where yyxx is the initial hex value to be loaded into the timer’s
registers.
4. Set TL = xx and TH = yy
Example -9
Assume that XTAL = 11.0592 MHz. What value do we need to load into the timer’s registers if we want to
have a time delay of 5 ms (milliseconds)? Show the program for Timer 0 to create a pulse width of 5 ms on
P2.3.
Example -10
Assuming that XTAL = 11.0592 MHz, write a program to generate a square wave of 2 kHz frequency on pin
PI .5.
Solution:
This is similar to Example 9-10, except that we must toggle the bit to generate the square wave. Look at the
following steps.
1. T = 1 / f = 1 / 2 kHz = 500 us the period of the square wave.
2. 1/2 of it for the high and low portions of the pulse is 250 us.
3. 250 us / 1.085 us = 230 and 65536 – 230 = 65306. which in hex is FF1AH.
4. TL = 1AH and TH = FFH. all in hex. The program is as follows.
Mode 2 programming
The following are the characteristics and operations of mode 2.
1. It is an 8-bit timer; therefore, it allows only values of 00 to FFH to be loaded into the timer’s
register TH.
2. After TH is loaded with the 8-bit value, the 8051 gives a copy of it to TL. Then the timer must
be started. This is done by the instruction “SETB TRO” for Timer 0 and “SETB TR1 1‘ for
Timer 1. This is just like mode 1.
3. After the timer is started, it starts to count up by incrementing the TL registers. It counts up
until it reaches its limit of FFH. When it rolls over from FFH to 00, it sets high the TF (timer
flag). If we are using Timer 0, TFO goes high; if we are using Timer 1, TF1 is raise
4. When the TL register rolls from FFH to 0 and TF is set to 1, TL is reloaded automatically
with the original value kept by the TH register. To repeat the process, we must simply clear
TF and let it go without any need by the programmer to reload the original value. This makes
mode 2 an auto-reload, in contrast with mode 1 in which the programmer has to reload TH
and TL
It must be emphasized that mode 2 is an 8-bit timer. However, it has an auto-reloading capability. In auto-
reload, TH is loaded with the initial count and a copy of it is given to TL.
Steps to program in mode 2
1. To generate a time delay using the timer’s mode 2, take the following steps.
2. Load the TMOD value register indicating which timer (Timer 0 or Timer 1) is to be used, and select
the timer mode (mode 2).
3. Load the TH registers with the initial count value.
4. Start the timer.
5. 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.
6. Clear the TF flag.
7. Go back to Step 4, since mode 2 is auto-reload
Example 11
Assuming that XTAL = 11.0592 MHz. find (a) the frequency of the square wave generated on pin P 1.0
in the following program, and (b) the smallest frequency achievable in this program, and the TH value
to do that.
Solution:
1. First notice the target address of SJMP. In mode 2 we do not need to reload TH since it is auto-
reload. Now (256 – 05) x 1.085 us = 251 x 1.085 us = 272.33 us is the high portion of the pulse.
Since it is a 50% duty cycle square wave, the period T is twice that; as a result T = 2 x 272.33 us
= 544.67 us and the frequency = 1.83597 kHz.
2. To get the smallest frequency, we need the largest T and that is achieved when TH = 00. In that
case, wehave T = 2 x 256 x 1.085 us = 555.52 us and the frequency = 1.8 kHz.
Program -12
8051 Serial Communication
When a microcontroller communicates with the outside world, it provides the data in byte-
sized chunks. In some cases, such as printers, the information is simply taken from the 8-bit data
bus and presented to the 8-bit data bus of the printer. This can work only if the cable is not too long,
since long cables diminish and even distort signals. Furthermore, an 8-bit data path is expensive.
For these reasons, serial communication is used for transferring data between two systems located
at distances of hundreds of feet to millions of miles apart. Fig(1) shows serial versus parallel data
transfers. The fact that serial communication uses a single data line instead of the 8-bit data line of
parallel communication. For serial data communication to work, the byte of data must be converted
to serial bits using a parallel-in-serial-out shift register, then it can be transmitted over a single data
line. At the receiving end there must be a serial-in-parallel-out shift register to receive the serial data
and pack them into a byte.
When the distance is short, the digital signal can be transferred as it is on a simple wire and
requires no modulation. However, for long-distance data transfers, serial data communication
requires a modem to modulate ( convert to 0s and 1s to audio tones) and demodulate ( converting
from audio tones to 0s and 1s).
Serial data communication uses two methods, asynchronous and synchronous. The
synchronous method transfers a block of data(characters) at a time, while the asynchronous method
transfers a single byte at a time. There are special IC’s made by many manufacturers for serial data
communications. These chips are commonly referred to as UART (universal asynchronous receiver
transmitter) and USART (universal synchronous asynchronous receiver transmitter).
Fig(1)
In data transmission if the data can be transmitted and received, it is a duplex transmission.
This is in contrast to simplex transmission such as with printers, in which the computer only sends
data. Duplex transmissions can be half o full duplex, depending on whether or not the data transfer
can be simultaneous. If data is transmitted one way at a time, it is referred to as half duplex. If the
data can go both ways at the same time, it is full duplex. Full duplex requires two wire conductors
for the data lines, one for transmission and one for reception, in order to transfer and receive data
simultaneously.
The data coming in at the receiving end of the data line in a serial data transfer is all 0s and
1s, it is difficult to make sense of the data unless the sender and receiver agree on a set of rules, a
protocol, on how the data is packed, how many bits constitute a character, and when the data begins
and ends.
The rate of data transfer in serial data communication is stated in bps ( bits per second).
Another widely used terminology for bps is baud rate. The baud rate is the modem terminology and
s defined as the number of signal changes per second.
RS 232 standards
RS 232 is an interfacing standard and is the most widely used serial I/O interfacing standard.
Since the standard was set long before the advent of the TTL logic family, its input and output
voltage levels are not TTL compatible. For this reason, to connect any RS232 to a microcontroller
system we must use voltage converters such as MAX232 to convert the TTL logic levels to RS232
voltage levels, and vice versa. MAX232 IC’s are commonly referred to as line drivers.
RS232 pins
RS232 cable commonly referred to as the DB-25 connector has 25 pins. Since not all the
pins are used in PC cables, IBM introduced the DB-9 version of the serial I/O standard, which uses
9 pins only, as shown in Table 1. The DB-9 pins are shown in Fig(3).
Current terminology classifies data communication equipment as DTE (data terminal equipment)
or DCE (data communication equipment). DTE refers to terminals and computers that send and
receive data, while DCE refers t communication equipment, such as modems, that responsible
for transferring the data. The simplest connection between a PC and microcontroller requires a
minimum of three pins, TxD, RxD and ground as shown in fig(4).
DTE DTE
TxD TxD
RxD RxD
ground
MAX232
Since the RS232 is not compatible with today’s microprocessors and microcontrollers, we
need a voltage converter(line driver) to convert the RS232’s signals to TTL voltage levels that will
be acceptable to the 8051’s TxD and RxD pins. One example of such converter is MAX232. It
converts from RS232 voltage levels to TTL voltage levels and vice versa. One advantage of the
MAX232 is that it uses a +5 V power source which is the same as the source voltage for the 8051.
The MAX232 has two sets of line drivers for transferring and receiving data as shown in
fig(5). The line drivers used for TxD are called T1 and T2, while the line drivers for RxD are
designated as R1 and R2. MAX232 requires four capacitors ranging from 1 to 22 µF. The most
widely used value for these capacitors is 22 µF.
Fig(5)
8051 SERIAL PORT PROGRAMMING
With XTAL = 11.0592 MHz, find the TH1 value needed to have the following baud
rates. (a) 9600, (b) 2400 and (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 to set baud rate.
Notice that dividing 1/12 of the crystal frequency by 32 is the default value upon activation of the 8051 RESET
pin.
SBUF REGISTER
SBUF is an 8-bit register used solely for serial communication in the 8051. For a byte of data to
be transferred via the TxD line, it must be placed in the SBUF register. Similarly, SBUF holds the byte of
data when it is received by the RxD line. SBUF can be accessed like any other register in the 8051.
Example: MOV SBUF, #‟D‟ ; load SBUF=44h, ASCII for „D‟
MOV SBUF, A ; copy accumulator into SBUF
MOV A, SBUF ; copy SBUF into accumulator
SMO, SM1:
They determine the framing of data by specifying the number of bits per character, and the start and stop bits.
There are four modes of operation out of which only mode 1 is of interest to us.
When mode 1 is chosen, the data framing is 8 bits, 1 start bit and 1 stop bit which makes it compatible with the
com ports of PC. This mode allows baud rate to be variable and is set by timer 1 in mode 2. In this mode for
each character a total of 10 bits are transferred where the first bit is the start bit, followed by 8 bits of data and 1
stop bit.
SM2:
This enables the multiprocessing capability of the 8051. For our application this bit is set to 0 since we are not
using the 8051 in multiprocessor environment.
REN:
The REN (receive enable) bit is also referred to as SCON.4. When this bit is high, it allows the 8051 to receive
data on the RxD pin of the 8051. For transferring and receiving data serially this bit should be set to 1. If the bit
is reset to 0, the receiver is disabled. It is used to block any serial data reception. It can be set using SETB
SCON.4 and cleared using CLR SCON.4.
TB8:
TB8 (transfer bit 8) is used for serial modes 2 and 3. Since we use only mode 1, this bit is reset to 0.
RB8:
RB8 (receive bit 8) gets a copy of stop bit when an 8 bit data is received in mode 1 operation. It is used in modes
2 and 3 hence it is reset to 0.
TI:
When the 8051 finishes the transfer of 8 bit character, it raises the TI (transmit interrupt) flag to indicate that it is
ready to transmit another byte. It is raised at the beginning of the stop bit.
RI:
When the 8051 receives data serially via RxD, it gets rid of the start and stop bits and places the byte in the SBUF
register. Then it raises the RI (receive interrupt) flag bit to indicate that a byte has been received and should be
picked up before it is lost. It is raised halfway through the stop bit.
In programming the 8051 to transfer character bytes serially, the following steps must be taken.
1. The TMOD register is loaded with the value 20H, indicating the use of Timer 1 in mode 2 to
set the baud rate.
2. TH1 is loaded with one of the values in Table(2) to set the baud rate.
3. The SCON register is loaded with the value 50H, indicating serial mode 1, where 8-bit data is
framed with start and stop bits and receive enable is turned on.
4. TR1 is set to 1 to start Timer 1.
5. RI is cleared with the “CLR RI” instruction.
6. The RI flag bit is monitored with the use of the instruction “JNB RI, xx” to see if an entire
character has been received yet.
7. When RI is raised, SBUF has the byte. Its contents are moved into safe place.
8. To receive the next character, go to Step 5.
Write a program for the 8051 to receive bytes of data serially, and put them in P1,
set the baud rate at 4800, 8-bit data, and 1 stop bit.
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
HERE: JNB RI, HERE ; wait for char to come in
MOV A, SBUF ; saving incoming byte in
A
MOV P1, A ; send to port 1
CLR RI ; get ready to receive next
byte SJMP HERE
Write a program for the 8051 to transfer “YES” serially at 9600 baud, 8-bit data,
1 stop bit, do this continuously
Solution:
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