Module 3Complete
Module 3Complete
Module 3Complete
8051 Timers/Counters:
Timers/Counters of 8051 Microcontroller ................................................... 2
Basic registers of Timer ................................................................................ 2
Timer – 0 registers .................................................................................. 2
Timer – 1 registers .................................................................................. 2
TMOD (Timer Mode) register .................................................................. 2
TCON (Timer Control) register ................................................................. 5
Mode 1 programming................................................................................... 6
Mode 2 programming................................................................................. 12
Counter Programming ................................................................................ 18
C/𝑇 bit in TMOD register ....................................................................... 19
Mode 1 Counter programming ................................................................... 19
Serial port Programming of 8051 ............................................................... 21
RxD and TxD pins in the 8051 ................................................................ 22
SBUF register......................................................................................... 22
SCON register (98h*) ............................................................................. 22
Setting the Baud rate ............................................................................ 24
3. 7. 5 Programming the 8051 to transfer data serially .................................. 25
3. 7. 6 Programming the 8051 to receive data serially ................................... 30
1
Module - 3 TCE, DSCE
3.1. Timers/Counters of 8051 Microcontroller
The 8051 microcontroller has two Timers/Counters, named Timer 0 (T0) and Timer 1 (T1). They
can be used either as timers to generate a required time delay or as a counter operation to count
external events happening. To operate these timers, knowledge about timer related SFRs are
necessary.
The 16 – bit registers of timer 0 is accessed as low byte and high byte. The low byte is called TL0
(Timer 0 low byte) and the high byte register is referred to as TH0 (Timer 0 high byte). These
registers can be accessed like any other registers with the help of MOV instruction with the help
of its direct SFR addresses.
3.2.2 Timer – 1 registers
Timer 1 is also 16 – bits, and its 16 – bit register is split into two bytes, referred to as TL1 (Timer 1
low byte) and TH1 (Timer 1 high byte). The timer registers for both timers are shown in Fig. 3.1
and 3.2.
3.2.3 TMOD (Timer Mode) register
Both timers (Timer 0 and Timer1) use the same register, called TMOD (Timer mode) register to
2
Module - 3 TCE, DSCE
set the timer in various modes of operation. TMOD, as shown in Fig 3.3, is an 8 – bit register, in
which lower 4 – bits are set aside 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 operations. This SFR
is accessed with the help of the byte address 89h.
MSB LSB
G C/T M1 M0 G C/T M1 M0
Timer 1 Timer 0
M1 M0 Mode Description
3
Module - 3 TCE, DSCE
Ex. 3.1 Find the values of TMOD to operate as timers in the following modes:
i). Mode 1 Timer 1
ii). Mode 2 Timer 0, Mode 1 Counter 1
iii). Mode 0 Timer 1
Sol: From Fig. 3.3,
i). TMOD is 0001 0000b = 10h.
ii). TMOD is 0101 0010b = 52h.
iii). TMOD is 0000 0000b = 00h.
̅ı̅m
̅ (Counter/T
C/T ̅e̅r):
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/𝑇̅= 0, it is used as a timer for delay generation. The
clock source for the time delay is the crystal frequency of the 8051.
Clock source for Timer:
Every timer needs a clock pulse to tick (increment). The source of the clock for the
timer operation (C/𝑇̅= 0), is the crystal (XTAL) attached to 8051. This means that the
size of the crystal frequency decides the speed at which the 8051 timer ticks. The
frequency of the timer is always 1 th of the frequency of the crystal attached to the
12
8051.
Timer
XTAL Frequency
Oscillator
÷ 12
Ex. 3.2 Find the timer’s clock frequency and its period for the various 8051 based
systems, with the following XTAL frequencies.
i). 12 MHz ii). 16 MHz iii). 11.0592 MHz
Sol: i). Timer Frequency = 1 * XTAL = 1 * 12 MHz= 1 MHz
12 12
So, Timer period (or) Timer Cycle (or) Machine cycle = 1 = 1 µs.
𝑇i𝑚𝑒𝑟 𝐹𝑟𝑒𝑞𝑢
4
Module - 3 TCE, DSCE
ii). For XTAL as 16 MHz,
Timer Freq = 1.33MHz & Timer Cycle = 0.75 µs
iii). For XTAL = 11.0592 MHz
Timer Freq = 921.6kHz & Timer Cycle = 1.085 µs
Gate:
The other bit of the TMOD register is the Gate (G) bit. Notice in the TMOD register of
Fig. 3.3 that both Timers 0 & 1 have gate bit. The purpose of this bit is explained
below:
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 timers in
the 8051 have both. The start and stop of the timer are controlled by way of software by
the Timer Run (TRX) bits (TR0 for Timer 0 and TR1 for Timer 1). So for starting the
timers with the help of only TRX pins are achieved by putting the Gate pin to ‘0’. This
way is referred as software control of timers. The hardware way of starting and
stopping the timers by an external source or with the help of a switch is achieved by
making Gate pin to ‘1’.
The bit pattern of Timer control register is shown in Fig. 3.4. Notice that, only the upper
nibble of TCON register have bits needed for timer operations. The Byte address of TCON
register is 88h. This register is a bit addressable SFR.
MSB LSB
5
Module - 3 TCE, DSCE
TF1 for Timer 1. The details of the bit pattern of TCON are shown below.
XTAL
÷ 12
Oscillator TH TL TF
6
Module - 3 TCE, DSCE
Steps to program in Mode 1:
To generate a time delay, using the timer’s mode 1, the following steps are taken.
1. Load the TMOD register indicating which timer (Timer 0 or Timer 1) is to be used
and timer in mode 1 is selected.
2. The 16 – bit initial values are loaded in TH and TL registers.
3. Start the timer, by making the timer run bit (TR) to 1.
4. Keep on monitoring the timer flag (TF) with JNB TFX, $ instruction, to see if it is
raised. Get out of the loop when TF becomes high.
5. Stop the timer and clear the timer flag bit.
6. Go back to step 2, to reload the Timer registers and repeat the process.
Ex. 3.3 Write an 8051 ALP to generate a square wave of 2 kHz frequency on Port pin P2.3.
Assume XTAL = 12 MHz. Use Timer0 in Mode 1 of operation.
Sol: Given XTAL = 12MHz.
1 Machine cycle = 1 * 12
⟹1 Machine cycle = 1 * 12 = 1µs. K𝑇𝐴𝐿
12𝑀
Timer 0 in Mode 1:
TMOD = 0000 0001 b = 01h
250µs
250µs
7
Module - 3 TCE, DSCE
MOV TMOD, #01h ;Timer 0 in Mode 1
Back: MOV TH0, #0FFh ; The 16 – bit initial values are loaded in Timer registers
MOV TL0, #06h
ACALL Delay
CPL P2.3 ;Complement Port pin P2.3
SJMP Back
Ex. 3.4 Assuming that XTAL = 11.0592 MHz, what 16 – bit value need to be loaded into
timer registers to have a delay of 5ms. Also write an 8051 ALP for Timer 1 to
create a pulse width of 5ms on P1.5
Sol: Given XTAL = 11.0592 MHz
⟹1 Machine cycle = 1 * 12 = 1.085µs.
Timer 1 in Mode 1:
TMOD = 0001 0000 b = 10h
The timer 1 in mode 1 should be configured to produce a delay of 5ms.
The number of timer cycles required to generate 5ms = 5𝑚 = 4608 timer cycles.
1.085µ
8
Module - 3 TCE, DSCE
ACALL Delay
CPL P1.5 ;Complement Port pin P1.5
SJMP Back
Ex. 3.5 Generate a square wave with an ON time of 3ms and an OFF time of 10ms on all
pins of port 0. Assume XTAL = 22 MHz. Use Timer 0 in Mode 1 operation.
3ms 10ms
All Bits of P0
Timer 0 in Mode 1:
TMOD = 0000 0001 b = 01h
On Time calculations: Off Time calculations:
Delay required = 3ms Delay required = 10ms
Timer cycles required = 3𝑚 = 5505cycles 10𝑚
Timer cycles required = = 22026cycles
0.545µ 0.545µ
TH0 TL0 TH0 TL0
hex hex
- 5505d EA 7F h - 22026d A9 F6 h
9
Module - 3 TCE, DSCE
MOV P0, #00h ;Make all bits of P0 as Low.
ACALL Delay
MOV TH0, #0EAh ;To generate ON Time, load EA 7Fh in timer registers.
MOV TL0, #7Fh
MOV P0, #0FFh ; Make all bits of P0 as high.
ACALL Delay
SJMP Back ;Repeat for reloading to get continuous square wave.
Ex. 3.6 For the following XTAL frequencies, calculate the Maximum delay generated
using 8051 mode 1 timer operations
i). 8 MHz ii). 11.0592 MHz iii). 22 MHZ
Sol: To produce Maximum delay in mode 1 timer operation, the 16 – bit initial value to be
loaded in timer registers be all zeros. i.e., 00 00h
The timer spends 65, 536 timer cycles (2^16) to overflow producing maximum delay
Sl. No XTAL Timer cycle Max. Delay
1. 8MHz 1.5µs 1.5µs * 65536 ≅ 98ms
2. 11.0592MHz 1.085µs 1.085µs * 65536 ≅ 71ms
3. 22MHz 0.545µs 0.545µs * 65536 ≅ 36ms
Ex. 3.7 Assuming XTAL = 22 MHz, write a program to generate a pulse train of 2 seconds on
pin P2.4. Use Timer 1 in mode 1.
Sol: Given XTAL = 22 MHz
⟹1 Machine cycle = 1 * 12 = 0.545µs.
22𝑀
10
Module - 3 TCE, DSCE
Timer 1 in Mode 1:
TMOD = 0001 0000 b = 10h
For a time period of 2 seconds, the
1 Sec
1 Sec
half period should be 1 second. From
Ex. 3.6, it is quite clear that the P2.4
11
Module - 3 TCE, DSCE
3.4 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 00h 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 TR0” for timer 0
and “SETB TR1” for timer 1.
3. Once the timer is started, it start to count up by incrementing the TL register. It
counts up until it reaches it limit of FFh. When it rolls over from FFh to 00h, it sets
high the TF flag.
4. When the TL register rolls from FFh to 00h, and TF is set to 1, TL is reloaded
automatically with its original value kept in TH register. To repeat the process, one
must clear the TF flag, and let it go without the need of programmer to reload the
initial value.
This makes mode – 2 an auto reload, in contrast with mode 1 in which the programmer
has to reload TH and TL registers.
XTAL
÷ 12
Oscillator TL TF
Reload
C/T = 0 TF goes high
TR
TH when FFh 00h
12
Module - 3 TCE, DSCE
5. Clear the TF flag
6. Go back to step 4, since mode 2 is auto reloaded.
Ex. 3.8 Assuming that XTAL is 22 MHz, write a program to generate a square wave of
frequency 5 kHz on pin P1.2. Use timer 1 in mode 2.
Sol: Given XTAL = 22MHz.
1 Machine cycle = 1 * 12
⟹ 1 Machine cycle = 1 * 12 =
22𝑀 K𝑇𝐴𝐿
0.545µs.
Timer 1 in Mode 2:
TMOD = 0010 0000 b = 20h
100µs
100µs
To produce a square wave of 5kHz,
assuming 50% duty cycle on pin P1.2
1 P1.2
TTot = = 200µs
5 𝑘𝐻𝑧
t
⟹ TON = TOFF = 𝑇𝑇𝑜𝑡 = 100µ
2
200µs
So, the pin P1.2 should be
Fig 3.10: Square wave of 5 kHz
complemented forever for 100µs once.
Delay required = 100µs
Timer cycles required = 100µ = 183 timer cycles.
0.545µ
So, - 183d should be loaded in TH1 register. The assembler will convert the negative value
to its appropriate 8 – bit value and loads into TH1.
MOV TMOD, #20h ; Timer 1 in mode 2
MOV TH1, # - 183 ;Load the initial value in TH1
SETB TR1 ; Start timer 1
Back: ACALL Delay
CPL P1.2
SJMP Back ; Repeat to get continuous square wave.
Delay: JNB TF1, Delay ;Wait till timer overflows, after 183 cycles, TF1 = 1
CLR TF1 ; Clear the overflow flag
RET
13
Module - 3 TCE, DSCE
Ex. 3.9 Assuming that timers are in mode 2 operation, find the value (in hex) loaded into
TH for each of the following cases
i). MOV TH1, # -200 ii). MOV TH0, # -60 iii). MOV TH1, # -3
Sol: Enter -200 in decimal mode and select hex, to get the 2’s complement value. Also
remember to take least 2 hex digits, since mode 2 is an 8 – bit timer operation.
i) -200d = 38h
ii) -60d = C4h
iii). -3d = FDh.
Ex. 3.10 For the following XTAL frequencies, calculate the Maximum delay generated
using 8051 mode 2 timer operations
i). 8 MHz ii). 11.0592 MHz iii). 22 MHZ
Sol: To produce Maximum delay in mode 2 timer operation, the 8 – bit initial value to be
loaded in timer registers be all zeros. i.e., 00h
The timer spends 256 timer cycles (2^8) to overflow producing maximum delay
Sl. No XTAL Timer cycle Max. Delay
1. 8MHz 1.5µs 1.5µs * 256 = 384µs
2. 11.0592MHz 1.085µs 1.085µs * 256 ≅ 278µs
3. 22MHz 0.545µs 0.545µs * 256 ≅ 140µs
Ex. 3.11 Assuming XTAL = 11.0592 MHz, write a program to generate a pulse train of 2 seconds
on pin P2.4. Use Timer 1 in mode 2.
Sol: Given XTAL = 11.0592 MHz
⟹1 Machine cycle = 1 * 12 = 1.085µs.
11.0592𝑀
Timer 1 in Mode 2:
TMOD = 0010 0000 b = 20h
For a time period of 2 seconds, the half period should be 1 second. From Ex. 3.10, it is quite
clear that the maximum delay possible using mode 2 timer with XTAL of 11.0592 MHz is
277µs, which is very less than the requires amount of 1 sec delay.
So, configure a delay of lesser than the max. delay, say 250µs, and repeat this delay for ‘n’ times
14
Module - 3 TCE, DSCE
to get the required amount of 1 sec.
1 sec = 250µs * n
⟹ n = 4000
Since, 4000 is > 8 – bits, this can be as a product of two 8 – bit numbers as follows:
4000 = 250 * 16
For a Delay of 250 µs:
Timer cycles required = 250µ = 230cycles
1.085µ
Delay: JNB TF1, $ ;Wait till Timer 1 overflows, thereby producing 250µs
CLR TF1 ;Reset the timer 1 Flag bit
RET
15
Module - 3 TCE, DSCE
CLR TF1
SJMP Back
Sol: i). Based on the value available in TH1, the timer spends 251 timer cycles before rolls over,
producing a delay of 251 * 1.085µ = 272µs.
So, TON = TOFF = 272µs.
TTOT = 272µ + 272µ = 544µs
The frequency of the square wave on pin P1.0 is 1 = 1.83 kHz.
544µ
ii). To get the smallest frequency possible, the delay should be of maximum. This can be
achieved by loading 00h in TH1 register as the initial value.
In this case, TON = TOFF = 256 * 1.085µ = 277.5µs.
TTOT = 277.5µ + 277.5µ = 555µs.
The frequency of the square wave on pin P1.0 is 1 = 1.80 kHz.
555µ
Ex. 3.13 Write an ALP to read input from port 1, complement it and output via port 2. Transfer
should be done once in 50ms, use timer 0 for delay generation and assume XTAL =
11.0592 MHz
Sol: Given XTAL = 11.0592 MHz
⟹1 Machine cycle = 1 * 12 = 1.085µs.
11.0592𝑀
TH0 TL0
hex
- 46083d 4B FD h
MOV TMOD, #01h ; Timer 0 in mode 1
MOV P0, #0FFh ;P0 as Input port
Back: MOV TH0, #4Bh ; 16 – bit initial value loaded in timer registers
MOV TL0, #0FDh
ACALL Delay
16
Module - 3 TCE, DSCE
MOV A, P1 ; Read port 1 and move to Acc
CPL A ; Complement A
MOV P2, A ; Output via port 2
SJMP Back ; go for next byte transfer.
Ex. 3.14 A switch ‘SW’ is connected to pin P1.2. Write an 8051 ALP to monitor the switch and
create the following frequencies on P1.7
i. When ‘SW’ = 0; F = 500 Hz
ii. When ‘SW’ = 1; F = 750 Hz.
Use timer 0, mode 1 for both of them. Assume XTAL = 11.0592 MHz.
Sol: Given XTAL = 11.0592 MHz
⟹1 Machine cycle = 1 * 12 = 1.085µs.
11.0592𝑀
So, the pin P1.7 should be complemented So, the pin P1.7 should be complemented
forever for 1ms once. forever for 0.667ms once.
Timer cycles for 1ms = 1𝑚 = 922cycles Timer cycles for 1ms = 0.667𝑚 = 615cycles
1.085µ 1.085µ
17
Module - 3 TCE, DSCE
MOV TMOD, #01h ; Timer 0 mode 1
SETB P1.2 ; Make Port P1.2 as input pin
Back: JNB P1.2, Fre_500Hz ; Read the switch position
Fre_750Hz: MOV TH0, # 0FDh ; Load the 16 – bit initial value for 750Hz
MOV TL0, # 99h
ACALL Delay
SJMP Last
Fre_500Hz: MOV TH0, # 0FCh ; Load the 16 – bit initial value for 500Hz
MOV TL0, # 66h
ACALL Delay
Last: CPL P1.7
SJMP Back ; Go back to check the status of the switch.
18
Module - 3 TCE, DSCE
̅ bit in TMOD register
C/T
Recall from the last section, that the C/𝑇̅ bit in the TMOD register decides the source of the
clock for the timer.
If C/𝑇̅= 0, Timer mode, the timer gets pulses from the crystal connected to the 8051.
If C/𝑇̅= 1, Counter mode, the timer gets pulses from outside the 8051.
̅ = 1, the counter counts up as pulses are fed from pins 14th and 15th of the 40 – pin
∴, when C/𝑇
DIP. These pins are called T0 (Timer 0 input) and T1 (Timer 1 input). Notice these pins belong to
port 3. In the case of Timer 0, when C/𝑇̅= 1, pin P3.4 provides the clock pulse and the counter
counts up for each of the clock pulse coming from that pin. Similarly, for timer 1, when C/𝑇̅= 1,
each clock pulse coming in from pin P3.5 makes the counter to count up.
Port 3 pins used for Timers 0 and 1
Pin Port Pin Function Description
14 P3.4 T0 Timer/Counter 0 External input
15 P3.5 T1 Timer/Counter 1 External input
MSB LSB
G C/T M1 M0 G C/T M1 M0
Timer 1 Timer 0
19
Module - 3 TCE, DSCE
Overflow Flag Overflow Flag
Timer 0 External input Timer 1 External input
P3.4 TH0 TL0 TF0 P3.5 TH1 TL1 TF1
C/T = 1
TR0 C/T = 1
TR1
a). Timer 0 with external input b). Timer 1 with external input
Ex. 3.14 Find the frequency of an input signal fed to pin P3.4. Use XTAL = 22MHz.
Sol: Since external pulse is fed to pin P3.4, the timer 0 should be configured as an event counter.
The frequency is the number of clock pulses per second duration. So Timer 1 should be
configured to generate a delay of 1 second. During this 1 second, Timer 0 is to run as a 16 –bit
event counter, as the input pulse is fed to P3.4. At the end of one sec, the values in TL0 and TH0
give the number of pulses that were received at pin p3.4 during this second. This gives the
frequency of the unknown signal.
TMOD = 0 0 0 1 0 1 0 1 b = 15h; T0 as Mode – 1 Event counter & T1 as Mode – 1 timer.
Given XTAL = 22MHz ⟹ 1 machine cycle = 0.545µs
Max. delay for timers in mode 1 = 216 machine cycle = 65 536 * 0.545µ = 35.71ms.
So, configure a delay of lesser than the max. delay, say 25ms, and repeat this delay for ‘n’ times
to get the required amount of 1 sec.
1 sec = 25ms * n
⟹ n = 40
For a Delay of 25 ms: TH1 TL1
hex
Timer cycles required = 25𝑚 = 45,872cycles - 45872d 4C D0 h
ORG 0000h
RPT: MOV TMOD, #15h ;Timer 0 as counter and Timer 1 as timer for 1 sec delay gen.
SETB P3.4 ; make P3.4 as input port.
MOV TL0, # 00h ;set the 16 – bit initial values in Timer 0 registers as 00 00h
MOV TH0, # 00h
SETB TR0 ;Start the Timer 0(Counter operation)
20
Module - 3 TCE, DSCE
MOV R0, # 40
Again: MOV TL1, # 0D0h ; Initialize the 16 – bit value in timer registers.
MOV TH1, # 4Ch
SETB TR1 ;Start timer 1
Wait: JNB TF1, $ ;Wait till timer 1 overflows producing 25ms delay
CLR TR1 ;Stop timer 1
CLR TF1 ;Clear timer 1 overflow flag
DJNZ R0, Again ;Repeat 25ms delay for 40 times.
MOV A, TL0
MOV P2, A ;Move TL0 to P2 and TH0 to P1 which is connected to some
MOV A, TH0 ;output devices
MOV P1, A
SJMP RPT ;Repeat this process
END
21
Module - 3 TCE, DSCE
3.7.1 RxD and TxD pins in the 8051
The 8051 has 2 pins that are used specially for transferring and receiving data serially. These two
pins are called TxD and RxD and are part of port 3 group (P3.0 and P3.1). Pins 11 of the 8051
(P3.1) is assigned to TxD and pin 10 (P3.0) is designated as RxD.
3.7.2 SBUF register
SBUF is an 8 – bit register used only for serial communication in the 8051. For a byte of data to
be transferred via TxD pin, it must be placed in SBUF. Similarly, SBUF holds the byte of the
data when it is received by the RxD pin of 8051. The moment a byte is placed in SBUF, it is
framed with the start bit before D0 bit and stop bit after D7 bit of the data and transferred serially
via TxD pin. Similarly, when bits are received serially via RxD pin, the 8051 de frames it by
eliminating the start and stop bits, and then places the received byte in SBUF
3.7.3 SCON register (98h*)
Fig 3.14 shows the SCON SFR, which is at the address 98h (a bit addressable SFR).
22
Module - 3 TCE, DSCE
SM0, SM1
SM0 and SM1 are D7 and D6 bits of the SCON register, respectively. These bits determine the
framing of data by specifying the number of bits per character, and the start and stop bits. They
take the following combinations:
D7
Goes out last Goes out first
Fig 3.15: Framing ASCII “A” (41h) with start and stop bit
More importantly, serial mode 1 allows the baud rate to be variable and is set by the timer 1 of
the 8051. In serial mode 1, 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 finally 1 stop bit.
SM2
SM2 is the D5 bit of SCON register. This bit enables the multiprocessor capability of the 8051
and for our discussion, set SM2 as ‘0’.
REN
The REN (receive enable) bit is D4 of the SCON register. The REN bit is also referred to as
SCON.4 since SCON is a bit addressable SFR.
when REN = 1: UART is enabled
23
Module - 3 TCE, DSCE
when REN = 0; UART is disabled.
So, by making REN as 1, 8051 can transfer and receive data serially via, TxD and RxD pins.
TB8 and RB8
TB8 (Transfer bit) and RB8 (Receive bit) are D3 and D2 bits of SCON register. We make these
bits as ‘0’, since it is used for serial modes 2 and 3.
TI
TI (Transmit interrupt) is the D1 bit of SCON register. This is an extremely important flag bit in
the SCON register. When the 8051 finishes the transfer of the 8 – bit character, it raises the TI
flag to indicate that UART is ready to transfer next character. The TI is raised by 8051 at the
beginning of the stop bit.
RI
RI (Receive interrupt) is the D0 bit of SCON register. This is another extremely important flag
bit in the SCON register. When the 8051 receives data serially via RxD, it get rid of start and
stop bits and places the byte in SBUF register. Then it raises the RI flag to indicate that a byte is
received serially. The RI is raised by 8051 at the halfway through the stop bit.
3.7.4 Setting the Baud rate
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 8051 transfers and receives data serially at many different baud rates. The baud rate in serial
mode 1 is variable and can be set by timer 1 in mode 2 operation.
As shown in fig 3.16, the 8051 divides the crystal frequency by 12 to get the machine cycle
frequency. In the case of XTAL as 11.0592 MHz, the machine cycle frequency is 921.6 kHz. The
8051 serial communication UART circuitry divides the machine cycle frequency by 32 before
given to timer 1 to set the baud rate. Therefore, 921.6 kHz divided by 32 gives 28, 800 Hz. This
frequency can be divided by a value to get compatible baud rates. For instance, if 9600 baud rate
is needed, -3d should be loaded to TH1meaning dividing the UART frequency by 3 times.
24
Module - 3 TCE, DSCE
Baud Rate TH1 (decimal) TH1 (Hex)
9600 -3 FD
4800 -6 FA
2400 -12 F4
1200 -24 E8
Ex. 3.15 With XTAL = 11.0592 MHz, find the TH1 value needed to have the following baud
rates.
a). 9600 b). 4800 c). 2400
Sol: With XTAL = 11.0592 MHz,
⇒ Machine cycle frequency = K𝑇𝐴𝐿 = 11.0592𝑀 = 921.6 kHz
12 12
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 in mode 2
(8 – bit auto reload) for setting the baud rate.
2. The TH1 is loaded with the one of the values in the above table to set the baud rate for
serial data transfer.
3. The SCON is loaded with 50h, indicating serial mode 1, where an 8 – bit data is framed
with start and stop bits.
4. TR1 is set to 1 to start Timer 1.
5. The character byte to be transferred serially is placed in SBUF register.
6. The TI flag is monitored with the use of JNB TI, $ instruction to check whether all the 8
– bits in the character is completely transferred.
7. Clear TI flag and repeat step 5 for next transfer.
25
Module - 3 TCE, DSCE
Ex. 3.16 Write a program to transfer a letter ‘Y’ serially at 9600 baud rate continuously,
and also send a letter ‘N’ through port 0, which is connected to a display device.
Sol Here one byte of data (ASCII value
‘Y’
of ‘Y’, which is 59h) is transmitted P3.1
(TX
serially via TxD pin (P3.1), and )
another is transmitted in parallel 8051
‘N’
(ASCII value of ‘N’, which is 4Eh) P0 DISPLAY
via P0 pins.
Note: relate the ALP written below with the step sequence stated above in section3.7.
3.7.4
MOV TMOD, # 20h 5
; Timer 1, Mode 2 (Auto reload operation)
MOV TH1, # -3 ; 9600 Baud rate
MOV SCON , # 50h ;Serial mode 1, 8 – bit UART with start and stop bits
SETB TR1 ;Start timer 1
Again: MOV SBUF, # ’Y’ ;place the character in SBUF to transmit serially
Here: JNB TI, Here ;Wait till all the bits in the character is transferred
CLR TI ; Clear the TI flag for next serial transfer
MOV P0, # ’N’ ; Move ‘N’ to P0 for parallel transfer
SJMP Again ; Repeat this process for ever.
Ex. 3.17 Take data in through ports 0, 1, and 2, one after the other and transfer the data
serially, continuously at a baud rate of 4800.
Sol: MOV TMOD, # 20h ; Timer 1, Mode 2 (Auto reload operation)
MOV TH1, # -6 ; 4800 Baud rate
MOV SCON, # 50h ;Serial mode 1, 8 – bit UART with start and stop bits
MOV P0, # 0FFh ;Make P0 as an input port
MOV P1, # 0FFh ;Make P1 as an input port
MOV P2, # 0FFh ;Make P2 as an input port
SETB TR1 ;Start timer 1
Rpt: MOV A, P0 ; Read P0 pins
ACALL Send ; Call the subroutine
MOV A, P1 ; Read P1 pins
26
Module - 3 TCE, DSCE
ACALL Send ; Call the subroutine
MOV A, P2 ; Read P2 pins
ACALL Send ; Call the subroutine
SJMP Rpt Repeat this process for ever
; Transferring data serially – subroutine
Send: MOV SBUF, A ;place the character in SBUF to transmit serially
Here: JNB TI, Here ;Wait till all the bits in the character is transferred
CLR TI ; Clear the TI flag for next serial transfer
RET ; Return to the main program
Ex. 3.18 Write an 8051 ALP to transmit serially the message “Welcome” at a baud rate of
9600, and assume XTAL freq as 11.0592 MHz
Sol: MOV TMOD, # 20h ; Timer 1, Mode 2 (Auto reload operation)
MOV TH1,# -3 ; 9600 Baud rate
MOV SCON, #50h ;Serial mode 1, 8 – bit UART with start and stop bits
SETB TR1 ;Start timer 1
MOV DPTR, # Msg ;Load pointer for the message
Rpt: CLR A
MOVC A, @A+DPTR ;Get the character
JZ Last ; If last character, get out
ACALL Send ; Otherwise call the subroutine
INC DPTR ; Point the next character
SJMP Rpt ; Repeat the process
Last: SJMP $
Msg: DB “Welcome” 0 ; Define Byte the message with last character as null
; Transferring data serially – subroutine
Send: MOV SBUF, A ;place the character in SBUF to transmit serially
Here: JNB TI, Here ;Wait till all the bits in the character is transferred
CLR TI ; Clear the TI flag for next serial transfer
RET ; Return to the main program
27
Module - 3 TCE, DSCE
Ex. 3.19 Write an 8051 ALP to transmit serially the message “SWITCH ON” or
“SWITCH OFF”, depending on the status of the simple switch connected to pin
P1.2. Use 2400 Baud rate, and assume XTAL freq as 11.0592 MHz
Sol
SW
SW = 1; “SWITCH ON”
P1.2 P3.1
8051 (TX SW = 0; “SWITCH OFF”
)
28
Module - 3 TCE, DSCE
Ex. 3.20 Write a program in assembly language for 8051 to send 2 messages “Normal
Speed” and “High Speed” to the serial port. Assuming that a SW is connected to
pin P2.0, monitor its status and set the baud rate as follows:
when SW = 0; MSG1 at 4800 Baud rate
when SW = 1; MSG2 at 9600 Baud rate
Sol: VCC
29
Module - 3 TCE, DSCE
SJMP Rpt ; Read the switch position
(TX
)
30
Module - 3 TCE, DSCE
Msg2: DB “Normal Speed” 0
; Transferring data serially – subroutine
Send: MOV SBUF, A ;place the character in SBUF to transmit serially
Here: JNB TI, Here ;Wait till all the bits in the character is transferred
CLR TI ; Clear the TI flag for next serial transfer
RET ; Return to the main program
Ex. 3.21 Write an 8051 ALP to send 2 different strings to the serial port. Assuming that a
SW is connected to pin P2.5, monitor its status and make a decision as follows:
when SW = 0; Send your first name
when SW = 1; Send your last name;
Assume XTAL = 11.0592 MHz, and a baud rate of 9600.
Sol: VCC
SW
SW = 0; “First name”
P2.5 P3.1
8051 (TX
) SW = 1; “Last name”
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 in mode 2
(8 – bit auto reload) for setting the baud rate.
2. The TH1 is loaded with the one of the values in the above table to set the baud rate.
3. The SCON is loaded with 50h, indicating serial mode 1, where an 8 – bit data is framed
with start and stop bits.
4. TR1 is set to 1 to start Timer 1.
5. The RI flag is monitored with the use of JNB RI, $ instruction to check whether all the 8
– bits in the character is completely received.
31
Module - 3 TCE, DSCE
6. When RI is raised, SBUF has the byte. Its contents are moved into a safe place, like
accumulator.
7. Clear RI flag and repeat step 5 to receive next character.
Ex. 3.22 Write an 8051 ALP to receive the data which has been sent in serial form and
sent it out to port 0 in parallel form. Also save the data at RAM loc. 60h.
Sol:
‘Y’
R P3.0
x
8051
Y
P0 DISPLAY
60h Y
Let the data received via RxD pin is the ASCII value of character ‘Y’ (59h). When the
8051 receives data serially via RxD, it gets rid of start and stop bits and places the byte in
SBUF register. Then it raises the RI flag to indicate that a byte is received serially. The
below program demonstrates this theory of receiving the data serially.
MOV TMOD, # 20h ; Timer 1, Mode 2 (Auto reload operation)
MOV TH1, # -3 ; 9600 Baud rate
MOV SCON, # 50h ;Serial mode 1, 8 – bit UART with start and stop bits
SETB TR1 ;Start timer 1
Wait: JNB RI, Wait ;Wait till all the bits in the character is received
CLR RI ;RI flag is cleared
MOV A, SBUF ; Received data, which is available in SBUF, is moved to A
MOV P0, A ; Send to P0 in parallel & and store in 60h internal RAM
MOV 60h, A
SJMP $
Ex. 3.23 Assume that the serial port is connected to the COM port of the IBM PC, and on
the PC we are using the hyper Terminal program to send and receive data serially.
P1 and P2 of the 8051 are connected to LEDs and switches, respectively. Write an
8051 program to
a). Send to the PC, the message “We are READY”,
32
Module - 3 TCE, DSCE
b). Receive any data sent by the PC and put it on the LEDs connected to P1, and
c). Get data on switches connected to P2 and sent it to PC serially.
The program should perform part (a) once, but parts (b) and (c) continuously. Use
4800 baud rate.
Sol:
LEDs
To PC P1
TxD
8051
From
RxD SW
COM Port P2
33
Module - 3 TCE, DSCE
RET ; Return to the main program
; Receiving data serially – subroutine
Recev: JNB RI, Recev ;Wait till all the bits in the character is received
MOV A, SBUF ;RI flag is cleared
CLR RI ; Received data, which is available in SBUF, is moved to A
RET ; Return to the main program
34
Module - 3 TCE, DSCE