Professional Documents
Culture Documents
Objective
8051 (serial communication) programmer bit bit IBM PC 8051 8051 RS232 PC COM port RS232 MAX232 IC 8051 registers 8051
2
Sections
10.1 10.2 10.3 10.4 10.5 Basics of serial communication 8051 connection to RS232 8051 serial port programming in Assembly Programming the second serial port Serial port programming in C
8051 (8031)
On-chip RAM
Timer 1 Timer 0
Counter Inputs
CPU
Serial Port
OSC
Bus Control
4 I/O Ports
P0 P1 P2 P3
TxD RxD
8051 and PC
The 8051 module connects to PC by using RS232. RS232 is a protocol which supports full-duplex, synchronous/asynchronous, serial communication. We discuss these terms in following sections.
RS232
PC
COM 1 port MAX232
8051
UART
Receiver
Device 2
Transmitter Receiver
Device 2
8
Receiver
Transmitter
Full duplex: if the data can go both ways at the same time.
Two wire conductors for the data lines.
Transmitter Receiver
Receiver
Transmitter
Half Duplex
Transmitter
Receiver
Receiver
Transmitter Receiver Transmitter
10
Full Duplex
Transmitter Receiver
Serial
11
Serial Transfer
Sender Receiver
Parallel Transfer
Sender D0 Receiver
D7
12
Serial Transfer
D0 Sender Receiver
Parallel Transfer
D0-D7 Sender Receiver
13
Serial Communication
How to transfer data?
Sender:
The byte of data must be converted to serial bits using a parallel-in-serial-out shift register. The bit is transmitted over a single data line.
Receiver
The receiver must be a serial-in-parallel-out shift register to receive the serial data and pack them into a byte.
11101000001011
1
8-bit character
14
preamble
receiver
In asynchronous communication, data is sent in bytes. byte sender byte stop bit byte start bit receiver
15
How to detect that a character is sent via the line in the asynchronous mode?
Answer: Data framing!
17
Framing (1/3)
Each character is placed in between start and stop bits. This is called framing.
Figure 10-3. Framing ASCII A (41H) Time (D0 first)
stop bit
mark
Framing (2/3)
The start bit is 0 (low) and always one bit. The LSB is sent out first. The stop bits is 1 (high). The stop bit can be one (if 8 bits used in ASCII) or two bits (if 7 bits used in ASCII).
It is programmed for data that is 7 or 8 bits.
Framing (3/3)
We have a total of 10 bits for each character:
8-bits for the ASCII code 2-bits for the start and stop bits 20% overhead
In some systems in order to maintain data integrity, the parity bit is included in the data frame.
In an odd-parity bit system the total number of bits, including the parity bit, is odd. UART chips allow programming of the parity bit for odd-, even-, and no-parity options.
20
Effective data rate is defined as the number of actual data bits transferred per second.
Redundant bits must be removed
21
22
00
11
10
01
10
00
11
11
8-bit character
Data rate = 2 * 2400 Hz = 4800 bps Effective ratio = 8 / (1+8+2) =8/11 Effective data rate = data rate * effective ratio = 4800 * 8 /11=3490.9 bps
24
RS232 Standard
RS232 is an interfacing standard which is set by the Electronics Industries Association (EIA) in 1960.
RS232 is the most widely used serial I/O interfacing standard. RS232A (1963), RS232B (1965) and RS232C (1969), now is RS232E
Define the voltage level, pin functionality, baud rate, signal meaning, communication distance.
25
RS 232 Voltage
25V
We must use voltage converter (also referred to as line driver) such as MAX232 to convert the TTL logic levels to the RS232 voltage level, and vice versa.
MAX232, TSC232, ICL232
logic 0
3V -3V undefined
logic 1
-25V
26
MAX232
MAX232 IC chips are commonly referred to as line drivers.
RS232
PC 8051
COM 1 port
MAX232
RS232 voltage level
UART
TTL voltage level
27
RS232 pins
Figure 10-4 shows the RS232 connector DB-25. Table 10-1 shows the pins and their labels for the RS232 cable.
DB-25P : plug connector (male) DB-25S: socket connector (female)
Figure 10-5 shows DB9 connector and Table 10-2 shows the signals.
IBM version for PC.
All the RS 232 pin function definitions of Tables 10-1 and 10-2 are from the DTE point of view.
28
DCE
RxD TxD GND modem
DTE
TxD RxD GND PC Com1
29
13
14
25
30
9/10 11 12 13
Reserved for data testing Unassigned Secondary data carrier detect Secondary clear to send
31
16 17 18
19 20 21 22 23 24 25
33
2
3 4 5 6 7 8
34
DB9 1 2 3 4 5 6 7 8 9
DB25 8 3 2 20 7 6 4 5 22
Function Data carrier detect Receive data Transmit data Data terminal ready Signal ground Data set ready Request to send Clear to send Ring indicator 35
36
PC (DTE)
RI
modem (DCE)
DCD
DTR
DTR (data terminal ready) DSR (data set ready) RTS (request to send) CTS (clear to send) DCD (carrier detect, or data carrier detect) RI (ring indicator)
DSR
RTS CTS TxD, RxD
Telephone is ringing Connection between two modems is set PC is ready modem is ready PC wants to sent data modem is ready to receive 37 transmit data
We can connect the 8051 serial port to the COM port of a PC for serial communication experiments.
DTE view PC
RS232
COM 1 port
MAX232
UART
39
DTE
TxD
DTE
TxD
DTE
TxD
RxD
ground
RxD
RxD
ground
RxD
PC Com1
8051-based board
PC Com1
PC Com1
40
41
42
TxD and RxD of the 8051 are TTL compatible. The 8051 requires a line driver to make them RS232 compatible.
One such line driver is the MAX232 chip.
43
MAX232 (1/2)
MAX232 chip converts from RS232 voltage levels to TTL voltage levels, and vice versa.
MAX232 uses a +5V power source which is the same as the source voltage for the 8051. 8051
MAX232
P3.1 TxD
11
11 14 13
2 5 3
P3.0 10 RxD
12
DB-9
44
MAX232 (2/2)
MAX232 has two sets of line drivers.
Figure 10.7 shows the inside of MAX232. MAX232 requires four capacitors ranging from 1 to 22 mF. The most widely used value for these capacitors is 22mF.
MAX233 performs the same job as the MAX232 but eliminates the need for capacitors.
Note that MAX233 and MAX232 are not pin compatible. Figure 10.8 (a) shows the inside of MAX233 Figure 10.8 (b) shows the connection to the 8051
45
16
2 6
MAX232
12
10
TTL side
RS232 side
11 14
12 13
2 5 3
P3.0 10 RxD
DB-9
TTL-compatible RS232-compatible
47
MAX233
TTL side
RS232 side
48
DB-9
TTL-compatible RS232-compatible
49
50
PC Baud Rates
110 bps
150 300
600
1200 2400 4800 9600 (default) 19200
Note: Baud rates supported by 486/Pentium IBM PC BIOS.
51
Hyperterminal supports baud rates much higher than the ones list in the Table.
XTAL oscillator
12
32
28800 Hz
By UART To timer 1
To set the Baud rate
52
Timer 1
53
Table 10-4: Timer 1 TH1 Register Values for Various Baud Rates
Baud Rate TH1 (Decimal) 9600 -3 4800 -6 2400 -12 1200 -24 Note: XTAL = 11.0592 MHz. TH1 (Hex) FD FA F4 E8
55
56
SBUF Register
Serial data register: SBUF
MOV SBUF,#A ;put char A to transmit MOV SBUF,A ;send data from A MOV A,SUBF ;receive and copy to A An 8-bit register Set the usage mode for two timers
For a byte of data to be transferred via the TxD line, it must be placed in the SBUF. SBUF holds the byte of data when it is received by the 8051s RxD line.
Not bit-addressable
57
SCON Register
Serial control register: SCON
SM0, SM1 Serial port mode specifier REN (Receive enable) set/cleared by software to enable/disable reception. TI Transmit interrupt flag. RI Receive interrupt flag. SM2 = TB8 = TB8 =0 (not widely used) (MSB) (LSB)
TI
RI
58
* SCON is bit-addressable.
SM0, SM1
SM1 and SM0 determine the framing of data.
SCON.6 (SM1) and SCON.7 (SM0) Only mode 1 is compatible with COM port of IBM PC. See Appendix A.3. SM1 SM0 0 0 0 1 1 0 1 1 Mode 0 1 2 3 Operating Mode Baud Rate Shift register Fosc./12 8-bit UART Variable by timer1 9-bit UART Fosc./64 or Fosc./32 9-bit UART Variable
59
REN=0
The receiver is disabled. The 8051 can not receive data. CLR SCON.4
60
61
RI (Receive Interrupt)
SCON.0 Receive interrupt flag. When the 8051 receives data serially via RxD, it gets rid of the start and stop bits and place the byte in the SBUF register. Set by hardware halfway through the stop bit time in mode 1. Must be cleared by software.
62
SM2
SCON.5 SM2 enables the multiprocessor communication for mode 2 & 3.
SM2=0 : Single processor environment SM2=1 : multiprocessor environment
1
TxD
5
RxD
data
data
1 address=5
63
1 bit
8 bits
Set/Cleared by software.
64
65
TI
RI
TI
RI
SCON.1
SCON.0
Transmit interrupt flag. Set by hardware at the beginning of the stop bit in mode 1. Must be cleared by software.
Receive interrupt flag. Set by hardware halfway through the stop bit time in mode 1. Must be cleared by software.
66
TI
67
7. After SBUF is loaded with a new byte, the TI flag bit must be cleared by the programmer.
68
2. Set the value TH1 to chose baud rate. Look at the Table 10-4.
MOV TH1,#FDH ;Baud rate = 9600bps
5. Clear TI flag.
CLR TI
6. The character byte to be transferred serially is written into the SBUF register.
MOV SBUF,#A
Example 10-2
Write a program for the 8051 to transfer letter A serially at 4800 baud, continuously. Solution: MOV MOV MOV SETB AGAIN: MOV HERE: JNB CLR SJMP TMOD,#20H TH1,#-6 SCON,#50H TR1 SBUF,#A TI,HERE TI AGAIN ;timer 1, mode 2 ;4800 baud rate ;8-bit,1 stop,REN enabled ;start timer 1 ;letter A to be transferred ;wait for the last bit ;clear TI for next char ;keep sending A
71
72
73
shift register
shift
8-bit
load
SBUF
RI
A
74
UART
5. After SBUF is copied into a safe place, the RI flag bit must be cleared by the programmer.
75
2. Set the value TH1 to chose baud rate. Look at the Table 10-4.
MOV TH1,#FDH ;Baud rate = 9600bps
5. Clear RI flag.
CLR RI
7. When RI is raised, SBUF has the whole byte. Move the content of SBUF to a safe place.
MOV A,SBUF
Example 10-4
Program 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 MOV MOV SETB HERE: JNB MOV MOV CLR SJMP TMOD,#20H TH1,#-6 SCON,#50H TR1 RI,HERE A,SBUF P1,A RI HERE ;timer1, mode 2 (auto reload) ;4800 baud ;8-bit, 1 stop, REN enabled ;start timer 1 ;wait for char to come in ;save incoming byte in A ;send to port 1 ;get ready to receive next byte ;keep getting data
78
79
P2
SW
;make P2 an input port ;4800 baud rate ;start timer 1 ;load pointer for message
80
(a)
(c) (b)
B_1:
81
82
83
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)
SMOD
(LSB)
--
--
--
GF1 GF2
PD
IDL
84
-3
-6 -12
FD
FA F4
9,600
4,800 2,400
19,200
9,600 4,800
-24
E8
1,200
2,400
Note: XTAL = 11.0592 MHz. 11.0592 MHz SMOD = 1 To Machine 57600 Hz timer 1 16 cycle freq. to set 28800 Hz baud 921.6 kHz 32 rate SMOD = 0
XTAL oscillator
12
86
Default on reset
See Appendix A, page 363
87
88
;SMOD=1, double baud rate ;Timer 1, mode 2,auto reload ;19200 baud rate ;8-bit data,1 stop bit, RI enabled ;start Timer 1 ;transfer letter B ;make sure TI=0 ;transfer it ;check TI ;do again
90
Example 10-7
Find the TH1 value (in both decimal and hex) to set the baud rate to each of the following: (a) 9600 Hz (b) 4800 Hz if SMOD =1 Assume that XTAL = 11.0592 MHz. Solution: With XTAL = 11.0592 and SMOD = 1, 11.0592 / 12 = 921.6 kHz machine cycle frequency. 921.6 /16 = 57,600 Hz frequency used by the timer 1 (a) 57,600 / 9600 = 6 TH1 = -6 or TH1 = FAH. (b) 57,600 / 4800 = 12 TH1 = -12 or TH1 = F4H.
91
Example 10-8
Find the baud rate if TH1 = -2, SMOD = 1, and XTAL = 11.0592 MHz. Is this baud rate supported by IBM/compatible PCs? Solution: With XTAL = 11.0592 and SMOD = 1, we have timer 1 frequency = 57,600 Hz. The baud rate is 57,600 / 2 = 28,800. This baud rate is not supported by the BIOS of the PC; however, the PC can be programmed to do data transfer at such a speed. The software of many modems can do this. Also, Hyperterminal in Windows 95 (and higher) supports this and other baud rates.
92
Table 104
93
Table 105
94
95
96
shared
new SCON1,SBUF1
97
Table 107 SFR Addresses for the DS89C4x0 (420, 430, etc.)
98
Figure 1012 SCON0 and SCON1 Bit Addresses (TI and RI bits must be noted)
99
port 1
port 0
Note: Notice P1.2 and P1.3 pins are used by Rx and Tx lines of the 2nd serial port
100
101
Also, many C compiler do not support the second serial port, we have to declare the byte addresses of the new SFR register by using the sfr keyword.
Example 10-20
102
103
;send A continuously
104
105
106
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; } }
107
109
Homework
Chapter 10 Problems3,8,15,36,38,39,47,62
110