Serial Communication 2
Serial Communication 2
Serial Communication 2
Introduce the RS232 standard and position it within the crowded field of serial communications standards. Configure the 8051 serial port. Read and write to the serial port. Introduce software and hardware handshaking.
6.1 Introduction
There are several popular types of serial communications. Here are a few worth noting: RS232. Peer-to-peer (i.e. communications between two devices) RS485. Multi-point (i.e. communications between two or more devices) USB (Universal Serial Bus). Replaced RS232 on desktop computers. CAN (Controller Area Network). Multi-point. Popular in the automotive industry. SPI (Serial Peripheral Interface). Developed by Motorola. Synchronous master/slave communications. I2C (Inter-Integrated Circuit).Developed by Philips. Multimaster communications.
The Silicon Laboratories 8051 development kit used in this book supports RS232, SPI and I2C communications. An RS232 serial port is included on most 8051 microcontrollers. It is usually listed on the datasheet as UART.
When we talk about serial communications, what do we really mean? How is the data transmitted? Serial data is transmitted between devices one bit at a time using agreed upon electrical signals. In our C programs though, we read and write bytes to the serial port not bits. To accomplish the necessary translation between bytes and bits, another piece of hardware is required the UART.
The UART is responsible for breaking apart bytes of data and transmitting it one bit at a time (i.e. serially). Likewise, the UART receives serialized bits and converts them back into bytes. In practice, its a little more complicated, but thats the basic idea.
The UART, however, doesnt operate at the line voltages required by the RS232 standard. The UART operates at TTL voltage levels (i.e. 0 to 5V). For noise immunity and transmission length, the RS232 standard dictates the transmission of bits at a higher voltage range and different polarities (i.e. typically 9V to +9V). An external transceiver chip is needed. Binary 0: UART: 0V RS232: 3-25V Binary 1: UART: 5V RS232 -3V to -25V
2 Vcc
8 Vdrv
GND 39
GND
UART communications is asynchronous (i.e. not synchronous). This means that there is no master clock used for timing data transfer between devices. The UART is also responsible for baud rate generation. This determines the speed at which data is transmitted and received. One baud is one bit per second (bps). As of this writing, data rates can reach up to 230,400 baud. The cable length between devices is limited by the baud rate -- the higher the speed, the shorter the cable. The RS-232C standard only permits transmission speeds up to 19200 baud with a cable length of 45 feet. With modern UARTs, 230,400 baud can be achieved with a short cable length of a few feet.
0 1 2
0 0 1
Set by Timer 1
9-bit UART
We focus on mode 0 and mode1 because mode 2 and mode 3 are not often used.
Another job of the UART is to frame the byte of data that is serialized and transmitted. There is always one start bit (set to 0) and one stop bit (set to 1). Looking at it another way, for every byte of data, 10 bits are transmitted.
Description RI (Receive Interrupt). SCON.0 TI (Transmit Interrupt). SCON.1 REN (UART Receive Enable). SCON.4 SM0 and SM1 (UART Operation Mode). SCON.6, SCON.7 This is a one-byte buffer for both receive and transmit. ES (Enable Serial). IE.4 Set the bit to 1 to enable receive and transmit interrupts. PS (Priority Serial). IP.4 Set the bit to 0 for a low priority or 1 for a high priority.
IP (Interrupt Priority)
RS232 works in a restricted range of baud rates: 75, 110, 300, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 33600, 56000, 115200 and 230400. With the UART operating in mode 1, the baud rate will be generated based on a formula using the factors listed above
Where: SMOD is the normal/double baud rate bit. is the clock rate in hertz. is the machine instruction executed each clock cycle. It is one for the 8051 microcontroller used in this book. For comparison, the original 8051 by Intel used 12 clock cycles for each instruction. TRV is the reload value for the timer.
Baud Summary
Set the UART operational mode to 1. (SCON.6 = 1, SCON.7 = 0) Set the REN bit to enable UART receive. (SCON.4 = 1) Set the UART enable bit (UARTEN) in the XBR0 register. (XBR0.2 = 1) Set the bit for normal or double baud rate (SMOD) in the PCON register. (PCON.7 = 1 for double) Determine the TRV (Timer Reload Value) based on crystal frequency and desired baud rate.
Baud rates for SMOD=0 Machine cycle freq. = 12 MHz / 12 = 1 MHz and 1MHz / 32 = 28,800 Hz since SMOD = 0
Practice
The SBUF register (both transmit and receive) can only hold one byte. How do you know when the byte that you wrote to the port has been transmitted? Conversely, how do you know when a byte is available? There are ways to handle this using time delays and polling. If your application is simple enough, you may be able to get away with it. The best solution to the problem, however, is to use interrupts. The two interrupts we are interested in are TI (Transmit Interrupt) and RI (Receive Interrupt).
6.6 Handshaking
The 8051 only has a one-byte buffer SBUF. In contrast, a typical PC serial port with a UART with 16byte buffer. If SBUF is not serviced quickly enough, an incoming byte may overwrite a byte that has not yet been read and processed. Using a control technique called handshaking, it is possible to get the transmitting device to stop sending bytes until the 8051 is ready. Likewise, the 8051 can be signaled by the receiving device to stop transmitting. There are two forms of handshaking software and hardware.
Software handshaking (also called XON/XOFF) uses control characters in the byte stream to signal the halting and resuming of data transmission. Control-S (ASCII 19) signals the other device to stop sending data. Control-Q (ASCII 17) signals the other device to resume sending data. The disadvantage with this approach is that the response time is slower and two characters in the ASCII character set must be reserved for handshaking use. Hardware handshaking uses additional I/O lines. The most common form of hardware handshaking is to use two additional control wires called RTS (Ready to Send) and CTS (Clear to Send). One line is controlled by each device. The line (either RTS or CTS) is asserted when bytes can be received and unasserted otherwise. These two handshaking lines are used to prevent buffer overruns.
Typically, the connector is male for DTE equipment and female for DCE equipment. RS232 DB9 pin D-SUB male connector
There are two other less commonly used lines DTR (Data Terminal Ready) and DSR (Data Set Ready). These lines are typically used by devices signaling to each other that they are powered up and ready to communicate. To summarize, RTS/CTS are used for buffer control and DTS/DSR are used for device present and working indicators. In practice, serial communication with no handshaking uses 3 wires (TX, RX and GND). Serial communications with basic hardware handshaking uses 5 wires (TX, RX, RTS, CTS and GND).
Pin 1 2 3 4 5 6 7 8 9
Signal Name Direction(DTE DCE) CD (Carrier Detect) RXD (Receive Data) TXD (Transmit Data) DTR (Data Terminal Ready) GND (System Ground) DSR (Data Set Ready) RTS (Request To Send) CTS (Clear To Send) RI (Ring Indicator)
6.8 Summary
This chapter introduced the RS232 serial communications standard and placed it in context with newer forms of serial communications. It also discussed the role of the UART and external transceiver circuits necessary to transmit bits of data at the proper voltage. On the software side, this chapter discussed how to configure the serial port using the special function registers and also discussed issues pertaining to baud rate generation. Finally, reading and writing to the serial port was addressed and both software and hardware handshaking concepts were introduced.