8051 Serial Ports One of the 8051’s many powerful features is its integrated Universal Asynchronous Receiver Transmitter (UART ), otherwise known as a serial port. With integrated serial port of 8051, data can be transmitted and received easily by reading and writing the data to the serial port registers. The features of the 8051 serial ports are Full duplex operation. Receive Buffered. Access using single double buffered Register SBUF. Four different modes of operation. Option to use fixed baud rate or programmable baud rate.
8051 Serial Ports Full Duplex serial port means that it can transmit and receive data simultaneously. It is also receive-buffered, meaning it can commence reception of a second byte before a previously received byte has been read from the SBUF register. However, if the first byte still hasn’t been read by the time reception of the second byte is complete, one of the bytes will be lost. The serial port receive and transmit registers are both accessed at Special Function Register SBUF.
8051 Serial Ports Data to be transmitted are written to the register SBUF and the data received by the serial port are read from the register SBUF. Physically reading and writing SBUF actually accesses two separate registers. This technique of having same address for two different registers is called double buffering.
SERIAL PORT CONTROL SFRS The serial port of 8051 is controlled by two registers in SFR area of 8051 as shown in Table The two registers are Serial Port control registers; SCON and serial port buffer register SBUF. SFR Name Description SFR Address SCON Serial port control register 98h SBUF Serial port buffer register 99h
SERIAL PORT CONTROL SFRS In addition to the above two registers, the MSB of PCON register named as SMOD bit is used to double the baud rate of serial transmission and reception. If SMOD bit is set to 1, then the baud rate is doubled. The individual bits of SCON have the functions as shown in Table . As the SCON register has many individual status bits, the individual bits of this register are bit addressable. The bit address is also given in Table . The programmer can use these bit addresses to check the status of the serial port and set the mode individually.
Bit patterns for SCON (98h) SFR Bit Name Bit Address Explanation of Function D7 SM0 9Fh Serial port mode select bits D6 SM1 9Eh D5 SM2 9Dh Multiprocessor Communications Enable bit D4 REN 9Ch Receiver Enable. This bit must be set in order to receive characters. D3 TB8 9Bh Transmit bit 8. The 9th bit to transmit in mode 2 and 3. D2 RB8 9Ah Receive bit 8. The 9th bit received in mode 2 and 3. D1 TI 99h Transmit Interrupt Flag. Set when a byte has been completely transmitted. D0 RI 98h Receive Interrupt Flag. Set when a byte has been completely received.
Bit Pattern - Description D7 and D6 bits of the SCON register define the operation modes of the serial port and the basic operating modes are given in Table. The SM0 and SM1 bits can select any one of the four operating modes described in the next section.
Definition of Bits SM0 and SM1 in SCON SFR
Bit Pattern - Description The next bit, SM2, is a flag used for enabling "Multiprocessor communication" in modes 2 and 3. If SM2 is set to 1 in modes 2 and 3, the “Receive Interrupt” RI flag will not be activated if the received 9th data bit is 0. If SM2 is set in Mode1, then “Receive Interrupt” RI flag will not be activated if valid stop bit is not received. This can be useful in certain advanced serial applications. It can be now assumed that SM2 bit has to be cleared so that RI flag will be set when any character is received.
Bit Pattern - Description The next bit, REN, is "Receiver Enable ." This bit is set in order to receive the characters from the receive data line of the serial port. The TB8 bit is used in modes 2 and 3. In modes 2 and 3, a total of nine data bits are transmitted. The first 8 bits are the 8 bits of the data to be transmitted, and the ninth bit is taken from TB8. The RB8 also operates in modes 2 and 3 on the reception side.
Bit Pattern - Description When a byte is received in modes 2 or 3, a total of nine bits are received. In this case, the first eight bits received are the data of the serial byte received and the value of the ninth bit received will be placed in RB8. TI means "Transmit Interrupt." When a program writes a data to the serial port buffer SBUF, then the serial port will start shifting this data in the serial transmit line bit by bit at the predefined clock speed or baud rate. 8051 will give TI signal to the programmer after sending the data completely.
Bit Pattern - Description Upon sensing the TI bit set to 1, the programmer can then write the next data for transmission. When the TI bit is set, the programmer may assume that the serial port is "free" and ready to send the next byte. Finally, the RI bit means "Receive Interrupt." Whenever a data is received on the receive data line of the serial port, this serial data will be shifted in to a buffer and then stored in the SBUF register. Setting of RI bit indicates that a byte has been received. Upon sensing the RI bit set to 1, the programmer may read the data from the SBUF.
SBUF SBUF is physically two registers with the same address. When data to be transmitted is written to the SBUF register, then it will be shifted bit by bit into the TXD line of 8051. The port 3 pin 3.1 acts as the TXD line . The shifting is done by the transmit clock which determines the baud rate. Similarly, when the data bits are received on the RXD line (Pin 3.0 of port 3), the bits are shifted serially into the shift register according to the Receive clock. After the reception is complete, the data received will be placed on SBUF from where it can be read by the programmer through the internal bus.
Reception / Transmission Mode 0: In this mode serial data is entering and exiting through RxD pin. So, in mode 0, Full duplex is not possible – meaning that both transmission and reception cannot take place simultaneously. TxD pin outputs the shift clock. 8 bits are transmitted/received (LSB first). The baud rate is fixed at 1/12 the oscillator frequency. Transmission is started by writing a data byte to the SBUF register and once the transmission is complete, TI flag is set The reception is started by enabling REN in SCON register. Once the data reception is complete, the RI flag is set The baud rate in Mode 0 is fixed at one twelfth of the clock frequency. Baud rate= (Clock frequency/12)
Reception / Transmission Mode 1: In this mode, 10 bits are transmitted through TxD and simultaneously 10 bits can be received through RxD . The 10 bits are made up of a start bit (0), 8 data bits (LSB first), and a stop bit (1). On completion of reception, the stop bit goes into RB8 in Special Function Register SCON. The baud rate is variable and is set by the Timer 1 overflow rate. The baud rate for mode 1 is fixed at the following rate.
Reception / Transmission Baud rate= (Timer 1 overflow rate /16) if SMOD bit in PCON SFR is set to 1. Baud rate= (Timer 1 overflow rate /32) if SMOD bit in PCON SFR is set to 0. The MSB of PCON register can be set or reset by the programmer. The baud rate can be doubled by setting the MSB of PCON.
Reception / Transmission In order to generate the baud rate clock from timer 1, Timer 1 can be configured to act as timer in auto reload mode with the timer 1 interrupt disabled. As in mode 0, the transmission is initiated by writing a data to SBUF register. Reception is initiated by a 1 to 0 transition that is the start bit received and also when REN of SCON SFR is 1.
Reception / Transmission Mode 2: In this mode, 11 bits are transmitted through TxD or received through RxD . The 11 bits are made up of one start bit (always 0), 8 data bits (LSB first), a programmable 9th data bit, and a stop bit (always 1). The 9th data bit transmitted is same as TB8 bit in SCON special function register. It can be assigned the value of 0 or 1 by the programmer. Or, for example, the parity bit (P, in the PSW) could be moved into TB8. On reception, the 9th data bit goes into RB8 in Special Function Register SCON, while the stop bit is ignored. The baud rate is programmable to either 1/32 or 1/64 of the oscillator clock frequency. Baud rate= (Clock frequency /32) if SMOD bit in PCON SFR is set to 1. Baud rate= (Clock frequency /64) if SMOD bit in PCON SFR is set to 0.
Reception / Transmission Mode 3: In this mode, 11 bits are transmitted through TxD and simultaneously 11 bits are received through RxD . The 11bits are made up of a start bit (0), 8 data bits (LSB first), a programmable 9th data bit, and a stop bit (1). In fact, Mode 3 is the same as Mode 2 in all respects except baud rate. The baud rate in Mode 3 is variable. The baud rate for mode 3 is fixed at the following rate similar to mode1. Baud rate= (Timer 1 overflow rate /16) if SMOD bit in PCON SFR is set to 1. Baud rate= (Timer 1 overflow rate /32) if SMOD bit in PCON SFR is set to 0.
Reception / Transmission In all four modes, transmission is initiated by any instruction that uses SBUF as a destination register. Reception is initiated in Mode 0 by the condition RI = 0 and REN = 1. Reception is initiated in the other modes by the incoming start bit when REN = 1. The flow chart 11.20 shows the steps in programming the serial port of 8051. These steps are detailed in the following sections and also in the example programs.