0% found this document useful (0 votes)
84 views76 pages

BEC405A Module3

The document outlines the vision and mission of the Electronics and Communication Engineering department, focusing on providing quality education and preparing students for industry challenges. It details the educational objectives and specific outcomes related to the 8051 microcontroller, including programming timers and serial communication. The section on programming 8051 timers explains the operation modes, register configurations, and practical examples for generating time delays and square waves.

Uploaded by

Veena sk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views76 pages

BEC405A Module3

The document outlines the vision and mission of the Electronics and Communication Engineering department, focusing on providing quality education and preparing students for industry challenges. It details the educational objectives and specific outcomes related to the 8051 microcontroller, including programming timers and serial communication. The section on programming 8051 timers explains the operation modes, register configurations, and practical examples for generating time delays and square waves.

Uploaded by

Veena sk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 76

MICROCONTROLLER BEC405A

Module 3:
8051 TIMERS & SERIAL
PORT

• Dr. VEENA S K
VISION OF THE DEPARTMENT

To be recognized by the society at


large as offering Value based Quality
Education to groom the next
generation entrepreneurs, leaders
and researchers in the field of
Electronics and Communication to
meet the challenges at global level.

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 2


MISSION OF THE DEPARTMENT
• To groom the students with strong foundations of
Electronics and Communication Engineering and to
facilitate them to pursue higher education and research.
• To educate and prepare the students to be competent to face
the challenges of the industry/society and /or to become
successful entrepreneurs.
• Toprovide ethical and value-based education by promoting
activities addressing the societal needs.
• Enable students to develop skills to solve complex
technological problems of current times and also provide a
framework for promoting collaborative and multidisciplinary
activities.

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 3


Program Educational Objectives
(PEOs)
• Be able to have a successful career in dynamic
industry that is global, multidisciplinary, and
evolving.
• Solve problems, design and innovate while
they work individually or in teams with sense
of professional ethics and social responsibility.
• Communicate effectively and manage
resources skillfully as members and leaders of
the profession

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 4


Program Specific Outcomes
(PSOs)
• An ability to apply the basic concepts of
engineering science into various areas of
Electronics Communication Engineering.
• An ability to solve complex Electronics and
Communication Engineering problems, using
state of the art hardware and software tools,
along with analytical skills to arrive at cost
effective and efficient solutions.

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 5


MICROCONTROLLER BEC405A

Module 3:
8051 TIMERS & SERIAL
PORT
COs
RBT
Module – 3 Level
address
ed

Timers/Counters & Serial port programming:


Basics of Timers & Counters,
Data types & Time delay in the 8051 using C,
Programming 8051 Timers, Mode 1 & Mode 2
Programming,
Counter Programming (Assembly Language only). L1, CO2
(Text book 2- 3.4, Text book 1- 7.1, 9.1,9.2) L2, ,
Basics of Serial Communication, L3 CO3
8051 Connection to RS232,
Programming the 8051 to transfer data serially & to
receive data serially using C.
( Text book 2- 3.5, Text book 1- 10.1,10.2,10.3 except
assembly language programs, 10.5)
Text Books:
1. “The 8051 Microcontroller and Embedded Systems – using assembly and C”,
Muhammad Ali Mazidi and Janice Gillespie Mazidi and Rollin D. McKinlay; PHI,
2006 / Pearson, 2006.
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 7
PROGRAMMING 8051 TIMERS

• 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
• Both Timer 0 and Timer 1 are 16 bits wide
• Since 8051 has an 8-bit architecture, each 16-bits timer is
accessed as two separate registers of low byte and high byte

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 8


PROGRAMMING 8051 TIMERS
• Timer 0 and Timer 1 registers
• low byte register is called TL0/TL1 (Timer 0/1 low byte) and
the high byte register is referred to as TH0 /TH1 (Timer 0/1
high byte)
• can be accessed like any other register, such as A, B, R0, R1,
R2, etc.
MOV TL0, #4 FH; moves the value 4FH into TL0
MOV R5, TH0; saves TH0 (high byte of Timer 0) in R5
Figure 9–1
Timer 0 Registers

Figure 9–2
Timer 1 Registers

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 9


PROGRAMMING 8051 TIMERS
TMOD Register
TMOD (timer mode) register
• Timers 0 and 1 use TMOD register to set operation modes
(only learn Mode 1 and 2)
• TMOD is a 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

GATE C/T M1 M0 GATE C/T M1 M0

Timer1 Timer0
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 10
PROGRAMMING 8051 TIMERS
TMOD Register

Figure 9–3
Microcontroller BEC405A-Dr. VEENATMOD
S K. Register 01/25/2025 11
PROGRAMMING 8051 TIMERS
TMOD Register

• Clock source for timer


• timer needs a clock pulse to tick
• if C/T = 0, the crystal frequency attached to the 8051 is the
source of the clock for the timer
• frequency for the timer is always 1/12th the frequency of the
crystal attached to the 8051
• XTAL = 11.0592 MHz allows the 8051 system to communicate
with the PC with no errors
• In our case, the timer frequency is 1MHz since our crystal
frequency is 12MHz

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 12


PROGRAMMING 8051 TIMERS
TMOD Register
Example 9-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.
(c) TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are selected.

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, with the
The clock source for crystal frequency 11.0592 MHz when C/T bit of TMOD is 0.
the time delay is the
crystal frequency
XTAL
of the 8051 oscillator
÷12

1/12  11.0592 MHz = 921.6 kHz;


T = 1/921.6 kHz = 1.085 us
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 13
PROGRAMMING 8051 TIMERS
TMOD Register
• Timers of 8051 do starting and stopping by either software or
hardware control
• In using software to start and stop the timer where GATE=0
• The start and stop of the timer are controlled by way of software by
the TR (timer start) bits TR0 and TR1
• 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
• Timer 0, mode 2
•C/T = 0 to use
Find the value for TMOD if we want to program timer 0 in mode 2, use 8051
XTAL clock source
• gate = 0 to use XTAL for the clock source, and use instructions to start and stop the timer.
internal (software) start
and stop method.
TMOD = 0000 0010
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 14
PROGRAMMING 8051 TIMERS
Mode 1
The following are the characteristics and operations of
mode1:
• It is a 16-bit timer; therefore, it allows value of 0000 to
FFFFH to be loaded into the timer’s register TL and
TH
• 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
• After the timer is started, it starts to count up
• It counts up until it reaches its limit of
FFFFH

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 15


PROGRAMMING 8051 TIMERS
Mode 1
• When it rolls over from FFFFH to 0000, it sets high a
flag bit called TF (timer flag)
• Each timer has its own timer flag: TF0 for timer 0,
and TF1 for timer 1
• 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
• 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
oscillator ÷12 TF

TF goes high Overflow


flag 16
Microcontroller BEC405A-Dr.
C/T = 0VEENA S K.
TR when FFFF  0 01/25/2025
PROGRAMMING 8051 TIMERS
Mode 1
To generate a time delay
1. Load the TMOD value register indicating which
timer (timer 0 or timer 1) is to be used and which
timer mode (0 or 1) is selected
2. Load registers TL and TH with initial count value
3. Start the timer (TRx=1)
4. Keep monitoring the timer flag (TF) with the JNB
TFx,target instruction to see if it is raised
5. Get out of the loop when TF becomes high
6. Stop the timer(TRx=0)
7. Clear the TF flag for the next round
8. Go back to Step 2 to load TH and TL again

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 17


PROGRAMMING 8051 TIMERS
Mode 1
Example 9-4
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

MOV TMOD,#01 ;Timer 0, mode 1(16-bit mode)


HERE: MOV TL0,#0F2H ;TL0=F2H, the low byte
MOV TH0,#0FFH ;TH0=FFH, the high byte
CPL P1.5 ;toggle P1.5
ACALL DELAY
SJMP HERE

In the above program notice the following step.


1.TMOD is loaded.
2.FFF2H is loaded into TH0-TL0.
3.P1.5 is toggled for the high and low portions of the pulse.

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 18


PROGRAMMING 8051 TIMERS
Example 9-4 (cont’) Mode 1
DELAY:
SETB TR0
;start the timer 0
AGAIN: JNB TF0,AGAIN ;monitor timer
flag 0

;until it rolls over


4. The DELAY subroutineCLR TR0
using the timer is called.
;stop timer 0
5. In the DELAY subroutine, timer 0 is started by the SETB TR0 instruction.
CLR TF0
6. Timer 0 counts ;clear
up with the passing of each timer
clock, which is provided by the
0 flag
crystal oscillator. As the timer counts up, it goes through the states of FFF3,
FFF4, FFF5,RETFFF6, 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
JNB
instruction falls through.

7. Timer 0 is stopped by the instruction CLR TR0. The DELAY subroutine ends,
and the process is repeated. 19
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025
PROGRAMMING 8051 TIMERS
Mode 1
Example 9-5
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 x 1.085us.
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 needed when it
rolls over from FFFF to 0 and raise the TF (b)
flag.inThis gives 14 x 1.085us =
decimal
(a) in hex
15.19us for half (FFFF
the pulse. For
– YYXX the entire period
+ 1) ConvertitYYXXis T = 2 x 15.19us =
30.38us as the time
x delay generated by thevalues
timer.of the TH,
1.085 us, where TL register to
YYXX are TH, TL decimal to get a
initial values NNNNN decimal,
respectively. then (65536 - 20
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025
NNNN) x
PROGRAMMING 8051 TIMERS
Example 9-6
Mode 1
In Example 9-5, calculate the frequency of the square wave generated on
pin P1.5.
Solution:
In the timer delay calculation of Example 9-5, we did not include the
overhead due to instruction in the loop. To get a more accurate timing, we
need to add clock cycles due to this instructions in the loop. To do that, we
use the machine cycle from Table A-1 in Appendix A, as shown below.
HERE: MOV TL0,#0F2H 2
Cycles
MOV 2
CPL TH0,#0FFH
P1.5 1
ACALL DELAY 2
SJMP HERE 2
DELAY:
SETB TR0 1
AGAIN: JNB 14
TF0,AGAIN CLR 1
TR0 1
CLR 2
TF0 Total
RET
T = 2  28  1.085 us = 60.76 us and F 28
= 16458.2
Hz Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 21
PROGRAMMING 8051 TIMERS
Mode 1
Example 9-7
Find the delay generated by timer 0 in the following code, using both of the
Methods of Figure 9-4. Do not include the overhead due to instruction.
CLR P2.3 ;Clear P2.3
MOV TMOD,#01 ;Timer 0, 16-bitmode
HERE: MOV TL0,#3EH ;TL0=3Eh, the low byte
MOV TH0,#0B8H ;TH0=B8H, the high byte
SETB P2.3 ;SET high timer 0
SETB TR0 ;Start the timer 0
AGAIN: JNB TF0,AGAIN ;Monitor timer flag
0 TR0 ;Stop the timer 0
CLR
CLR TF0 ;Clear TF0 for next round
CLR P2.3
Solution:
a. (FFFFH – B83E + 1) = 47C2H = 18370 in decimal and 18370 x 1.085 us =
19.93145 ms
b. Since TH – TL = B83EH = 47166 (in decimal) we have 65536 – 47166 =
18370. This means that the timer counts from B83EH to FFFF. This plus
Rolling over to 0 goes through a total of 18370 clock cycles, where each
clock is 1.085 us in duration.
Microcontroller
c. Therefore, we have BEC405A-Dr.
18370 VEENA S K.us = 19.93145 ms as the width
x 1.085 of the22
01/25/2025
PROGRAMMING 8051 TIMERS
Example 9-8 Mode 1
Modify TL and TH in Example 9-7 to get the largest time delay possible.
Find the delay in ms. In your calculation, exclude the overhead due to the
instructions in the loop.
Solution:
To get the largest delay we make TL and TH both 0. This will count up from
0000 to CLR
FFFFH and then;Clear
P2.3 roll over to zero.
P2.3
MOV TMOD,#01 ;Timer 0, 16-
bitmode ;TL0=0, the low byte
HERE: MOV TL0,#0
MOV TH0,#0 ;TH0=0, the high byte
SETB P2.3 ;SET high P2.3
SETB TR0 ;Start timer 0
AGAIN: JNB TF0,AGAIN ;Monitor timer flag
0 CLR TR0 ;Stop the timer 0
CLR TF0 ;Clear timer 0 flag
CLR P2.3

Making TH and TL both zero means that the timer will count from 0000 to
FFFF, and then roll over to raise the TF flag. As a result, it goes through a
total Of 65536 states. Therefore, we have delay = (65536 - 0) x 1.085 us =
71.1065ms.
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 23
PROGRAMMING 8051 TIMERS
Example 9-9
Mode 1
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: TL1,#34H ;TL1=34H, low byte of
MOV MOV timer
TH1,#76H ;TH1=76H, high byte
SETB
timerTR1 ;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 x 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
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 24
included.
PROGRAMMING 8051 TIMERS
Mode 1 - Finding the Loaded Timer Values

To calculate the values to be loaded into the TL


and TH registers, look at the following example
 Assume XTAL = 11.0592 MHz, 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 Step1
3. Convert the result of Step2 to hex, where yyxx is the
initial hex value to be loaded into the timer’s register
4. Set TL = xx and TH = yy

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 25


PROGRAMMING 8051 TIMERS
Mode 1
Example 9-10
Assume that XTAL = 11.0592 MHz. What value do we need to load the timer’s
register 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.
Solution:
Since XTAL = 11.0592 MHz, the counter counts up every 1.085 us.
This means that out of many 1.085 us intervals we must make a 5 ms pulse. To get
that, we divide one by the other. We need 5 ms / 1.085 us = 4608 clocks.
To Achieve that we need to load into TL and TH the value 65536 – 4608 = EE00H.
Therefore, we have TH = EE and TL = 00.
CLR P2.3 ;Clear P2.3
MOV TMOD,#01 ;Timer 0, 16-
HERE: MOV TL0,#0
bitmode ;TL0=0, the low byte
MOV TH0,#0EEH ;TH0=EE, the
high byte CMP P2.3 ;SET high P2.3
SETB TR0 ;Start timer 0
AGAIN: JNB TF0,AGAIN ;Monitor timer flag 0
CLR TR0 ;Stop the timer 0
CLR TF0 ;Clear timer 0 flag
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 26
PROGRAMMING 8051 TIMERS
Mode 1
Example 9-11
Assume that XTAL = 11.0592 MHz, write a program to generate a square wave
of 2 kHz frequency on pin P1.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.
a) T = 1 / f = 1 / 2 kHz = 500 us the period of square wave.
b) 1 / 2 of it for the high and low portion of the pulse is 250 us.
c) 250 us / 1.085 us = 230 and 65536 – 230 = 65306 which in
hex is FF1AH.
d) TL = 1A and TH = FF, all in hex. The program is as follow.
MOV TMOD,#10 ;Timer1, 16-bitmode
AGAIN: TL1,#1AH ;TL1=1A, low byte of
MOV MOV timer
TH1,#0FFH ;TH1=FF, the high
byte SETB TR1 ;Start timer 1
BACK: JNB TF1,BACK ;until timer rolls
CLR
over TR1 ;Stop the timer 1
CMP P1.5 ;
CLR TF1 ;Clear timer 1 flag
SJMP AGAIN ;Reload timer
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 27
PROGRAMMING 8051 TIMERS
Mode 1
Example 9-12
Assume XTAL = 11.0592 MHz, write a program to generate a square wave of 50
kHz frequency on pin P2.3.
Solution:
Look at the following
steps.
a) T = 1 / 50 = 20 us, the period of square wave.
b) 1 / 2 of it for the high and low portion of the pulse is 10 us.
c) 10 ms / 1.085 us = 9.216 and 65536 – 9.216 = 65526.784 in decimal,
and in hex it is FFF7h.
d) TL = F7 and TH = FF (hex).
MOV TMOD,#10H ;Timer 1, mod 1
AGAIN: ;TL1=00,low byte of timer
MOV TH1,#0FFh
TL1,#0F7h ;TH1=DC, the high byte
SETB
MOV TR1 ;Start timer 1
BACK: JNB TF1,BACK ;until timer rolls over
CLR TR1 ;Stop the timer 1
P2.3 ;Comp. p2.3 to get hi, lo
CMP AGAIN
SJMP ;Reload timer
;mode 1 isn’t auto-reload
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 28
PROGRAMMING 8051 TIMERS
Mode 1
Example 9-13
Examine the following program and find the time delay in seconds.
Exclude the overhead due to the instructions in the loop.

MOV TMOD,#10H ;Timer 1, mod 1


MOV R3,#200 ;cnter for multiple delay
AGAIN: MOV TL1,#08H ;TL1=08,low byte of timer
MOV TH1,#01H ;TH1=01,high byte
SETB TR1 ;Start timer 1
BACK: JNB TF1,BACK ;until timer rolls over
CLR TR1 ;Stop the timer 1
CLR TF1 ;clear Timer 1 flag
DJNZ R3,AGAIN ;if R3 not zero then
;reload timer
Solution:
TH-TL = 0108H = 264 in decimal and 65536 – 264 = 65272.
Now 65272  1.085 s = 70.820 ms, and for 200 of them we have 200 70.820 ms = 14.164024
sec.

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 29


PROGRAMMING 8051 TIMERS
Mode 2
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 TR0 for timer 0
and SETB TR1 for timer 1
3. After the timer is started, it starts to count up by
incrementing the TL register
• It counts up until it reaches its limit of FFH
• When it rolls over from FFH to 00, it sets high01/25/2025
Microcontroller BEC405A-Dr. VEENA S K.
the TF 30
PROGRAMMING 8051 TIMERS
Mode 2

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
XTAL Overflow
and TLoscillator
÷12 TL TF
flag
TR Reload TF goes high
C/T = 0
when FF 
TH 0

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 31


PROGRAMMING 8051 TIMERS
Mode 2 - To generate a time delay

1. Load the TMOD value register indicating which timer


(timer 0 or timer 1) is to be used, and the timer mode
(mode 2) is selected
2. Load the TH registers with the initial count value
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

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 32


PROGRAMMING 8051 TIMERS
Mode 2 - To generate a time delay
Example 9-14
Assume XTAL = 11.0592 MHz, find the frequency of the square wave
generated on pin P1.0 in the following program
MOV TMOD,#20H ;T1/8-bit/auto reload
MOV TH1,#5 ;TH1 = 5
SETB TR1 ;start the timer 1
BACK: JNB TF1,BACK ;till timer rolls over
CPL P1.0 ;P1.0 to hi, lo
CLR TF1 ;clear Timer 1 flag
SJMP BACK ;mode 2 is auto-reload
Solution:
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

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 33


PROGRAMMING 8051 TIMERS
Mode 2 - To generate a time delay
Example 9-15
Find the frequency of a square wave generated on pin P1.0.
Solution:
MOV TMOD,#2H ;Timer 0, mod 2
;(8-bit, auto reload)
MOV TH0,#0
AGAIN: R5,#250 ;multiple delay count
MOV ACALL DELAY
CPL P1.0
SJMP AGAIN
DELAY: SETB TR0 ;start the timer 0
BACK: JNB TF0,BACK ;stay timer rolls over
CLR TR0 ;stop timer
CLR TF0 ;clear TF for next round
DJNZ R5,DELAY
RET
T = 2 ( 250 x 256 x 1.085 us ) = 138.88ms, and frequency = 72 Hz
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 34
PROGRAMMING 8051 TIMERS
Mode 2 - To generate a time delay
Example 9-16
Assuming that we are programming the timers for mode 2, find the value (in
hex) loaded into TH for each of the following cases.

(a) MOV TH1,#-200 (b) MOV TH0,#-60


(c) MOV TH1,#-3 (d) MOV TH1,#-12
(e) MOV TH0,#-48

Solution:
You can use the Windows scientific calculator to verify the result provided by the
assembler. In Windows calculator, select decimal and enter 200. Then select hex,
then +/- to get the TH value. Remember that we only use the right two digits and
ignore the rest since our data is an 8-bit data.
Decimal 2’s complement (TH value)
-3 FDH
-12 F4H The advantage of using
The number 200 is the
timer count till the
-48 D0H negative values is that you
-60 don’t need to calculate
TFset to 1
is
-200 C4H the value loaded to THx
38H
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 35
SERIAL COMMUNICATION

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 36


BASICS OF SERIAL
COMMUNICATION
 Computers transfer data in two ways:
 Parallel
 Often 8 or more lines (wire conductors) are used to transfer data to a
device that is only a 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

Serial Transfer ParallelD Transfer


0

Sende Receive Sende Receive


r r r r

D
Microcontroller BEC405A-Dr. VEENA S K. 7 01/25/2025 37
BASICS OF SERIAL
COMMUNICATION
• At the transmitting end, the byte of data must be converted to
serial bits using parallel-in-serial-out shift register
• At the receiving end, there is a serial-in-parallel-out shift
register to receive the serial data and pack them into byte
• When the distance is short, the digital signal can be
transferred as it is on a simple wire and requires no
modulation
• If data is to be transferred on the telephone line, it must be
converted from 0s and 1s to audio tones
• This conversion is performed by a device called a modem,
“Modulator/demodulator”
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 38
BASICS OF SERIAL
COMMUNICATION
• Serial data communication uses two methods
• Synchronous method transfers a block of data at a time
• 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)
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 39
BASICS OF SERIAL COMMUNICATION
Half- and Full- Duplex Transmission
• If data can be transmitted and received, it is a duplex
transmission
• If data transmitted one way a time, it is referred to as half duplex
• If data can go both ways at a time, it is full duplex
• This is contrast to simplex transmission
Simple Transmitte Receiver
x r

Transmitte Receiver
Half r
Duplex Receiver Transmitte
r
Transmitte
Transmitte Receiver
Full rr
Duplex Receiver Transmitte
r
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 40
BASICS OF SERIAL COMMUNICATION
Start and Stop Bits

• A protocol is a set of rules agreed by both the sender and


receiver on
• How the data is packed
• How many bits constitute a character
• When the data begins and ends
• Asynchronous serial data communication is widely used for
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 stop bit(s) is 1 (high)

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 41


BASICS OF SERIAL COMMUNICATION
Start and Stop Bits
ASCII character “A” (8-bit binary 0100 0001)

Stop Start Mark


Space 0 1 0 0 0 0 0 1 Bit
Bit

D7 D0

Goes out last Goes out first


The 0 (low) is
referred to as The transmission begins with
space a start bit followed by D0,
the LSB, then the rest of the When there is no
bits
until MSB (D7), and finally, transfer, the signal
the one stop bit indicating is 1 (high), which
the of the is referred to as
end
mark
character

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 42


BASICS OF SERIAL COMMUNICATION
Start and Stop Bits

• Due to the extended ASCII characters, 8-bit ASCII data is


common
• In older systems, ASCII characters were 7- bit
• In modern PCs the use of one stop bit is standard
• In older systems, due to the slowness of the receiving mechanical
device, two stop bits were used to give the device sufficient time to
organize itself before transmission of the next byte
• Assuming that we are transferring a text file of ASCII characters
using 1 stop bit, we have a total of 10 bits for each character
• This gives 25% overhead, i.e. each 8-bit character with an extra 2 bits
• In some systems in order to maintain data integrity, the parity
bit of the character byte is included in the data frame
• UART chips allow programming of the parity bit for odd-, even-, and no-
parity options

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 43


BASICS OF SERIAL COMMUNICATION
Data Transfer 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
• It is modem terminology and is defined as the number
of signal changes per second
• In modems, there are occasions when a single change
of signal transfers several bits of data
• As far as the conductor wire is concerned, the
baud rate and bps are the same, and we use the
terms interchangeably
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 44
BASICS OF SERIAL COMMUNICATION
Data Transfer Rate
• The data transfer rate of given computer system
depends on communication ports incorporated
into that system
• IBM PC/XT could transfer data at the rate of 100 to
9600 bps
• Pentium-based PCs transfer data at rates as high as
56K bps
• In asynchronous serial data communication, the baud
rate is limited to 100K bps

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 45


BASICS OF SERIAL COMMUNICATION
RS232 Standards

• An interfacing standard RS232 was set by the


Electronics Industries Association (EIA) in 1960
• The standard was set long before the advent of
the TTL logic family, its input and output voltage
levels are not TTL compatible
• In RS232, a 1 is represented by -3 ~ -25 V, while
a 0 bit is +3 ~ +25 V, making -3 to +3 undefined

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 46


BASICS OF SERIAL COMMUNICATION
RS232 DB-25
RS232 Standards
PinsDescription
Pin Pin Description
1 Protective ground 14 Secondary transmitted data
2 Transmitted data (TxD) 15 Transmitted signal element
timing
3 Received data (RxD) 16 Secondary receive data
4 Request to send (-RTS) 17 Receive signal element timing
5 Clear to send (-CTS) 18 Unassigned
6 Data set ready (-DSR) 19 Secondary receive data
7 Signal ground (GND) 20 Data terminal ready (-DTR)
8 Data carrier detect (-DCD) 21 Signal quality detector
9/10 Reserved for data testing 22 Ring indicator (RI)
11 Unassigned 23 Data signal rate select
12 Secondary data carrier 24 Transmit signal element timing
detect
13 Secondary clear to send 25 Unassigned

RS232 Connector
DB-25

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 47


BASICS OF SERIAL COMMUNICATION
RS232 Standards

• Since not all pins are used in PC cables, IBM


introduced the DB-9 version of the serial I/O standard

RS232 Connector RS232 DB-9


DB-9 Pins Description
Pin
1 Data carrier detect (-DCD)
2 Received data (RxD)
3 Transmitted data (TxD)
4 Data terminal ready (DTR)
5 Signal ground (GND)
6 Data set ready (-DSR)
7 Request to send (-RTS)
8 Clear to send (-CTS)
9 Ring indicator (RI)

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 48


BASICS OF SERIAL COMMUNICATION
Data Communication Classification
• Current terminology classifies data communication
equipment as
• DTE (data terminal equipment) refers to terminal and computers
that send and receive data
• DCE (data communication equipment) refers to communication
equipment, such as modems
• The simplest connection between a PC and microcontroller
requires a minimum of three pins, TxD, RxD, and ground
Null modem
connection
DTE DTE
TxD TxD
RxD RxD

groun
d
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 49
BASICS OF SERIAL COMMUNICATION
RS232 Pins
• DTR (data terminal ready)
• When terminal is turned on, it sends out signal DTR to
indicate that it is ready for communication
• DSR (data set ready)
• When DCE is turned on and has gone 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

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 50


BASICS OF SERIAL COMMUNICATION
RS232 Pins
• DCD (data carrier detect)
• The modem asserts signal DCD to inform the DTE that
a valid carrier has been detected and that contact
between it and the other modem is established
• RI (ring indicator)
• An output from the modem and an input to a PC
indicates that the telephone is ringing
• It goes on and off in synchronous with the ringing
sound

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 51


BASICS OF SERIAL COMMUNICATION
8051 CONNECTION TO RS232
• A line driver such as the MAX232 chip is required to
convert RS232 voltage 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 converter) to convert
the R232’s signals to TTL voltage levels that will be
acceptable to 8051’s TxD and RxD pins
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 52
BASICS OF SERIAL COMMUNICATION
8051 CONNECTION TO RS232

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 53


BASICS OF SERIAL COMMUNICATION
MAX233
• To save board space, some designers use MAX233 chip from
Maxim
• MAX233 performs the same job as MAX232 but eliminates the need
for capacitors
• Notice that MAX233 and MAX232 are not pin compatible

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 54


SERIAL COMMUNICATION
PROGRAMMING
• To allow data transfer between the PC and an 8051
system without any error, we must make sure that the
baud rate of 8051 system matches the baud rate of the
PC’s COM port
• HyperTerminal function supports baud rates much higher
than listed below PC Baud Rates
110
150
300
600
1200
2400
4800
9600 Baud rates supported
19200 by 486/Pentium IBM
PC BIOS
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 55
SERIAL COMMUNICATION
PROGRAMMING
With XTAL = 11.0592 MHz, find the TH1 value needed to have the
following 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 to set 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
11.0592 MHz
Notice that dividing 1/12 of the crystal frequency by 32 is the default
Machine cycle 28800
XTAL ÷ 32
value upon activation of the 8051 RESET
Oscillator
÷ 12 pin.
freq
By UART
Hz
To
921.6 kHz timer1
To set
Baud TH1 (Decimal) TH1 (Hex) the
Rate Baud
TF is set to 1 every 12 9600 -3 FD rate

ticks, so it functions 4800 -6 FA


as a frequency divider 2400 -12 F4
1200 -24 E8
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 56
SERIAL COMMUNICATION

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 57


SERIAL COMMUNICATION PROGRAMMING
SBUF Register
• SBUF is an 8-bit register used solely for serial
communication
• For a byte data to be transferred via the TxD line, it must be
placed in the SBUF register
• The moment a byte is written into SBUF, it is framed with the start
and stop bits and transferred serially via the TxD line
• SBUF holds the byte of data when it is received by 8051 RxD
line
• When the bits are received serially via RxD, the 8051 de-frames it
by eliminating the stop and start bits, making a byte out of the
data received, and then placing it in SBUF

MOV SBUF,#’D’ ;load SBUF=44h, ASCII for ‘D’


MOV SBUF,A ;copy accumulator into SBUF
MOV A,SBUF ;copy SBUF into accumulator

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 58


SERIAL COMMUNICATION PROGRAMMING
SCON Register
• SCON is an 8-bit register used to program the start
bit, stop bit, and data bits of data framing, among
other things
SM0 SM1 SM2 REN TB8 RB8 TI RI

SM0 Serial port mode


SCON.7 specifier Serial port
SM1 mode specifier
SCON.6 Used for multiprocessor communication
SM2
TB8 Set/cleared by software to enable/disable
SCON.5
SCON.3 reception
REN
RB8 Not widely used
SCON.4
SCON.2 TI Not widely used
RISCON.1 Transmit interrupt flag. Set by HW at the
SCON.0 begin of the stop bit mode 1. And cleared
by SW Receive interrupt flag. Set by HW
Not Make SM2,at the
TB8, and
e: RB8 =0 begin of the stop bit mode 1. And cleared
by SW VEENA S K.
Microcontroller BEC405A-Dr. 01/25/2025 59
SERIAL COMMUNICATION PROGRAMMING
SCON Register
SM0, SM1
• They determine the framing of data by specifying the
number of bits per character, and the start and stop
bits

SM2
• This enables the multiprocessing capability of the 8051

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 60


SERIAL COMMUNICATION PROGRAMMING
SCON Register
REN (receive enable)
• It is a bit-addressable register
• When it is high, it allows 8051 to receive data on RxD pin
• If low, the receiver is disable
TI (transmit interrupt)
• When 8051 finishes the transfer of 8-bit character
• It raises TI flag to indicate that it is ready to transfer another byte
• TI bit is raised at the beginning of the stop bit
RI (receive interrupt)
• When 8051 receives data serially via RxD, it gets rid of the start and
stop bits and places the byte in SBUF register
• It raises the RI flag bit to indicate that a byte has been received and
should be picked up before it is lost
• RI is raised halfway through the stop bit
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 61
SERIAL COMMUNICATION PROGRAMMING
Programming Serial Data Transmitting
In programming the 8051 to transfer character bytes serially
1. TMOD register is loaded with the value 20H, indicating the use of
timer 1 in mode 2 (8-bit auto-reload) to set baud rate
2. The TH1 is loaded with one of the values to set baud rate for
serial data transfer
3. The SCON register is loaded with the value 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. TI is cleared by CLR TI instruction
6. The character byte to be transferred serially is written into SBUF
register
7. The TI flag bit is monitored with the use of instruction JNB TI,xx
to see if the character has been transferred completely
8. To transfer the next byte, go to step 5
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 62
SERIAL COMMUNICATION PROGRAMMING
Programming Serial Data Transmitting

Write a program for the 8051 to transfer letter “A”


serially at 4800 baud, 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

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 63


SERIAL COMMUNICATION PROGRAMMING
Programming Serial Data Transmitting
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 ;transfer “E”
ACALL TRANS
A,#”E”
MOV A,#”S” ;transfer “S”
ACALL
TRANS SJMP ;keep doing it
;serialAGAIN
data transfer subroutine
TRANS: MOV SBUF,A ;load SBUF
HERE: JNB ;wait for the last bit
TI,HERE CLR ;get ready for next byte
TI
RET
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 64
SERIAL COMMUNICATION PROGRAMMING
Importance of TI Flag
The steps that 8051 goes through in transmitting a character via
TxD
1. The byte character to be transmitted is written into the SBUF
register
2. The start bit is transferred
3. The 8-bit character is transferred on bit at a time
4. The stop bit is transferred
• It is during the transfer of the stop bit that 8051 raises the TI flag, indicating
that the last character was transmitted
5. By monitoring the TI flag, we make sure that we are not overloading
the SBUF
• If we write another byte into the SBUF before TI is raised, the untransmitted
portion of the previous byte will be lost
6. After SBUF is loaded with a new byte, the TI flag bit must be forced
to 0 by CLR TI in order for this new byte to be transferred
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 65
SERIAL COMMUNICATION PROGRAMMING
Importance of TI Flag
• By checking the TI flag bit, we know whether or
not the 8051 is ready to transfer another byte
• It must be noted that TI flag bit is raised by 8051 itself
when it finishes data transfer
• It must be cleared by the programmer with instruction
CLR TI
• If we write a byte into SBUF before the TI flag bit is
raised, we risk the loss of a portion of the byte being
transferred
• The TI bit can be checked by
• The instruction JNB TI,xx
• Using an interrupt
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 66
SERIAL COMMUNICATION PROGRAMMING
Programming Serial Data Receiving
In programming the 8051 to receive character bytes serially
1. TMOD register is loaded with the value 20H, indicating the use of
timer 1 in mode 2 (8-bit auto-reload) to set baud rate
2. TH1 is loaded to set baud rate
3. The SCON register is loaded with the value 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. RI is cleared by CLR RI instruction
6. The RI flag bit is monitored with the use of 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
a safe place
8. To receive the next character, go to step 5

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 67


SERIAL COMMUNICATION PROGRAMMING
Programming Serial Data Receiving
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 ;keep getting data

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 68


SERIAL COMMUNICATION PROGRAMMING
Programming Serial Data Receiving
Example 10-5
Assume that the 8051 serial port is connected to the COM port of IBM PC, and
on the PC, we are using the terminal.exe 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 PC the message “We Are
Ready”, (b) receive any data send by PC and put it on LEDs connected to P1,
and (c) get data on switches connected to P2 and send it to PC serially. The
program should perform part (a) once, but parts (b) and (c) continuously, use
4800 baud rate.
Solution:
ORG 0
P2,#0FFH ;make P2 an input port
MOV TMOD,#20H ;timer 1, mode 2
MOV ;4800 baud rate
TH1,#0FAH ;8-bit, 1 stop, REN enabled
SETB
MOV TR1 ;start timer 1
MOV SCON,#50H
DPTR,#MYDATA ;load pointer for
H_1: message CLR A
MOV A,@A+DPTR ;get the character
...

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 69


SERIAL COMMUNICATION PROGRAMMING
Programming Serial Data Receiving
Example 10-5 (cont’)

JZ B_1 ;if last character get out


ACALL SEND ;otherwise call transfer
INC DPTR ;next one
SJMP H_1 ;stay in loop
B_1: MOV a,P2 ;read data on P2
ACALL ;transfer it serially
SEND ;get the serial data
ACALL
MOV RECV ;display it on LEDs
P1,A ;stay in loop indefinitely
SJMP data
;----serial B_1 transfer. ACC has the data------
SEND: MOV ;load the data
SBUF,A H_2:JNB ;stay here until last bit
TI,H_2 ;gone
CLR ;get ready for next char
TI RET ;return to caller
;----Receive data serially in ACC----------------
RECV: JNB ;wait here for char
RI,RECV MOV ;save it in ACC
A,SBUF ;get ready for next char
CLR RI ;return to caller
RET
... Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 70
SERIAL COMMUNICATION PROGRAMMING
Programming Serial Data Receiving

Example 10-5
(cont’)
;-----The message---------------
MYDATA: DB “We Are
Ready”,0 END

8051
P LE
To Tx
D
1 D
PC

COM Rx
P S
Port D
2 W

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 71


SERIAL COMMUNICATION PROGRAMMING
Importance of RI Flag

In receiving bit via its RxD pin, 8051 goes through


the following steps
• It receives the start bit
• Indicating that the next bit is the first bit of the
character byte it is about to receive
• The 8-bit character is received one bit at time
• The stop bit is received
• When receiving the stop bit 8051 makes RI = 1,
indicating that an entire character byte has been
received and must be picked up before it gets
overwritten by an incoming character

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 72


SERIAL COMMUNICATION PROGRAMMING
Importance of RI Flag
(cont’)
• By checking the RI flag bit when it is raised, we know
that a character has been received and is sitting in the
SBUF register
• We copy the SBUF contents to a safe place in some other
register or memory before it is lost
• After the SBUF contents are copied into a safe place,
the RI flag bit must be forced to 0 by CLR RI in order
to allow the next received character byte to be placed
in SBUF
• Failure to do this causes loss of the received character

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 73


SERIAL COMMUNICATION PROGRAMMING
Importance of RI Flag
• By checking the RI flag bit, we know whether or not the
8051 received a character byte
• If we failed to copy SBUF into a safe place, we risk the loss of
the received byte
• It must be noted that RI flag bit is raised by 8051 when it finish
receive data
• It must be cleared by the programmer with instruction CLR RI
• If we copy SBUF into a safe place before the RI flag bit is raised,
we risk copying garbage
• The RI bit can be checked by
• The instruction JNB RI,xx
• Using an interrupt

Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 74


Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 75
GvÀÛªÀĪÁzÀzÀÄzÀÄ ±ÀæªÀÄ«®èzÉ
®©ü¸ÀzÀÄ, EZÉѬĮè¢zÀÝgÀAvÀÆ
CzÀÄ ¸ÁzsÀåªÉà E®è.
PÀÄ.ªÉA.¥ÀÄ.

Nothing
worth having was ever achieved

without effort
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 76

The0dore Roosevelt

You might also like