Sie sind auf Seite 1von 28

Unit 2

8051 Serial Port Programming in C


By

Dr Narayana Swamy Ramaiah


Professor and HoD, Dept of CSE
FET, JU

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

Interrupt ROM for


On-chip Timer 1 Counter
Control program
RAM Timer 0 Inputs
code

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

Serial Port External Communication Using


serial port (TxD, RxD Pins)
UART
TxD

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

UART -Serial Port Unit


SBUF
Deframing

• 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

UART -Serial Port Unit


NOTE:
• SBUF is physically two registers .
• one is write only and is used to hold data to be transmitted out of the 8051
via TXD.
• The other is read only and holds received data from external sources via RXD.
Both mutually exclusive registers use address 99h.
SCON Register
• SCON is an 8-bit register used to program the start bit,
stop bit, and data bits of data framing, among other
things
SCON Register (cont.)
• 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
SCON Register (cont.)

• 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

• SCON is a bit-addressable register


SCON Register (cont.)

SMO SM1 SM2 REN TB8 RB8 TI RI

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

• PC supports several 150


baud rates. 300
• You can use Netterm, 600
terminal.exe, stty, ptty
to send/receive data. 1200
• Hyperterminal supports 2400
baud rates much higher 4800
than the ones list in
the Table. 9600 (default)
19200
Note: Baud rates supported by
486/Pentium IBM PC BIOS. 14
Baud Rates in the 8051 (1/2)
• The 8051 transfers and receives data serially at
many different baud rates by using UART.
• UART divides the machine cycle frequency by 32
and sends it to Timer 1 to set the baud rate.
• Signal change for each roll over of timer 1
11.0592 MHz
Machine cycle 28800 Hz
XTAL frequency ÷ 32
÷ 12 Timer 1
oscillator 921.6 kHz By UART To timer 1
To set the
Baud rate 15
Baud Rates in the 8051 (2/2)
• Timer 1, mode 2 (8-bit, auto-reload)
• Define TH1 to set the baud rate.
• XTAL = 11.0592 MHz
• The system frequency = 11.0592 MHz / 12 = 921.6 kHz
• Timer 1 has 921.6 kHz/ 32 = 28,800 Hz as source.
• TH1=FDH means that UART sends a bit every 3 timer source.
• Baud rate = 28,800/3= 9,600 Hz

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

Notice that dividing 1/12th of the crystal frequency by 32 is the


default value upon activation of the 8051 RESET pin.
17
Table 10–4 Timer 1 TH1
Register Values for Various
Baud Rates

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.

8-bit char bit by bit


8-bit SBUF TxD
A
zero detect shift add start and
TI UART stop bits 20
Transfer Data with the TI flag (2/2)
• Sequence continuous:
5. During the transfer of the stop bit, the 8051 raises the TI
flag, indicating that the last character was transmitted and
it is ready to transfer the next character.
6. By monitoring the TI flag, we know whether or not the
8051 is ready to transfer another byte.
– We will not overloading the SBUF register.
– If we write another byte into the SBUF before TI is raised, the
untransmitted portion of the previous byte will be lost.
– We can use interrupt to transfer data in Chapter 11.
7. After SBUF is loaded with a new byte, the TI flag bit must
be cleared by the programmer.

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.

8. To transfer the next character, go to Step 5.

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

6. Keep monitoring the Receive Interrupt (RI) to see if it is raised.


7. When RI is raised, SBUF has the whole byte. Move the content of
SBUF to a safe place.
8. To receive the next character, go to Step 5.

27
THANK YOU

Das könnte Ihnen auch gefallen