Serial Communication Programming in C
Serial Communication Programming in C
Session 2
1
Example 10-15
Write an 8051 C program to transfer the letter “A” serially at 4800 baud
continuously. Use 8-bit data and 1 stop bit.
Solution :
#include <reg51.h>
void main(void) {
TMOD=0x20;
SCON=0x50;
TH1=0xFA; //4800 baud rate
TR1=1;
while (1) {
SBUF =‘A’;
while (TI==0);
TI=0;
}
} 2
Inside Architecture of 8051
External interrupts
On-chip Timer/Counter
CPU
Bus Serial
4 I/O Ports
OSC Control Port
P0 P1 P2 P3 TxD RxD
Address/Data
Figure 1-2. Inside the 8051 Microcontroller Block Diagram
3
8051 Registers related to Serial
Communication
• SBUF Register -- to hold data
• SCON Register -- controls data communication
• PCON Register -- controls data rates
Serial Port in 8051
SBUF
RxD
8051 Microcontroller
SBUF Register
• SBUF is an 8-bit register used 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.
Data Bus
BYTE
Framing
Stop Start
Bit BYTE Bit TxD
SBUF
• 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.
Data Bus
10101010 Deframing
BYTE Start
BYTE Stop
Bit
Bit
0 10101010 1 RxD
SBUF
• SM2
• This enables the multiprocessing capability of the 8051
SCON Register (cont.)
• 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
0 1 0 1 0 0 0 0
5 0
Value to be loaded into SCON Register to configure it as 8bit, 1stop bit, 1 start bit, REN enabled
= 50H
PCON Register
SMOD Double baud rate. If Timer 1 is used to generate baud and SMOD=1, the
baud rate is doubled when the Serial Port is used in modes 1,2,3.
GF1,GF0 General purpose flag bit.
PD Power down bit. Setting this bit activates “Power Down” operation in the
80C51BH. (precedence)
IDL Idle Mode bit. Setting this bit activates “Idle Mode” operation in the
80C51BH.
(MSB (LSB
)
SMOD -- -- -- GF1 GF2 PD )IDL
13
* PCON is not bit-addressable. See Appendix H. p410
PC Baud Rates
110 bps
16
Example 10-1
With XTAL = 11.0592 MHz, find the TH1 value needed to have the
following baud rates. (a) 9600 (b) 2400 (c) 1200
Solution:
With XTAL = 11.0592 MHz, we have:
The frequency of system clock = 11.0592 MHz / 12 = 921.6 kHz
The frequency sent to timer 1 = 921.6 kHz/ 32 = 28,800 Hz
(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
18
Example 10-15
Write an 8051 C program to transfer the letter “A” serially at 4800 baud
continuously. Use 8-bit data and 1 stop bit.
Solution :
#include <reg51.h>
void main(void) {
TMOD=0x20;
SCON=0x50;
TH1=0xFA; //4800 baud rate
TR1=1;
while (1) {
SBUF =‘A’;
while (TI==0);
TI=0;
}
} 19
Steps for Programming Transfer
Data
• The following sequence is the steps that the 8051 goes through in
transmitting a character via TxD:
1. The byte character to be transmitted is written into the SBUF register.
2. It transfers the start bit.
3. The 8-bit character is transferred one bit at a time.
4. The stop bit is transferred.
21
Programming the 8051 to Transfer
data Serially (1/2)
1. Use the timer 1 in mode 2
• TMOD,#20H
2. Set the value TH1 to chose baud rate. Look at the Table 10-4.
• TH1,#0FDH ;Baud rate = 9600Hz
3. Set SCON register in mode 1.
• SCON,#50H
4. Start the timer.
• TR1 = 1
22
Programming the 8051 to Transfer
data Serially (2/2) raise when
sending the stop
5. Clear TI flag. bit
TI=0
• TI = 0 transfer data
6. The character byte to be transferred serially is written into the SBUF
register.
• SBUF,#’A’
7. Keep monitoring the Transmit Interrupt (TI) to see if it is raised.
23
Steps to Receive Data
• The following sequence is the steps that the 8051 goes through in
receiving a character via RxD:
1. 8051 receives the start bit indicating that the next bit is the first bit of the
character to be received.
2. The 8-bit character is received one bit at a time. When the last bit is
received, a byte is formed and placed in SBUF.
remove/check the
start and stop bits
bit by bit 8-bit 8-bit
RxD shift regiter SBUF A
shift load
REN=1 UART RI 24
Receive Data with the TI flag (2/2)
• Sequence continuous:
3. The stop bit is received. During receiving the stop bit, the
8051 make RI=1, indicating that an entire character was
been received and must be picked up before it gets
overwritten by an incoming character.
4. By monitoring the RI flag, we know whether or not the
8051 has received a character byte.
– If we fail to copy SBUF into a safe place, we risk the loss of the
received byte.
– We can use interrupt to transfer data in Chapter 11.
5. After SBUF is copied into a safe place, the RI flag bit must
be cleared by the programmer.
25
Programming the 8051 to Receive
data Serially (1/2)
1. Use the timer 1 in mode 2
• TMOD,#20H
2. Set the value TH1 to chose baud rate. Look at the Table 10-4.
• TH1= #0FDH ;Baud rate = 9600Hz
3. Set SCON register in mode 1.
• SCON = #50H
4. Start the timer.
• TR1 = 1
26
Programming the 8051 to Receive
data Serially (2/2) raise when getting
the stop bit
5. Clear RI flag.
RI=0
receive data
27
THANK YOU