Serial Port 8051
Serial Port 8051
Serial Port 8051
Chapter 10
8051 Serial Communications
2
Serial Transfers
3
Serial Transfers
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
4
Asynchronous
• Bit timing is important since both
transmitter and receiver need to be in
agreement of timing.
• Bit Width = 1/Baud rate or bit rate
• 9600 Baud = bit width of 104uS
5
Setting Baud Rate
• For Baud rate, OSC/12 then /32.
• Timer 1 in Mode 2 is used as Baud Rate
Generator.
• 28,800/3 = 9600 baud
• 28,800/12 = 2400 baud
• 28,800/24 = 1200 baud
• And so on.
6
7
SBUF
• Serial buffer, SBUF is used to hold data to
be transmitted on TxD, and holds received
data on RxD.
• Note that there are actually 2 SBUFs, but
one is read only and one is write only.
MOV SBUF, A
MOV A, SBUF
8
SCON
• SCON, serial control register, is used for
serial control.
Mode 0: In this
mode the serial port
function as half
duplex serial port
with fixed baud
rate.
9
• REN (receive enable)
SCON is a bit-addressable register
When REN 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
10
• 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
11
Programming the 8051 to transfer serially
a. Initialize the timer 1 in mode 2 to perform auto reload.
12
Programming the 8051 to receive data serially
a. Initialize the timer 1 in mode 2 to perform auto reload.
13
Output?
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
14
Problems
1. Write a program to transfer the letter ‘y’ serially at 9600
baud connected to P3.1 continuously.
2. Write program to take data through ports P0, P1, and P2
one after another and transfer the data serially one by one.
3. Write the program to receive the data which has been send
in serial form and send it out to port 0 in parallel form.
Save the data at RAM location 60H
4. Write the program to transfer the numbers 1 to 9 serially.
5. Discuss how baud in 8051 can be doubled?
15
Problem 1
MOV TMOD, #20H
MOV TH1, #-3
MOV SCON, #50H
SETB TR1
CLR TI
AGAIN:MOV SBUF, #‘Y’
HERE: JNB TI, HERE
CLR TI
SJMP AGAIN
16
Problem 2
MOV TMOD, #20H
MOV TH1, #-3
MOV SCON, #50H
MOV P0,#0FFH ; MAKE P0 input port
MOV P0,#0FFH
MOV P0,#0FFH
SETB TR1
CLR TI
AGAIN: MOV A, P0
ACALL SEND
MOV A, P1
ACALL SEND
MOV A, P2
ACALL SEND
SJMP AGAIN
SEND: MOV SBUF, A
HERE: JNB TI, HERE
CLR TI
RET
17
DOUBLING THE BAUDS RATE
18
Baud rates for SMOD=0
Machine cycle freq. = 11.0592 MHz / 12 = 921.6 kHz
and
921.6 kHz / 32 = 28,800 Hz since SMOD = 0
19
Baud rates for SMOD=1
Machine cycle freq. = 11.0592 MHz / 12 = 921.6 kHz
and
921.6 kHz / 16 = 57,600 Hz since SMOD = 1
MOV A, PCON
SETB ACC.7
MOV PCON, A
20