0% found this document useful (0 votes)
558 views29 pages

8051 Serial Port Programming

Here is a program to serially transfer data received from ports P0, P1, and P2 continuously at 4800 baud: AGAIN: SETB TR1 ;Start timer 1 MOV A, P0 ;Read data from P0 ACALL XMIT ;Transmit data MOV A, P1 ;Read data from P1 ACALL XMIT ;Transmit data MOV A, P2 ;Read data from P2 ACALL XMIT ;Transmit data SJMP AGAIN ;Repeat continuously XMIT: MOV SBUF, A ;Load data to transmit JNB TI, XMIT

Uploaded by

Kiran boby
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)
558 views29 pages

8051 Serial Port Programming

Here is a program to serially transfer data received from ports P0, P1, and P2 continuously at 4800 baud: AGAIN: SETB TR1 ;Start timer 1 MOV A, P0 ;Read data from P0 ACALL XMIT ;Transmit data MOV A, P1 ;Read data from P1 ACALL XMIT ;Transmit data MOV A, P2 ;Read data from P2 ACALL XMIT ;Transmit data SJMP AGAIN ;Repeat continuously XMIT: MOV SBUF, A ;Load data to transmit JNB TI, XMIT

Uploaded by

Kiran boby
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/ 29

Serial Port Programming

Ways of Data Transfer


Serial Parallel
• To transfer to a device • Often 8 or more lines (wire
located many meters conductors) are used to
• away, the serial method is transfer data to a device
used that is only a few feet away
• The data is sent one bit at a • Lot of data can be
time transferred using many
• Fast data transfer with only wires in parallel
few wires • Eg: printers, hard disks
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 serialin- 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”
Methods Of Serial Data Communication

Synchronous method Asynchronous method


• transfers a block of data at a • transfers a single byte at a
time time

IC Chips used for Serial Communication

•UART (universal asynchronous Receiver transmitter)

• USART (universal synchronous-asynchronous Receiver transmitter)


• protocol
It is a set of rules agreed by both the sender and receiver on
o How the data is packed
o How many bits constitute a character
o When the data begins and ends
• Asynchronous serial data communication is widely used for
character-oriented transmissions
o Each character is placed in between start and stop bits, this
is called framing
o 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)
• Due to the extended ASCII characters, 8-bit ASCII data is
common
• In modern PCs the use of one stop bit is standard
Data Transfer Rate
bps (bits per second)
• The rate of data transfer in serial data
communication

baud rate
• Another widely used terminology for bps
• defined as the number of signal changes per
second
Note:

• 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
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
Data communication equipments

DTE (data terminal equipment)


• It refers to terminal and computers that send and
receive data

DCE (data communication equipment)


• It 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

• 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
• 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
8051 Baud Rate
• 8051 transfers & receives data serially at many
different baud rates
• Baud rate in 8051 is programmable
• This is done using Timer 1
• Timer 1 must be programmed in mode 2 (8-bit
auto reload)
Relationship between crystal
frequency & Baud Rate
• Let crystal freq=11.0592MHz
• Machine cycle freq=11.0592 MHz/12=921.6KHz
• 8051’s serial communication UART circuitry
divides the machine cycle freq of 921.6KHz by
32 once more before it is used by timer 1 to set
baud rate
• So 921.6KHz/32= 28,800Hz
Example:
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
Notice that dividing 1/12 of the crystal frequency by 32 is the
default value upon activation of the 8051 RESET pin.
Baud Rate in 8051
PC Baud Rates Timer 1 TH1 Register values for
19200 various Baud Rates
9600
Baud TH1 TH1
4800 Rate (Decimal) (Hex)
2400
9600 -3 FD
1200
600 4800 -6 FA
300 2400 -12 F4
150 1200 -24 E8
SBUF
• It is an 8-bit register used solely for serial
communication
• For a byte of 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 deframes 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
SCON
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
• 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

REN (receive enable)


• 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
Programming Serial Data Transmitting
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
Example:
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
SJMP AGAIN ;keep sending A
Example:
Write a program for the 8051 to transfer “YES” serially at 9600
baud, 8-bit data, 1 stop bit, do this continuously
Solution:
MOV TMOD,#20H ;timer 1,mode 2(auto reload)
MOV TH1,#-3 ;9600 baud rate
MOV SCON,#50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
AGAIN: MOV A,#”Y” ;transfer “Y”
ACALL TRANS
MOV A,#”E” ;transfer “E”
ACALL TRANS
MOV A,#”S” ;transfer “S”
ACALL TRANS
SJMP AGAIN ;keep doing it
;serial data transfer subroutine
TRANS: MOV SBUF,A ;load SBUF
HERE: JNB TI,HERE ;wait for the last bit
CLR TI ;get ready for next byte
RET
Example:
Take data in through ports 0,1 and 2 one after the other and transfer
the data serially, continuously
MOV TMOD, #20H ;Timer 1, mode 2
MOV TH1, #-6 ;4800 baud rate
MOV SCON, #50H ;8 bit, 1 stop bit, REN enabled
MOV P0, #0FFH ;Make P0 an i/p port
MOV P1, #0FFH ;Make P1 an i/p port
MOV P2, #0FFH ;Make P2 an i/p port
SETB TR1 ;start timer 1
RPT: MOV A, P0
ACALL SEND ;call subroutine for transmission
MOV A, P1
ACALL SEND
MOV ,P2
ACALL SEND
SJMP RPT
Program cont….

;subroutine for transferring serially


SEND: MOV SBUF, A ;load data into SBUF
HERE: JNB T1, HERE ;wait for transmission to end
CLR TI ;clear TI for next transmission
RET
Programming Serial Data Receiving
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
Example:
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

You might also like