1
The 8051 Microcontroller
and Embedded Systems
Chapter 10
8051 Serial Communications
From the text by Mazidi & Mazidi (2000)
2
Serial Vs Parallel
3
Serial Transfers
4
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
5
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
6
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.
7
8
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
9
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.
10
• 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
11
• 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
12
Programming the 8051 to transfer serially
a. Initialize the timer 1 in mode 2 to perform auto reload.
b. Set the baud
c. Load SCON with 50H, indicating serial mode1 where 8 bits data
are transmit with one start and one stop bit
d. Set TR1 to start timer
e. clear interrupt flag, CLR, TI
f. Character byte to be transfer serially is being written into SBUF
g. TI flag is monitored with the use of JNB TI, XX
h. To transfer the next character, go to step e
13
Programming the 8051 to receive data serially
a. Initialize the timer 1 in mode 2 to perform auto reload.
b. Set the baud
c. Load SCON with 50H, indicating serial mode1 where 8 bits data
are transmit with one start and one stop bit
d. Set TR1 to start timer
e. clear interrupt flag, CLR, RI
f. Character byte to be transfer serially is being written into SBUF
g. TI flag is monitored with the use of JNB RI, XX
h. To transfer the next character, go to step e
14
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
15
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?
16
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
17
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
18
DOUBLING THE BAUDS RATE
There are two ways to increase the baud rate of
data transfer
To use a higher frequency crystal
To change a bit in the PCON register
PCON register is an 8-bit register
When 8051 is powered up, SMOD is zero
We can set it to high by software and thereby
double the baud rate
19
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
20
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

Serial.ppt and it contains all information

  • 1.
    1 The 8051 Microcontroller andEmbedded Systems Chapter 10 8051 Serial Communications From the text by Mazidi & Mazidi (2000)
  • 2.
  • 3.
  • 4.
    4 Serial Transfers A protocolis 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
  • 5.
    5 Asynchronous • Bit timingis 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
  • 6.
    6 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.
  • 7.
  • 8.
    8 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
  • 9.
    9 SCON • SCON, serialcontrol register, is used for serial control. Mode 0: In this mode the serial port function as half duplex serial port with fixed baud rate.
  • 10.
    10 • REN (receiveenable) 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
  • 11.
    11 • RI (receiveinterrupt) 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
  • 12.
    12 Programming the 8051to transfer serially a. Initialize the timer 1 in mode 2 to perform auto reload. b. Set the baud c. Load SCON with 50H, indicating serial mode1 where 8 bits data are transmit with one start and one stop bit d. Set TR1 to start timer e. clear interrupt flag, CLR, TI f. Character byte to be transfer serially is being written into SBUF g. TI flag is monitored with the use of JNB TI, XX h. To transfer the next character, go to step e
  • 13.
    13 Programming the 8051to receive data serially a. Initialize the timer 1 in mode 2 to perform auto reload. b. Set the baud c. Load SCON with 50H, indicating serial mode1 where 8 bits data are transmit with one start and one stop bit d. Set TR1 to start timer e. clear interrupt flag, CLR, RI f. Character byte to be transfer serially is being written into SBUF g. TI flag is monitored with the use of JNB RI, XX h. To transfer the next character, go to step e
  • 14.
    14 Output? MOV TMOD,#20H ;timer1,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
  • 15.
    15 Problems 1. Write aprogram 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?
  • 16.
    16 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
  • 17.
    17 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
  • 18.
    18 DOUBLING THE BAUDSRATE There are two ways to increase the baud rate of data transfer To use a higher frequency crystal To change a bit in the PCON register PCON register is an 8-bit register When 8051 is powered up, SMOD is zero We can set it to high by software and thereby double the baud rate
  • 19.
    19 Baud rates forSMOD=0 Machine cycle freq. = 11.0592 MHz / 12 = 921.6 kHz and 921.6 kHz / 32 = 28,800 Hz since SMOD = 0
  • 20.
    20 Baud rates forSMOD=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