Unit-7 8051 Serial Port and Programming_Technical
Unit-7 8051 Serial Port and Programming_Technical
No-g092|
Mhan institute o
Transmitter Receiver.
Review Questions
Marking
Stop
Transmitter D D, D, D,D, D,|Ds| D| bits Receiver
ASCII
characters CLK
CLK
Time
transmission
Fig. 10.2.1 Transmission format for asynchronous
1 Stop bit
Start bit
1 1 0 0
Transmitter Receiver
1Frame
Time
The data rate can be expressed as bits/sec or characters/sec. The term bits/ sec 15 asv
called the baud rate. The asynchronous format is generally used in low-speed
transmission (less than 20 kbits/sec).
Protective ground
Secondary transmitted data 14 2 Transmitted data (TxD) DCE
Transmission signal element timing (DCE source) 15 3 Received data (RxD) DTE
Secondary received data 16 Request to send (RTS) DCE
Receiver signal element timing (DCE source) 17 Clear to send (CTS) DTE
Unassigned 18 6 Data set ready (DSR) DTE
Secondary request to send 19 Signal ground
Data terminal ready (DTR) 20 Received line signal detector
Fig. 10.2.4
10.2.4.1 DB-25 P Connector
The Table 10.2.2 shows pins and signals description for RS 232C DB-25P connector.
AA Protective ground
2 TxD BA Transmitted data N
RxD BB OUT
Received data
4 RTS CA N
Request to send
5 CTS CB
OUT
Clear to send
6 CC OUT
Data set ready
7 GND AB
....e..... Signal ground (common retum)
8 8 CF OUT
Received line signal detector
(Reserved for data set testing)
10
(Reserved for data set testing)
11
Unsigned
12 SCF OUT
Secondary recd. line sig. detector
19 SCA
Unassigned
20 DTR
Secondary request to send IN
CD
Data terminal ready IN
Signal quality detector OUT
2 CE
Ring indicator
........ OUT
23 CH/CI Data signal rate selector (DTE/DCE source)
IN/OUT
24 DA
Transmit signal element timing (DTE source) IN
Unassigned
c00o0000ooooooooooow
Table 10.2.2
9 RI Ring indicator
Table 10.2.3 Pin description for DB-9P connector
232C
10.2.4.3 Data Transmission and Reception using RS
shows the digital data transmission using modems and standard telephone
Fig. 10.2.6 serial data over long distances are
and other eguipment used to send
lines. Modems
(DCE). The terminals and comnputers that are
known as Data Communication Equipment (DTE).
receiving the serial data are referred to as Data Terminal Equipment
sending or
Large
Microcomputer timeshare
controlled Modem computer
terminal Modem
Telephone TXD
TXD line
Rx
RxD
RTS
RTS
CTS
CTS
CD
DTR
DTR
DSR
DSR
DCE DCE
DTE
DTE DTE = Data Terminal Equipment
DCE = Data Communication Equipment
Start
Output DTR
low The DTE is ready
Read DSR
from modem Check if the modem is ready
No Low
Wait until it is low
Yes
Prepare for Initialize pointers and counters
transmission for the text to be
transmitted
Output RTS The DTErequest use of the data
low channel to begin transmission
Yes
Read CTS When CTS goes low the modem
and communication channels are
from modem ready for transmissions
No Low
Wait for modem to be ready
Yes
Transmission
begins
Fig. 10.3.1
reception of the data durng
The way in which SBUF is used for the transmission and
serial communication is explained below.
Transmission : When a byte of data is to be transmitted via the TxD p, ue
written into SBUF, it
SBUF is loaded with this data byte. As soon as a data byte is
is framed with the start and stop bits and transmitted serially via the TxD pin.
detrae
Reception : When 8051 receives data serially via RxD pin of it, the 8051 byteis
This
it. The start and stop bits are separated out from a byte of data.
placed in SBUF register.
Bit pattern of SCON register A Communication.
The 8051 provides four programmable modes for serial data mode
particular mode can be selected by setting the SM0 and SM1 bits in SCON. The
for SCON.
selection also decides the baud rate. The Fig. 10.3.2 shows the bit patterns
(MSB)
(LSB)
7 5 4 3 2 1 0
knowledge
TECHNICAL PUBLICATIONS- An up thrust for
8051 Serial Port
10- 12 and
Microprocessors and Microcontrollers
Programming
Name and significance
Symbol Position
Serial baud rate modify bit. It is 0 at reset. It is set to 1 by program to double the
SMOD PCON.7
baud rate.
In this mode, serial data enters and exits through RxD. TxD outputs the shift clock.
8 bits are transmitted/received : 8 data bits (LSB first). The baud rate is fixed at 1/12 the
Oscillator frequency.
Mode 1
In this mode, 10 bits are transmitted (through TxD) or received (through RxD) : A
start bit (0), 8 data bits (LSB first) and a stop bit (1). On receive, the stop bit goes into
RB8 in Special Function Register SCON. The baud rate is variable.
Mode 2
In this mode, 11 bits are transmitted (through TxD) or received (through RxD) : A
start bit (0), 8 data bits (LSB first), a programmable 9n data bit, and a stop bit(1). On
Transmit, the 9 data bit (TB8 in SCON) can be assigned the value of 0 Or 1. Or, for
thegth
example, the parity bit (P, in the PSW) could be moved into TB8. On receive,
data bit goes into RB8 in Special Function Register SCON, while the stop bit is ignored.
The baud rate is programmable to either 1 1
32 64 the oscillator frequency.
Mode 3
Rates
Using Timer/Counter 2 to Generate Baud
rate generating mode. If Timer 2
For this purpose, Timer 2 must be used in the baud
is :
is being clocked through pin T2 (P1.0) the baud rate
Timer 2 overflow rate
Baud rate =
16
1
SMOD = 0, Baud rate =
64 Oscillator frequency
To set the SMOD bit : ORL PCON, #80H. The address of PCON is 87H.
Note By changing SMOD bít in PCON from 0 to 1 we can double the baud rate in
8051.
Serial Port in Mode 3
The baud rate in mode 3 is variable and sets up exactly the same as in
mode 1
Review Questions
P 3.0
RxD 3
R, OUT R, IN
P3.1
TxD
T IN T, OUT
DB-9P
Rg OUT R, IN
connector
RS 232C
T, IN T, OUT
Review Question
Transfer
10.5.1 Programming 8051 for Serial Data
to perform following sequence of
To program 8051, to transfer data serially we have
actions :
value 20H to use timer 1 in mode 2
1. Load the TMOD register with the
(8-bit auto-reload) to set the baud rate.
data transfer.
2. Load TH1 to set the desire baud rate for serial
3. Load SCON register with the value 50H, to use
serial mode 1, where an &-bit data
is framed with start and stop bits.
4. Set TR1 to 1 to start timer 1.
5. Clear TI with CLR TI instruction.
6. Write a character to be sent in to the SBUF register.
character has
7. Check the TI flag bit with instruction JNB TI, XXXX to see if the
been transferred completely.
8. Go to step 5 to transfer the next character.
Example 10.5.1 8051 uses 11.0592 MHz Crystal. To get 9600 hertz. baud rate how w
you program it for serial transmission?
9600 hertz is
Solution: When 11.0592 MHz crystal is used and a standard baud rate of
required then, the setting of TH1can be found as
THI = 256 -
k xOscillator frequency
384 x Baud rate
1. Load the TMOD register with the value 20H to use timer 1 in mode 2 (8-bit
auto-reload) to set the baud rate.
2. Load THI to set the desire baud rate for serial data transfer.
3. Load SCON register with the value 50H, to use serial mode 1, where an &-bit data
is framed with start and stop bits.
4. Set TR1 to 1 to start timer 1.
5. Clear RI with CLR RI instruction..
6. Check the RI flag bit with instruction JNB RI, XXXX to see if an entire character
has been received yet.
7. If RI is set, SBUF has the byte. Save
this byte.
8. Go to step 5 to receive the next
character.
Example 10.5.4 Write an 8051 Assembly language program to receive bytes serially withbaud
rate 9600, 8-bit data and 1 stop bit.
Solution :
Simultaneously send received bytes to por
MOV TMOD, #20H
timer 1, mode 2 (auto reload)
MOV TH1, #FDH
9600 baud rate
MOV SCON, #5OH
SETB TR1 8-bit, 1stop, REN enabled
HERE: start timer 1
JNB RI, HERE
MOV A, SBUF wait for character receive completely
MOV P2, A save the received character
CLR RI send character to port 2
SJMP HERE Get ready to receive next byte
Go to receive next character
TXD
(P3.1)
To PC
COM Port
RxD
(P3.0)
Fig. 10.5.1
MOVTMOD, #20H Initialize timer 1 in mode 2
MOVTH1, #0FDH Load count to get 9600 baud rate
MOV SCON, #5OH 8-bit, 1 stop, REN enabled
SETB TR1 Start timer 1
MOVDPTR, #2000H Initialize memory pointer to
save received data
MOVR0,#05H Initialize counter to read 5 characters
RECV: JNB RIRECV wait for character
10.5.4 Examples
transfer the message GOODLUCK
Example 10.5.6 Write an ssembly language program to GTU :Winter-06, 15
sertally at 9600 baud, 8-bitdata, 1 stop bit.
Solution :
MOV TMOD, #20H ;timer 1, mode 2
MOVTH1, #FDH ;9600 baud rate
MOV SCOH, #50H ;8 bit, 1 stop bit, REN-enabled.
SETB TRI, ;start timer 1
START MOV A, #"G" ;transfer "C
ACALL TRANS
MOVA, #"O" ;transfer "O"
ACALL TRANS
MOV A, #"O" ;transfer "O"
ACALL TRANS
MØV A, #"D" ;transfer "D"
ACALL TRANS
MOV A, #"L" ;transfer "I"
ACALL TRANS
MOV A, #"U" ;transfer "U"
ACALL TRANS
MOV A, #"C" ;transfer "CH
ACALL TRANS
MOV A, #"K" ;transfer "K"
;serial data transfer subroutine
TRANS: MOV SBUF, A;load SBUF
HERE JNB TI, HERE;wait for last bit to transfer
CLR TI ; clear TI for next
RET ;character
X1x 10 92
1.085x 10-6
Example 10.5.9Write 8051 progTamto receive bytesof aata serally ana put them tn PI Set
GTU: Summer-17, Marks 7
the baud rate as 4800, 8 bit aata and one 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 bit,REN enabled
SETB TR1 ;start Timer 1
HERE: JNB RI, HERE :Wait for character to come in
MOVA, SBUF ;save incoming byte in A
MOV P1, A :Send received byte to port 1
CLR RI ;Get ready to receive next byte
SJMP HERE :Continue receiving data
Example 10.5.10 Write instructions to initialize serial port in mode 1 with baud rate of 80
and crystal frequency of 11.059, MH,
o*****
Solution :
MOV TMOD, #20H ;timer 1, mode 2(auto reload)
MOV TH1, #FAH ;count for baud rate 4800
MOV SCON, #50H ;8-bit, 1stop, mode 1
SETB TR1 ;start timer 1
= 256
1x11.059 MHz
384 x 4800
= 250 = FAH
Solution:
SW1 EQU P2.2
;Starting position
ORG OH
MAIN : MOV TMOD, #20H : Timer 1, mode 2
baud rate
MOV TH1, #-12 ;(28800/12) = 2400
MOV SCON, #50H ;8-bit data, 1 stop bit
SETB TR1 ;Start Timer 1
SETB SW1 :Make SW an input
ST: JBP2.2, NEXT ;Check the status of SW1
MOV DPTR, #MESS1 : if SW1 = 0 send FALSE'
BACK: CLR A
MOVC A,@A+DPTR : Read the value
JZLAST
ACALL SENDCOM :Send value to serial port
INC DPTR : Point to the next value
SJMP BACK ;Repeat
NEXT : MOV DPTR, #MESS2:If SW1 = 2 send 'CORRE CT
BACK1: CLR A
MOVC A, @A+DPTR :Read the value
JZLAST
ACALL SENDCOM ;Send value to serial port
INC DPTR :Point to the next value
SJMP BACK1 ;Repeat
LAST: NOP
MESS1: DB "FALSE", 0
MESS2: DB "CORRECT" 0
END
Delay Calculations:
With XTAL = 11.0592 MHz we have,
11.0592/12
= 921.6 kHz machine cycle frequency
921.6 kHz/32 = 28800 Hz frequency used by Timer 1 to set the baud rate
Baud rate = 28800/12 = 2400
Review Questions
4. Write an 8051 program to send the message "The Earth is beautiful", to the serial port
continuously. Assume XTAL = 11.0592 MHz, 9600 baud rate, 8-bit data and one stop bit.
5. Write 8051 program to transfer serially the message "VITUBELGALUM" continuously at baud rate
of 9600.
6. Explain the importance of TI and RI flags.
7. Indicate steps of programming 8051 to transmit a character and recetve a character serialy.
10.6 Serial Communication Programming in C
GTU : Winter-10, 11, 13, 17, Summer-10, 15, 19
We have seen that the SFR registers of the 8051 are accessible directly in 8051 C
compilers by inclusion of the reg51.h file. However, to use second serial port we have to
declare the byte addresses of the new SFR registers, ie. SBUF1 and sCONI using SFR
data type. In additiorn, we have to declare bit addresses for TI1 and RIl using bit data
type.
Example 10.6.1 Writen Cprogram for the 8051 to tnansfer the letter "C serialty at
9600 baud continuously.Use 8-bitdataand1 stopbit GTU : Winter-10
Solution:
#include<reg51.h>
void main (void)
TMOD= (x20;
TH1 = XFD;
SCON = x50;
TR1 = 1;
while(1)
{
SBUF='C;
while(TI ==0);
TI=0;
Example 10.6.2 Write aC program that continuously receives asingie bit of data from P1.0
and sends it to P2.0, while simultaneously creating a square wave of 400 us period on pin
P2.5. Use timer 0to create the sauare wave. Assume that XTAL 11.0592 MHz.
Solution : We will use timer 0 in mode 2 (auto-reload). One half of the period is 200 us.
200/1.085 us = 184 and THO = 256 184 = 72 or 48H.
#include <reg51.h>
sbit IBt = P1^0;
sbit Obit = P2^0:
sbit SWNAVE = P2^5;
void timer0(void) interrupt 1
{
SWAVE = ~ SWAVE; /* toggle pin P2.5 */
void main(void)
{
Obit = 1; /* make P1.0 input */
TMOD = 0x02;
THO = Ox72; * THO = - 184 */
IE = 0x82; /* enable interruptsfor timer 0 */
while(1)
Example 10.6.3 Nrte a Cprogram that continuously getsa single bit of data from P1.0and
sends it to P2:0 in the main, while simultaneously (a) creating a squarewave of 400 JS
period on pin P2.5, and (b) sendingletter 'A through Z to the serial port. Use timer0
to create the squarewave.Assume thatXTAL = 11.0592 MHz. Use the 9600 baud Tate.
GTU: Winter-17, Marks 7
Solution : We will use timer 0in mode 2 (auto-reload). THO = 200/1.085 us =- 184,
which is 72H.
#include <reg51.h>
sbit Ibit = P1^0;
sbit Obit = P2^0;
sbit SWAVE = P2^5;
unsigned char ch = 'A:
void timerD(void) interrupt 1
else
RI = 0; /* clear interrupt */
void main(void)
if(TI == 1)
TECHNICAL PUBLICATIONS - An up thrust for knowledge
8051 Serial Port and Programming
10- 28
MicroprOcessors end Microcontrollers
TI = 0; /* clear interrupt */
else
*/
PO = SBUF: /* put value on pins
RI = 0; /* clear interrupt */
void main()
{
unsigned char c;
* make P1 an input "/
P1 = 0xFF;
TMOD = 0x22;
TH1 = OxFO; /* 9600 baud rate */
SCON = Ox50;
THO = Ox72; /* 2.5 kHz has T= 400 us */
IE = 0x92; /* enable interrupts "/
TR1 = 1; /* start timer 1 */
TRO = 1; /* start timer 0 */
while(1)
Example 10.6.5 Write u 8051 C program that continuously gets a single bit of data from
PL7 and sends it to Pl.0, which creates a square waoe of 200 us period on pm
XTAL frequency = 1l.0592 MHz. GTU : Summer-10
Solution: We will use timer 0in mode 2 (auto-reload). One half of the period is 100 S.
100/1.085 us = 92 and TH0 = 256 92 = 164 or A4H.
#include <reg51.b>
sbit IBit = P1^7;
sbit Obit = P1^0;
sbit SWAVE = P2^5;
TECHNICAL PUBLICATIONS - An up
thrust for
Microprocessors and Microcontrollers 10- 29 8051 Serial Port and Programming
vold main(vold)
= D2H
#include <reg51.b>
sbit SW =P1^2;
sbit ST =P2^1;
sbit SWAVE = P2^3;
vold timer 0(void) interrupt 1
if (TI = = 1)
else
void main ()
Example 10.6.8 Write a program to send the message "Happy New Year to serial port.
Assume aswitch SW is connected to pin P1.2. Monitor its status and set the baud ate as
follows : If SW 0, 4800 baud rate otherwise SW = 1 9600 baud rate. Assume
XTAL = 11.0592 MHz, 8-bit data and 1stop bit GTU : Summer-15, Marks 7
Solution :
SW BIT P1.2
ORG 0 ;starting position
MAIN: MOV TMOD, #20H
MOV TH1, # -6 :4800 baud rate (default)
MOV SCON, #50H
SETB TR1 ;start Timer 1
SETB SW ; make SW an input
CH: JNB SW, BR48 ;check SW status
MOV A, PCON Read PCON
SETB ACC.7 ;set SMOD High for 9600 baud rate
MOV PCON, A ;write PCON
SJMP OVER : send message
SerialTx(Y);
SerialTx(E');
SerialTx(S);
}
void SerialTx(unsigned char x)
Review Questions
1. Write an 8051 Cprogram to transfer the message "Good morning" serially at 9600 baud, 8-bit
data, 1stop bit.
2. Write 8051 'C program to receive bytes of data serially and put themn in P1, Set the baud rate as
4800, 8 bit data and one stop bit.
3. Write a 8051 Cprogram to transmit the character * continuously serially in the &bit, 1start bit,
1stop bit, 2400 baud rate format. Assume the crystal frequency as 11.0592 MHz.
32 frequency
b 1
programmable to the oscillator frequency
64
C fixed dË either (a) or (b) [Ans. : d]