Chapter 10 8051 Serial Port Programming in Assembly and C
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
Section 10.1 Basics of Serial Communication
Figure 4-1. 8051 Pin Diagram
PDIP/Cerdip
P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST (RXD)P3.0 (TXD)P3.1 (INT0)P3.2 (INT1)P3.3 (T0)P3.4 (T1)P3.5 (WR)P3.6 (RD)P3.7 XTAL2 XTAL1 GND 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 Vcc P0.0(AD0) P0.1(AD1) P0.2(AD2) P0.3(AD3) P0.4(AD4) P0.5(AD5) P0.6(AD6) P0.7(AD7) EA/VPP ALE/PROG PSEN P2.7(A15) P2.6(A14) P2.5(A13) P2.4(A12) P2.3(A11) P2.2(A10) P2.1(A9) P2.0(A8)
8051 (8031)
Inside Architecture of 8051
External interrupts Interrupt Control On-chip ROM for program code
Timer/Counter
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
Address/Data Figure 1-2. Inside the 8051 Microcontroller Block Diagram
6
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
Simplex vs. Duplex Transmission
Simplex transmission: the data can sent in one direction.
Example: the computer only sends data to the printer. Transmitter
Device 1
Receiver
Device 2
Duplex transmission: the data can be transmitted and received.
Transmitter Receiver
Device 1
Transmitter Receiver
Device 2
8
Half vs. Full Duplex
Half duplex: if the data is transmitted one way at a time.
Transmitter Receiver
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
Figure 10-2. Simplex, Half-, and FullDuplex Transfers
Simplex Transmitter Receiver
Half Duplex
Transmitter
Receiver
Receiver
Transmitter Receiver Transmitter
10
Full Duplex
Transmitter Receiver
Parallel vs. Serial
Computers transfer data in two ways:
Parallel
data is sent a byte or more a time (fast) Only very short distance between two systems The 8-bit data path is expensive Example: printer, hard disks The data is sent one bit at a time (slow) with simple wire Relative long distance (rarely distortion) cheap For long-distance data transfers using communication lines such as a telephone, it requires a modem to modulate (0/1 to analog) and demoulate (analog to 0/1).
Serial
11
Figure 10-1. Serial versus Parallel Data Transfer (1/2)
Serial Transfer
Sender Receiver
Parallel Transfer
Sender D0 Receiver
D7
12
Figure 10-1. Serial versus Parallel Data Transfer (2/2)
Serial Transfer
D0 Sender Receiver
Parallel Transfer
D0-D7 Sender Receiver
Other control lines
Other control lines
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.
register parallel-in serial-out
11101000001011
1
register serial-in parallel-out
8-bit character
14
Asynchronous vs. Synchronous
Serial communication uses two methods:
In synchronous communication, data is sent in blocks of bytes. byte byte byte byte sender
01011111 01010101
preamble
receiver
In asynchronous communication, data is sent in bytes. byte sender byte stop bit byte start bit receiver
15
UART & USART
It is possible to write software to use both methods, but the programs can be tedious and long. Special IC chips are made for serial communication:
USART (universal synchronous-asynchronous receiver-transmitter) UART (universal asynchronous receiver-transmitter)
The 8051 chip has a built-in UART.
16
8051 Serial Communication
The 8051 has serial communication capability built into it.
Full-duplex Asynchronous mode only.
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
1 start mark bit D0 goes out last
18
goes out last D7
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.
When there is no transfer, the signal is 1 (high), which is referred to as mark.
19
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
Data Transfer Rate (1/2)
How fast is the data transferred? Three methods to describe the speed:
Baud rate is defined as the number of signal changes per second.
The rate of data transfer is stated in Hz (used in modem).
Data rate is defined as the number of bits transferred per second.
Each signal has several voltage levels. The rate of data transfer is stated in bps (bits per second).
Effective data rate is defined as the number of actual data bits transferred per second.
Redundant bits must be removed
21
Data Transfer Rate (2/2)
The data transfer rate depends on communication ports incorporated into that system.
Ex: 100-9600 bps in the early IBM PC/XT Ex: 56K bps in Pentium-based PC The baud rate is generally limited to 100kHz.
22
Example of Data Transfer Rate (1/2)
Assume that data is sent in the following asynchronous mode:
2400 baud rate each signal has 4 voltage levels (-5V, -3V, 3V, 5V) one start bit, 8-bit data, 2 stop bits Time (D0 first)
stop stop mark bit bit start mark bit
23
00
11
10
01
10
00
11
11
8-bit character
Example of Data Transfer Rate (2/2)
2400 baud = 2400 signals per second =2400 Hz 4 voltage level
Log24=2, 2 bits is sent in every signal change
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
RS232 Voltage Level
The input and output voltage of RS232 is not of the TTL compatible.
RS232 is older than TTL.
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
DTE and DCE
DTE (Data Terminal Equipment)
DTE refers to PC, 8051, or other equipments.
DCE (Data Communication Equipment)
DCE refers to communication equipment, such as modems, that are responsible for transferring the data. DTE view DCE view
DTE
TxD
RxD
PC GND Com1
DCE
RxD TxD GND modem
DCE Telephone Line
RxD TxD GND modem
DTE
TxD RxD GND PC Com1
29
Figure 10-4. RS232 Connector DB-25
13
14
25
30
Table 10-1: RS232 Pins (DB-25) for DTE (1/2)
Pin 1 2 3 4 5 6 7 8 Description Protective ground Transmitted data (TxD) Received data (RxD) Request to send (RTS) Clear to send (CTS) Data set ready (DSR) Signal ground (GND) Data carrier detect (DCD)
9/10 11 12 13
Reserved for data testing Unassigned Secondary data carrier detect Secondary clear to send
31
Table 10-1: RS232 Pins (DB-25) for DTE (2/2)
Pin 14
15
Description Secondary transmitted data
Transmit signal element timing
16 17 18
19 20 21 22 23 24 25
Secondary received data Receive signal element timing Unassigned
Secondary request to sent Data terminal ready (DTR) Signal quality detector Ring indicator (RI) Data signal rate select Transmit signal element timing Unassigned
32
Figure 10-5. DB-9 9-Pin Connector
33
Table 10-2: IBM PC DB-9 Signals for DTE
Pin 1 Description Data carrier detect (DCD)
2
3 4 5 6 7 8
Received data (RxD)
Transmitted data (TxD) Data terminal ready (DTR) Signal ground (GND) Data set ready (DSR) Request to send (RTS) Clear to send (CTS)
Ring indicator (RI)
34
DB9 - DB25 conversion
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
RS232 Handshaking Signals
Many of the pins of the RS232 connector are used for handshaking signals.
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)
36
Communication Flow (1/2)
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
Communication Flow (2/2)
While signals DTR and DSR are used by the PC and modem, respectively, to indicate that they are alive and well. TRS and CTS control the flow of data. When the PC wants to send data, it asserts RTS. If the modem is ready (has room) to accept the data, it sends back CTS. If, for lack of room, the modem does not activate CTS, and PC will de-assert DTR and try again.
38
IBM PC/compatible COM ports
IBM PC has 2 COM ports.
Both COM ports have RS232-type connectors. For mouse, modem
We can connect the 8051 serial port to the COM port of a PC for serial communication experiments.
DTE view PC
RS232
DTE view 8051
COM 1 port
MAX232
UART
39
Null Modem Connection
The simplest connection between a PC and microcontroller requires a minimum of three pins, TxD, RxD, and GND.
Figure 10-6 shows null modem connection DTE
TxD
DTE
TxD
DTE
TxD
DTE
TxD
RxD
ground
RxD
RxD
ground
RxD
PC Com1
8051-based board
PC Com1
PC Com1
40
RS422 & RS485
By using RS232, the limit distance between two PCs is about 15m. It works well even the distance=30m. If you want to transfer data with long distance (ex: 300m), you can use RS422 or RS485.
41
Section 10.2 8051 Connection to RS232
42
TxD and RxD pins in the 8051
Many of the pins of the RS232 connector are used for handshaking signals. However, they are not supported by the 8051 UART chips. In 8051, the data is received from or transmitted to
RxD: received data (Pin 10, P3.0) TxD: transmitted data (Pin 11, P3.1)
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
Figure 10-7 (a): Inside MAX232
Vcc + 22 mF C1 + C2 11 1 3 4 C3 + C4 + 14 13 7 8
46
16
2 6
MAX232
12
10
5 T1IN R1OUT T2IN R2OUT 15
T1OUT R1IN T2OUT R2IN
TTL side
RS232 side
Figure 10-7: (b) MAX232s Connection to the 8051 (Null Modem)
8051
MAX232
P3.1 11 TxD
11 14
12 13
2 5 3
P3.0 10 RxD
DB-9
TTL-compatible RS232-compatible
47
Figure 10-8: (a) Inside MAX233
Vcc 13 14 12 17 2 3 1 20 7 11 15 16 10 5 4 18 19
MAX233
T1IN R1OUT T2IN R2OUT 6
T1OUT R1IN T2OUT R2IN
TTL side
RS232 side
48
Figure 10-8: (b) MAX233s Connection to the 8051 (Null Modem)
8051 MAX233 P3.1 11 TxD
P3.0 10 RxD
2 5 2 5
DB-9
TTL-compatible RS232-compatible
49
Section 10.3 8051 Serial Port Programming in Assembly
50
PC Baud Rates
110 bps
PC supports several baud rates.
You can use netterm, terminal.exe, stty, ptty to send/receive data.
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.
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 frequency 921.6 kHz
XTAL oscillator
12
32
28800 Hz
By UART To timer 1
To set the Baud rate
52
Timer 1
Baud Rates in the 8051
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
53
Example 10-1 (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.
54
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
Registers Used in Serial Transfer Circuit
SBUF (Serial data buffer) SCON (Serial control register) PCON (Power control register) You can see Appendix H (pages 609-611) for details. PC also has several registers to control COM1, COM2.
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)
SM0 SM1 SM2 REN TB8 RB8
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 (Receive Enable)
SCON.4 Set/cleared by software to enable/disable reception.
REN=1
It enable the 8051 to receive data on the RxD pin of the 8051. If we want the 8051 to both transfer and receive data, REN must be set to 1. SETB SCON.4
REN=0
The receiver is disabled. The 8051 can not receive data. CLR SCON.4
60
TI (Transmit Interrupt Flag)
SCON.1 When the 8051 finishes the transfer of the 8-bit character, it raises the TI flag. TI is raised by hardware at the beginning of the stop bit in mode 1. Must be cleared by software.
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
TB8 (Transfer Bit 8)
SCON.3 For multiprocessor communication The 9th bit that will be transmitted in mode 2 & 3.
TB8 = 1: address TB8 = 0: data
Set/Cleared by software.
64
RB8 (Receive Bit 8)
SCON.2 For multiprocessor communication In serial mode 1, RB8 gets a copy of the stop bit when an 8-bit data is received. In serial mode 2 & 3, RB8 gets a copy of the 9th bit. In fact, it is the TB8 in transmitter.
65
Figure 10-9. SCON Serial Port Control Register (Bit Addressable)
SM0 SM1 SM2 REN TB8 RB8
SM0 SM1 SM2 REN TB8 RB8 SCON.7 SCON.6 SCON.5 SCON.4 SCON.3 SCON.2 Serial port mode specifier Serial port mode specifier Used for multiprocessor communication. (Make it 0) Set/cleared by software to enable/disable reception. Not widely used. Not widely used.
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
Note: Make SM2, TB8, and RB8 = 0.
Transfer Data with the TI flag (1/2)
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 TxD SBUF A
zero detect
TI
shift add start and UART stop bits
67
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.
68
Programming the 8051 to Transfer data Serially (1/2)
1. Use the timer 1 in mode 2
MOV TMOD,#20H
2. Set the value TH1 to chose baud rate. Look at the Table 10-4.
MOV TH1,#FDH ;Baud rate = 9600bps
3. Set SCON register in mode 1.
MOV SCON,#50H
4. Start the timer.
SETB TR1
69
Programming the 8051 to Transfer data Serially (2/2)
raise when sending the stop bit
5. Clear TI flag.
CLR TI
TI=0 transfer data
6. The character byte to be transferred serially is written into the SBUF register.
MOV SBUF,#A
7. Keep monitoring the Transmit Interrupt (TI) to see if it is raised.
HERE: JNB TI, HERE
8. To transfer the next character, go to Step 5.
70
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
Example 10-3 (1/2)
Write a program to transfer the message YES serially at 9600 baud, 8-bit data, 1 stop bit. Do this continuously. Solution: MOV TMOD,#20H ;timer 1, mode 2 MOV TH1,#-3 ;9600 baud MOV SCON,#50H SETB TR1 AGAIN:MOV A,#Y ;transfer Y ACALL TRANS MOV A,#E ;transfer E ACALL TRANS MOV A,#S ;transfer S ACALL TRANS SJMP AGAIN ;keep doing it
72
Example 10-3 (2/2)
;serial data transfer subroutine TRANS:MOV SBUF,A ;load SBUF HERE: JNB TI,HERE ;wait for last bit to transfer CLR TI ;get ready for next byte RET
73
Receive Data with the RI flag (1/2)
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.
bit by bit RxD
REN=1
shift register
shift
8-bit
load
SBUF
RI
A
74
UART
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.
75
Programming the 8051 to Receive data Serially (1/2)
1. Use the timer 1 in mode 2
MOV TMOD,#20H
2. Set the value TH1 to chose baud rate. Look at the Table 10-4.
MOV TH1,#FDH ;Baud rate = 9600bps
3. Set SCON register in mode 1.
MOV SCON,#50H
4. Start the timer.
SETB TR1
76
Programming the 8051 to Receive data Serially (2/2)
raise when getting the stop bit
5. Clear RI flag.
CLR RI
RI=0 receive data
6. Keep monitoring the Receive Interrupt (RI) to see if it is raised.
HERE: JNB RI, HERE
7. When RI is raised, SBUF has the whole byte. Move the content of SBUF to a safe place.
MOV A,SBUF
8. To receive the next character, go to Step 5.
77
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
Example 10-5 (1/4)
Assume that the 8051 serial port is connected to the COM port of the IBM PC, and on the PC we are using the terminal.exe program to send and receive data serially. P1 and P2 of the 8051 are connected to LEDs and switches, respectively. Write an 8051 program to (a) send to the PC the message We Are Ready, (b) receive any data sent by the PC and put it on LEDs connected to P1, and (c) get data on switches connected to P2 and send it to the PC serially. The program should perform part (a) once, but parts (b) and (c) continuously. Use the 4800 baud rate.
79
Example 10-5 (2/4)
8051 To PC COM port Solution: TxD P1 LED
RxD 0 P2,#0FFH TMOD,#20H TH1,#0FAH SCON,#50H TR1 DPTR,#MYDATA
P2
SW
ORG MOV MOV MOV MOV SETB MOV
;make P2 an input port ;4800 baud rate ;start timer 1 ;load pointer for message
80
Example 10-5 (3/4)
H_1: MOV DPTR,#MYDATA ;load pointer for message CLR A MOVC A,@A+DPTR ;get the character JZ B_1 ;if last character get out ACALL SEND INC DPTR SJMP H_1 ;next character MOV A,P2 ;read data on P2 ACALL SEND ;transfer it serially ACALL RECV ;get the serial data MOV P1,A ;display it on LEDs SJMP B_1 ;stay in loop indefinitely
(a)
(c) (b)
B_1:
81
Example 10-5 (4/4)
;-----------serial data transfer. ACC has the data SEND: MOV SBUF,A ;load the data H_2: JNB TI,H_2 ;stay here until last bit gone CLR TI ;get ready for next char RET ;-------------- Receive data serially in ACC RECV: JNB RI,RECV ;wait here for char MOV A,SBUF ;save it in ACC CLR RI ;get ready for next char RET ;---------------The message to send MYDATA:DB We Are Ready,0 END
82
Doubling the Baud Rate in the 8051
There are two ways to increase the baud rate of data transfer in the 8051:
1. To use a higher frequency crystal.
It is not feasible in many situations since the system crystal is fixed. Many new crystal may not be compatible with the IBM PC serial COM ports baud rate.
2. To change a bit in the PCON register.
This is a software way by setting SMOD=1.
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
* PCON is not bit-addressable. See Appendix H. p410
SMOD Flag of the PCON Register
Power control register: PCON
MOV A, PCON SETB ACC.7 MOV PCON,A ;we dont want to modify other bits An 8-bit register Not bit-addressable SCOM=0: default SCOM=1: double the baud rate See Table 10-5
85
Table 10-5: Baud Rate Comparison for SMOD = 0 and SMOD =1
TH1 (Decimal) (Hex) SMOD = 0 SMOD = 1
-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
Baud Rates for SMOD=0
When SMOD=0, the 8051 divides 1/12 of the crystal frequency by 32, and uses that frequency for timer 1 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=256 - Crystal frequency/(12*32*Baud rate)
Default on reset
See Appendix A, page 363
87
Baud Rates for SMOD=1
When SMOD=1, the 8051 divides 1/12 of the crystal frequency by 16, and uses that frequency for timer 1 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/ 16 = 57,600 Hz as source. TH1=256 - Crystal frequency/(12*16*Baud rate)
88
Example 10-6 (1/2)
Assuming that XTAL = 11.0592 MHz for the following program, state (a) what this program does, (b) compute the frequency used by timer 1 to set the baud rate, and (c) find the baud rate of the data transfer. Solution:
(a) This program transfers ASCII letter B (01000010 binary) continuously. (b) and (c) With XTAL = 11.0592 MHz and SMOD = 1 11.0592 / 12 = 921.6 kHz machine cycle frequency. 921.6 /16 = 57,600 Hz frequency used by timer 1 to set the baud rate. 57,600 / 3 = 19,200, the baud rate.
89
Example 10-6 (2/2)
MOV SETB MOV MOV MOV MOV SETB MOV A_1:CLR MOV H_1:JNB SJMP A,PCON ACC.7 PCON,A TMOD,#20H TH1,#-3 SCON,#50H TR1 A,#`B TI SBUF,A TI H_1 A_1
;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
Timer 1 TH1 Register Values for Various Baud Rates
93
Table 105
Baud Rate Comparison for SMOD = 0 and SMOD = 1
94
Section 10.4 Programming the Second Serial Port
95
Second Serial Port
Many of the new generations of the 8051 microcontrollers come with two serial ports.
DS89C4x0, DS89C320 But not all versions of 8051/52 microcontroller.
There two ports are called as Port 0 and Port 1.
Port 0 is as same as the original 8051. Port 1 uses new pins and new registers. See Table 10-6 and Figure 10-12 for registers See Figure 10-10 for 89C4x0 See Figure 10-11 for connection with MAX232
96
Table 106 SFR Byte Addresses for DS89C4x0 Serial Ports
shared
as same as the original 8051 serial port
Compare with p556 Table A-2
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
Figure 1010 DS89C4x0 Pin Diagram.
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
Figure 1011 Connection with MAX232
(a) Inside MAX232
(b) MAX232s Connection to the DS89C4x0
101
Assembly Language for Second Port
The older 8051 assemblers do not support the second serial port, we need to define them by their SFR address.
Example 10-11.
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
Example 10-11 (1/2)
Write a program for the second serial port of the DS89C4x0 to continuously transfer the letter A serially at 4800 baud. User 8-bit data and 1 stop bit. User Timer 1. Solution:
SBUF1 SCON1 TI1 RI1 ORG MAIN: MOV MOV MOV SETB EQU 0C1H ;defined by yourself EQU 0C0H BIT 0C1H BIT 0C0H 0H TMOD,#20H TH1,#-6 SCON1,#50H TR1
103
Example 10-11 (2/2)
ANAIN: MOV A,#A ACALL SENDCON2 SJMP AGAIN
;send A continuously
SENDCOM2: MOV SBUF1,A JNB TI1,HERE CLR TI1 RET END
;send by using port 1
104
Section 10.5 Serial Port Programming in C
105
Transmitting and Receiving Data in C
Using C to program the serial port.
Example 10-15 does the same work as Example 10-11.
Please read other examples by yourself.
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
You are able to (1/2)
Contract and compare serial versus parallel communication List the advantages of serial communication over parallel Explain serial communication protocol Contrast synchronous versus asynchronous communication Contrast half- versus full-duplex transmission Explain the process of data framing Describe data transfer rate and bps rate
108
You are able to (2/2)
Define the RS232 standard Explain the use of the MAX232 and MAX232 chips Interface the 8051 with an RS232 connector Discuss the baud rate of the 8051 Describe serial communication features of the 8051 Program the 8051 for serial data communication Program the 8051 serial port in Assembly and C Program the second serial port of DS89C4x0 in Assembly and C
109
Homework
Chapter 10 Problems3,8,15,36,38,39,47,62
110