Serial Data Input/Output
Computers must be able to communicate with other computers in modern multiprocessor distributed systems. One cost-effective way to communicate is to send and receive data bits serially. The 8051 has a serial data communication circuit that uses register SBUF to hold data. Register SCON controls data communication, register PCON controls data rates, and pins RXD (P3.0) and TXD (P3.1) connect to the serial data network.
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.
There are four programmable modes for serial data communication that are chosen by setting the SMX bits in SCON. Baud rates are determined by the mode chosen. Figure 13 shows the bit assignments for SCON and PCON.
Serial Data Interrupts
Serial data communication is a relatively slow process, occupying many milliseconds per data byte to accomplish. In order not to tie up valuable processor time, serial data flags are
FIGURE 13 SCON and PCON Function Registers
THE SERIAL PORT CONTROL (SCON) SPECIAL FUNCTION REGISTER
Bit |
symbol |
Function |
||||||||||||||||||||
7 |
SM0 |
Serial port mode bit 0. Set/cleared by program to select mode. |
||||||||||||||||||||
6 |
SM1 |
Serial port mode bit 1. Set/cleared by program to select mode.
|
||||||||||||||||||||
5 |
SM2 |
Multiprocessor communications bit. Set/cleared by program to enable multiprocessor communications in modes 2 and 3. When set to 1 an interrupt is generated if bit 9 of the received data is a 1; no interrupt is generated if bit 9 is a 0. If set to 1 for mode 1, no interrupt will be generated unless a valid stop bit is received. Clear to 0 if mode 0 is in use. |
||||||||||||||||||||
4 |
REN |
Receive enable bit. Set to 1 to enable reception; cleared to 0 to dissable reception. |
||||||||||||||||||||
3 |
TB8 |
Transmitted bit B. Set/cleared by program in modes 2 and 3. |
||||||||||||||||||||
2 |
RB8 |
Received bit B. Bit B of received data in modes 2 and 3; stop bit in mode 1. Not used in mode 0. |
||||||||||||||||||||
1 |
T1 |
Transmit interrupt flag. Set to one at the end of bit 7 time in mode 0, and at the beginning of the stop bit for other modes. Must be cleared by the program. |
||||||||||||||||||||
0 |
R1 |
Receive interrupt flag. Set to one at the end of bit 7 time in mode 0, and halfway through the stop bit for other modes. Must be cleared by the program. |
Bit addressable as SCON.O to SCON.7
THE POWER MODE CONTROL (PCON) SPECIAL FUNCTION REGISTER
Bit |
symbol |
Function |
7 |
SMOD |
Serial baud rate modify bit. Set to 1 by program to double baud rate using timer 1 for modes 1, 2, and 3. Cleared to 0 by program to use timer 1 baud rate. |
6-4 |
– |
Not implemented. |
3 |
GF1 |
General purpose user flag bit 1. Set/cleared by program. |
2 |
GF0 |
General purpose user flag bit 0 . Set/cleared by program. |
1 |
PD |
Power down bit. Set to 1 by program to enter power down configuration for CHMOS processors. |
0 |
IDL |
Idle mode bit. Set to 1 by program to enter idle mode configuration for CHMOS processors. PCON is not bit addressable. |
included in SCON to aid in efficient data transmission and reception. Notice that data transmission is under the complete control of the program, but reception of data is unpredictable and at random times that are beyond the control of the program.
The serial data flags in SCON. T1 and R1, are set whenever a data byte is transmitted (T1) or received (R1). These flags are ORed together to produce an interrupt to the program. The program must read these flags to determine which caused the interrupt and then clear the flag. This is unlike the timer flags that are cleared automatically; it is the responsibility of the programmer to write routines that handle the serial data flags.
Data Transmission
Transmission of serial data bits begins anytime data is written to SBUF. T1 is set to a 1 when the data has been transmitted and signifies that SBUF is empty (for transmission purposes) and that another data byte can be sent. If the program fails to wait for the T1 flag and overwrites SBUF while a previous data byte is in the process of being transmitted, the results will be unpredictable (a polite term for "garbage out").
Data Reception
Reception of serial data will begin if the receive enable bit (REN) in SCON is set to 1 for all modes. In addition, for mode 0 only, R1 must be cleared to 0 also. Receiver interrupt flag R1 is set after data has been received in all modes. Setting REN is the only direct program control that limits the reception of unexpected data; the requirement that R1 also be 0 for mode 0 prevents the reception of new data until the program has dealt with the old data and reset R1.
Reception can begin in modes 1, 2, and 3 if R1 is set when the serial stream of bits begins. R1 must have been reset by the program before the last bit is received or the incoming data will be lost. Incoming data is not transferred to SBUF until the last data bit has been received so that the previous transmission can be read from SBUF while new data is being received.
Serial Data Transmission Modes
The 8051 designers have included four modes of serial data transmission that enable data communication to be done in a variety of ways and a multitude of baud rates. Modes are selected by the programmer by setting the mode bits SM0 and SM1 in SCON. Baud rates are fixed for mode 0 and variable, using timer 1 and the serial baud rate modify bit (SMOD) in PCON, for modes 1. 2. and 3.
Serial Data Mode 0-Shift Register Mode
Setting bits SM0 and SM 1 in SCON to 00b configures SBUF to receive or transmit eight data bits using pin RXD for both functions. Pin TXD is connected to the internal shift frequency pulse source to supply shift pulses to external circuits. The shift frequency, or baud rate, is fixed at 1/12 of the oscillator frequency, the same rate used by the timers when in the timer configuration. The TXD shift clock is a square wave that is low for machine cycle states S3-S4-S5 and high for S6-S1-S2. Figure 14 shows the timing for mode 0 shift register data transmission.
When transmitting, data is shifted out of RXD; the data changes on the falling edge of S6P2, or one clock pulse after the rising edge of the output TXD shift clock. The system designer must design the external circuitry that receives this transmitted data to receive the data reliably based on this timing.
FIGURE 14 Shift Register Mode 0 Timing
Received data comes in on pin RXD and should be synchronized with the shift clock produced at TXD. Data is sampled on the Jailing edge of S5P2 and shifted in to SBUF on the rising edge of the shift clock.
Mode 0 is intended not for data communication between computers, but as a high-speed serial data-collection method using discrete logic to achieve high data rates. The baud rate used in mode 0 will be much higher than standard for any reasonable oscillator frequency; for a 6 megahertz crystal, the shift rate will be 500 kilohertz.
Serial Data Mode 1-Standard UART
When SM0 and SM1 are set to 01b, SBUF becomes a 10-bit full-duplex receiver/ transmitter that may receive and transmit data at the same time. Pin RXD receives all data, and pin TXD transmits all data. Figure 15 shows the format of a data word.
Transmitted data is sent as a start bit, eight data bits (Least Significant Bit, LSB, first), and a stop bit. Interrupt flag T1 is set once all ten bits have been sent. Each bit interval is the inverse of the baud rate frequency, and each bit is maintained high or low over that interval.
Received data is obtained in the same order; reception is triggered by the falling edge of the start bit and continues if the stop bit is true (0 level) halfway through the start bit interval. This is an anti-noise measure; if the reception circuit is triggered by noise on the transmission line, the check for a low after half a bit interval should limit false data reception.
Data bits are shifted into the receiver at the programmed baud rate, and the data word will be loaded to SBUF if the following conditions are true: R1 must be 0, and mode bit SM2 is 0 or the stop bit is 1 (the normal state of stop bits). R1 set to 0 implies that the program has read the previous data byte and is ready to receive the next; a normal stop bit will then complete the transfer of data to SBUF regardless of the state of SM2. SM2 set to 0 enables the reception of a byte with any stop bit state, a condition which is of limited use in this mode, but very useful in modes 2 and 3. SM2 set to 1 forces reception of only "good" stop bits, an anti-noise safeguard.
Of the original ten bits, the start bit is discarded, the eight data bits go to SBUF. and the stop bit is saved in bit RB8 of SCON. R1 is set to 1, indicating a new data byte has been received.
FIGURE 15 Standard UART Data Word
If R1 is found to be set at the end of the reception, indicating that the previously received data byte has not been read by the program. or if the other conditions listed are not true, the new data will not be loaded and will be lost.
Mode 1 Baud Rates
Timer 1 is used to generate the baud rate for mode I by using the overflow flag of the timer to determine the baud frequency. Typically. timer I is used in timer mode 2 as an autoload R-hit timer that generates the baud frequency:
SMOD is the control hit in PCON and can be 0 or 1, which raises the 2 in the equation to a value of 1 or 2.
If timer 1 is not run in timer mode 2, then the baud rate is
and timer 1 can be run using the internal clock or as a counter that receives clock pulses from any external source via pin T1.
The oscillator frequency. is chosen to help generate both standard and nonstandard baud rates. If standard baud rates are desired, then an 11.0592 megahertz crystal could be selected. To get a standard rate of 9600 hertz then, the setting of TH 1 may be found as
follows:
if SMOD is cleared to 0 .
Serial Data Mode 2-Multiprocessor Mode
Mode 2 is similar to mode 1 except 11 bits are transmitted: a start bit, nine data bits. and a stop bit, as shown in Figure 16. The ninth data bit is gotten from bit TB8 in SCON during transmit and stored in bit RB8 of SCON when data is received. Both the start and stop bits are discarded.
The baud rate is programmed as follows:
figure 16 Multiprocessor Data Word
Here, as in the case for mode 0, the baud rate is much higher than standard communication rates. This high data rate is needed in many multi-processor applications. Data can be collected quickly from an extensive network of communicating microcontrollers if high baud rates are employed.
The conditions for setting RI for mode 2 are similar to mode 1: RI must be 0 before the last bit is received, and SM2 must be 0 or the ninth data bit must be a 1. Setting RI based upon the state of SM2 in the receiving 8051 and the state of bit 9 in the transmitted message makes multiprocessing possible by enabling some receivers to be interrupted by certain messages, while other receivers ignore those messages. Only those 8051’s that have SM2 set to 0 will be interrupted by received data which has the ninth data bit set to 0; those with SM2 set to 1 will not be interrupted by messages with data bit 9 at 0. All receivers will be interrupted by data words that have the ninth data bit set to 1; the state of SM2 will not block reception of such messages.
This scheme allows the transmitting computer to "talk" to selected receiving computers without interrupting other receiving computers. Receiving computers can be commanded by the "talker" to "listen" or "deafen" by transmitting coded byte(s) with the ninth data bit set to 1 . The I in data bit 9 interrupts all receivers. instructing those that are programmed to respond to the coded byte(s) to program the state of SM2 in their respective SCON registers. Selected listeners then respond to the bit 9 set to 0 messages. while all other receivers ignore these messages. The talker can change the mix of listeners by transmitting bit 9 set to 1 messages that instruct new listeners to set SM2 to 0, while others are instructed to set SM2 to 1 .
Serial Data Mode 3
Mode 3 is identical to mode 2 except that the baud rate is determined exactly as in mode 1 . using Timer I to generate communication frequencies.
Thanks gazillions for this post 🙂
Thanks gazillions for this post 🙂
do u have a program for multiprocessor communication