Microcontroller Serial Communication
8051 has a serial port which is full duplex,hence it can transmit and receive simultaneously. The port is a receive buffered, due to this port can start receiving byte before the previous bytes has already been read from the register SBUF. There will be collision if the byte arrives in SBUF before the previous byte is read from it.
The serial port receive/transmit registers are both accessed at SBUF. The serial port can operate in 4 modes as mentioned below.
SCON(Serial Control Register), BIT Addressable
The serial port control and status register is the Special Function Register SCON. This register contains not only the mode selection bits, but also the 9th data bit for transmit and receive (TB8 and RB8), and the serial ports interrupt bits (TI and RI).
SM0-SCON.7- Serial port mode specifier(Table-1)
SM1-SCON.6- Serial port mode specifier(Table-1)
SM2-SCON.5- Enable multiprocessor communication in modes 2/3.
REN-SCON.4- Set/clear by software to enable/disable reception
TB8-SCON.3- The 9th bit that will be transmitted in mode2/3, set/clear by software
RB8-SCON.2- In mode2/3, it is the 9th bit that was received. In mode 1, if SM2=0, RB8
is the stop bit that was received, In mode 0, it is not used.
TI-SCON.1- Transmit Intterupt flag, set by hardware at the end of 8th bit time in mode 0,
at the beginning of the stop bit in the other modes, it must be cleared by software.
RI-SCON.0- receive Interrupt flag, set by hardware and must be cleared by software.
Based on SM0 and SM1 various baudrates are selected as shown in the table below.
|0||1||1,8 bit UART,Variable|
|1||0||2,9 bit UART,(Fosc./64) OR (Fosc./32)|
|1||1||3,9 bit UART, Variable|
Microcontroller Serial Port Initialization C program
As shown in the C program, it initializes timer 0 for delay and timer 1 for baud rate of 9600 bps.
Transmission via SBUF
SBUF=transmit_array[i++]; /* Transmitting character array/*