SERIAL COMMUNICATION
BASICS OF SERIAL COMMUNICATION:
- Parallel
- Often 8 or more lines (wire conductors) are used to transfer data to a device that is only a few feet away
- Serial
- To transfer to a device located many meters away, the serial method is used
- The data is sent one bit at a time
- At the transmitting end, the byte of data must be converted to serial bits using parallel-in-serial-out shift register
- At the receiving end, there is a serialin- parallel-out shift register to receive the serial data and pack them into byte
- When the distance is short, the digital signal can be transferred as it is on a simple wire and requires no modulation
- If data is to be transferred on the telephone line, it must be converted from 0s and 1s to audio tones
- This conversion is performed by a device called a modem, “Modulator/demodulator”
Methods of Serial Communication:
- Synchronous method transfers a block of data at a time
- Asynchronous method transfers a single byte at a time
Half- and Full- Duplex Transmission
- If data transmitted one way a time, it is referred to as half duplex
- If data can go both ways at a time, it is full duplex
- This is contrast to simplex transmission
Data Transfer Rate
- The rate of data transfer in serial data communication is stated in bps (bits per second)
- Another widely used terminology for bps is baud rate
- It is modem terminology and is defined as the number of signal changes per second
- In modems, there are occasions when a single change of signal transfers several bits of data
- 28,800 / 3 = 9600 where -3 = FD (hex) is loaded into TH1
- 28,800 / 12 = 2400 where -12 = F4 (hex) is loaded into TH1
- 28,800 / 24 = 1200 where -24 = E8 (hex) is loaded into TH1
SBUF Register
- SBUF is an 8-bit register used solely for serial communication
- For a byte data to be transferred via the TxD line, it must be placed in the SBUF register
- The moment a byte is written into SBUF, it is framed with the start and stop bits and transferred serially via the TxD line
- SBUF holds the byte of data when it is received by 8051 RxD line
- When the bits are received serially via RxD, the 8051 deframes it by eliminating the stop and start bits, making a byte out of the data received, and then placing it in SBUF
SCON Register
SM0 | SCON.7 | Serial port mode specifier |
SM1 | SCON.6 | Serial port mode specifier |
SM2 | SCON.5 | Used for multiprocessor communication |
REN | SCON.4 | Set/cleared by software to enable/disable reception |
TB8 | SCON.3 | Not widely used |
RB8 | SCON.2 | Not widely used |
TI | SCON.1 | Transmit interrupt flag. Set by HW at the begin of the stop bit mode 1. And cleared by SW |
RI | SCON.0 | Receive interrupt flag. Set by HW at the begin of the stop bit mode 1. And cleared by SW |
- They determine the framing of data by specifying the number of bits per character, and the start and stop bits
- Only MODE1 is of our interest.
SM0 | SM1 | |
0 | 0 | Serial Mode 0 |
0 | 1 | Serial Mode 1, 8-bit data, 1 stop bit, 1 start bit |
1 | 0 | Serial Mode 2 |
1 | 1 | Serial Mode 3 |
- This enables the multiprocessing capability of the 8051
- It is a bit-adressable register
- When it is high, it allows 8051 to receive data on RxD pin
- If low, the receiver is disable
- When 8051 finishes the transfer of 8-bit character
- It raises TI flag to indicate that it is ready to transfer another byte
- TI bit is raised at the beginning of the stop bit
- When 8051 receives data serially via RxD, it gets rid of the start and stop bits and places the byte in SBUF register
- It raises the RI flag bit to indicate that a byte has been received and should be picked up before it is lost
- RI is raised halfway through the stop bit
Programming Serial Data Transmitting
- TMOD register is loaded with the value 20H, indicating the use of timer 1 in mode 2 (8-bit auto-reload) to set baud rate
- The TH1 is loaded with one of the values to set baud rate for serial data transfer
- The SCON register is loaded with the value 50H, indicating serial mode 1, where an 8- bit data is framed with start and stop bits
- TR1 is set to 1 to start timer 1
- TI is cleared by CLR TI instruction
- The character byte to be transferred serially is written into SBUF register
- The TI flag bit is monitored with the use of instruction JNB TI,xx to see if the character has been transferred completely
- To transfer the next byte, go to step 5
Importance of TI Flag
- The byte character to be transmitted is written into the SBUF register
- The start bit is transferred
- The 8-bit character is transferred on bit at a time
- The stop bit is transferred
- It is during the transfer of the stop bit that 8051 raises the TI flag, indicating that the last character was transmitted
- By monitoring the TI flag, we make sure that we are not overloading the SBUF
- If we write another byte into the SBUF before TI is raised, the untransmitted portion of the previous byte will be lost
- After SBUF is loaded with a new byte, the TI flag bit must be forced to 0 by CLR TI in order for this new byte to be transferred
- It must be noted that TI flag bit is raised by 8051 itself when it finishes data transfer
- It must be cleared by the programmer with instruction CLR TI
- If we write a byte into SBUF before the TI flag bit is raised, we risk the loss of a portion of the byte being transferred
- The instruction JNB TI,xx
- Using an interrupt
Programming Serial Data Receiving
- TMOD register is loaded with the value 20H, indicating the use of timer 1 in mode
- (8-bit auto-reload) to set baud rate
- TH1 is loaded to set baud rate
- The SCON register is loaded with the value 50H, indicating serial mode 1, where an 8- bit data is framed with start and stop bits
- TR1 is set to 1 to start timer 1
- RI is cleared by CLR RI instruction
- The RI flag bit is monitored with the use of instruction JNB RI,xx to see if an entire character has been received yet
- When RI is raised, SBUF has the byte, its contents are moved into a safe place
- To receive the next character, go to step 5
Importance of RI Flag
- It receives the start bit
- Indicating that the next bit is the first bit of the character byte it is about to receive
- The 8-bit character is received one bit at time
- The stop bit is received
- When receiving the stop bit 8051 makes RI = 1, indicating that an entire character byte has been received and must be picked up before it gets overwritten by an incoming character
- By checking the RI flag bit when it is raised, we know that a character has been received and is sitting in the SBUF register
- We copy the SBUF contents to a safe place in some other register or memory before it is lost
- After the SBUF contents are copied into a safe place, the RI flag bit must be forced to 0 by CLR RI in order to allow the next received character byte to be placed in SBUF
- Failure to do this causes loss of the received character
- If we failed to copy SBUF into a safe place, we risk the loss of the received byte
- It must be noted that RI flag bit is raised by 8051 when it finish receive data
- It must be cleared by the programmer with instruction CLR RI
- If we copy SBUF into a safe place before the RI flag bit is raised, we risk copying garbage
- The RI bit can be checked by
- The instruction JNB RI,xx
- Using an interrupt
Doubling Baud Rate
- To use a higher frequency crystal (The System Crystel is Fix)
- To change a bit in the PCON register
- When 8051 is powered up, SMOD is zero
- We can set it to high by software and thereby double the baud rate