Introduction
Serial communication refers to the transfer of data one bit at a time, while parallel communication allows for the simultaneous transfer of multiple bits, depending on the number of available data lines.
There are two methods of serial communication:
- Synchronous Communication: Transfers bulk data in a framed structure simultaneously.
- Asynchronous Communication: Transfers a single byte of data in a framed structure at a time.
The 8051 microcontroller features a built-in UART, with the RXD (serial data receive pin) and TXD (serial data transmit pin) located on PORT3.0 and PORT3.1, respectively.
Data transmission rate
- The data transmission rate is measured in bits per second (bps) and is also referred to as baud rate in the binary system, indicating the number of signal changes per second.
- Standard baud rates supported are:
- 1200 bps
- 2400 bps
- 4800 bps
- 19200 bps
- 38400 bps
- 57600 bps
- 115200 bps
- Typically, 9600 bps is used when speed is not a critical factor.
Interface Standard
- The 8051 serial communication operates at TTL voltage levels:
- 0 V represents logic 0
- 5 V represents logic 1
- For serial communication, computers and many older devices use the RS232 protocol with a DB9 connector.
- The RS232 protocol employs different voltage levels than those used by the 8051 serial communication:
- +3 V to +25 V for logic 0
- -3 V to -25 V for logic 1
- To enable communication with the RS232 protocol, a voltage level converter, such as the MAX232 IC, is necessary.
- Although the DB9 connector has nine pins, only three are required for basic communication:
- 2nd pin: Tx (Transmit)
- 3rd pin: Rx (Receive)
- 5th pin: GND (Ground)
8051 Serial Interface Diagram
MAX232 interfacing With 8051 MCU
8051 UART Programming
Baud Rate calculation:
To understand the calculation for achieving a baud rate of 9600 using the 8051 microcontroller’s UART, let’s break down the process step by step:
1. Crystal Frequency and Machine Cycle
- The 8051 microcontroller uses a crystal oscillator to generate its clock signal. A commonly used frequency for the crystal is 11.0592 MHz.
- The machine cycle frequency of the 8051 is calculated by dividing the crystal frequency by 12:
Machine Cycle Frequency = Crystal Frequency/12 = 11.0592 MHz/12 ≈921.6 kHz
The internal UART (Universal Asynchronous Receiver/Transmitter) block of the 8051 divides the machine cycle frequency by 32 to derive the frequency used for serial communication:
2. UART Frequency
- The internal UART (Universal Asynchronous Receiver/Transmitter) block of the 8051 divides the machine cycle frequency by 32 to derive the frequency used for serial communication:
UART Frequency = Machine Cycle Frequency/32 = 921.6 kHz /32 ≈28,800 Hz
3. Achieving the Desired Baud Rate
- The desired baud rate for serial communication is 9600 bps. Since the UART frequency is 28,800 Hz, we need to divide this frequency to achieve the desired baud rate:
Division Factor=UART Frequency/Desired Baud Rate=28,800 Hz/9600 bps=3
4. Using Timer 1 in Mode 2
- To achieve this division, Timer 1 can be configured in mode 2, which is the auto-reload mode. In this mode, the timer counts up to a certain value and then overflows, generating an interrupt or an event.
- To set the timer for a division factor of 3, you need to load the TH1 register (the high byte of Timer 1) with a value that will allow the timer to overflow every 3 cycles.
5. Calculating the Timer Value
- The timer needs to be loaded with a value that will allow it to overflow after counting to 3. The formula for the timer value in mode 2 is:Timer Count=256−Desired Count\text{Timer Count} = 256 – \text{Desired Count}Timer Count=256−Desired Count
- Since we want the timer to overflow every 3 cycles, we need to calculate how long it takes for the timer to count to 3. In this case, the timer will count up to 3 from 0. The maximum value the timer can hold is 255 (in an 8-bit timer).
Thus, the value to load into TH1 is:TH1=256−3=253\text{TH1} = 256 – 3 = 253TH1=256−3=253
6. Summary
By setting the TH1 register to 253, Timer 1 in mode 2 will cause the timer to overflow every 3 machine cycles (or every 3 counts). This effectively divides the 28,800 Hz frequency by 3, resulting in a baud rate of 9600 bps.
8051 Microcontroller Baud Rate calculation
Division factor to achieve different baud rates
Baud Rate | TH1 (Hex) |
---|---|
9600 | FD |
4800 | FA |
2400 | F4 |
1200 | E8 |
8051 Serial communication Registers
SBUF: Serial Buffer Register
This is the serial communication data register used to transmit or receive data through it.
SCON: Serial Control Register
Serial control register SCON is used to set serial communication operation modes. Also it is used to control transmit and receive operations.
Bit 7:6 – SM0:SM1: Serial Mode Specifier
Mode | SM0 | SM1 | Mode |
---|---|---|---|
0 | 0 | 0 | 1/12 of Osc frequency shift register mode fixed baud rate |
1 | 0 | 1 | 8-bit UART with timer 1 determined baud rate |
2 | 1 | 0 | 9-bit UART with 1/32 of Osc fixed baud rate |
3 | 1 | 1 | 9-bit UART wit h timer 1 determined baud rate |
In the 8051 microcontroller, Mode 1 (with SM0 = 0 and SM1 = 1) is typically used for serial communication, which consists of 8 data bits, 1 start bit, and 1 stop bit. The configuration bits related to the UART in this mode are as follows:
- Bit 5 – SM2: Multiprocessor Communication
This bit enables the multiprocessor communication feature in Modes 2 and 3. - Bit 4 – REN: Receive Enable
- 1: Receive is enabled.
- 0: Receive is disabled.
- Bit 3 – TB8: 9th Transmit Bit
This is the 9th bit to be transmitted in Modes 2 and 3 (9-bit mode). - Bit 2 – RB8: 9th Receive Bit
This is the 9th bit received in Modes 2 and 3 (9-bit mode). In Mode 1, if SM2 = 0, then RB8 holds the stop bit that was received. - Bit 1 – TI: Transmit Interrupt Flag
This bit indicates that transmission is complete and is set after transmitting the byte from the buffer. In Mode 0, TI is set by hardware at the end of the 8th bit, and in other modes, it is set at the beginning of the stop bit. - Bit 0 – RI: Receive Interrupt Flag
This bit indicates that reception is complete and is set after receiving the complete byte in the buffer. In receiving mode, RI is set by hardware at the end of the 8th bit in Mode 0 and at the stop bit receive time in other modes.
This configuration allows for efficient serial communication, enabling both data transmission and reception with appropriate signaling.
8051 Microcontroller Programming Steps
- Configure Timer 1: Set Timer 1 to operate in auto-reload mode.
- Load TH1: Set the TH1 register with a value according to the required baud rate. For a baud rate of 9600, load 0xFD (which is -3 in decimal).
- Load SCON: Initialize the SCON register with the desired serial mode and control bits. For Mode 1 (8 data bits, variable baud rate) and to enable reception, load 0x50.
- Start Timer 1: Begin the timer by setting the TR1 bit to 1.
- Load Data to Transmit: Place the data you wish to transmit into the SBUF (Serial Buffer) register.
- Wait for Transmission to Complete: Poll the TI (Transmit Interrupt) flag to check if the data has been completely transmitted.
- Clear the TI Flag: Once the TI flag is set, clear it to prepare for the next transmission.
- Repeat: Go back to step 5 to transmit more data as needed.
These steps outline the process for setting up and using the UART for serial communication on the 8051 microcontroller.
Example
Let’s Program 8051 (here AT89C51) to send character data “test” serially at 9600 baud rate in mode 1
8051 Serial Program for serial data transmit
/*
* 8051_Serial_UART
* http://www.electronicwings.com
*/
#include <reg51.h> /* Include x51 header file */
void UART_Init()
{
TMOD = 0x20; /* Timer 1, 8-bit auto reload mode */
TH1 = 0xFD; /* Load value for 9600 baud rate */
SCON = 0x50; /* Mode 1, reception enable */
TR1 = 1; /* Start timer 1 */
}
void Transmit_data(char tx_data)
{
SBUF = tx_data; /* Load char in SBUF register */
while (TI==0); /* Wait until stop bit transmit */
TI = 0; /* Clear TI flag */
}
void String(char *str)
{
int i;
for(i=0;str[i]!=0;i++) /* Send each char of string till the NULL */
{
Transmit_data(str[i]); /* Call transmit data function */
}
}
void main()
{
UART_Init(); /* UART initialize function */
String("test"); /* Transmit 'test' */
while(1);
}
8051 Serial Interrupt
The 8051 UART includes a serial interrupt feature. When data is transmitted or received, the serial interrupt flags TI (Transmit Interrupt) and RI (Receive Interrupt) are activated, respectively.
The 8051 serial interrupt has a vector address of 0023H, where the microcontroller can jump to serve the Interrupt Service Routine (ISR) if both the global and serial interrupts are enabled.
Let’s outline how to use the serial interrupt routine in serial communication programming.
Programming Steps
- Set Timer 1 in Auto-Reload Mode:
- Configure Timer 1 to operate in auto-reload mode, which is essential for generating the required baud rate.
- Load TH1 with the Value for the Desired Baud Rate:
- For a baud rate of 9600, load the TH1 register with the value 0xFD.
- Load SCON with Serial Mode and Control Bits:
- Set up the SCON register for the desired serial mode and enable reception. For mode 1 with reception enabled, load SCON with 0x50.
- Start Timer 1:
- Begin the timer by setting the TR1 bit to 1.
- Enable Global and Serial Interrupts:
- Enable global interrupts by setting EA = 1 and enable the serial interrupt by setting ES = 1.
Once these steps are completed, whenever data is received or transmitted, the corresponding interrupt flag will be set, and the microcontroller will jump to the serial ISR.
Important Note: The TI and RI flags must be cleared by software within the ISR. Both transmission and reception interrupts share the same interrupt vector address, so when the controller enters the ISR, you must check the status of the TI and RI bits to determine whether it is a Tx or Rx interrupt.
Example
Let’s program the 8051 (specifically the AT89C51) to receive character data serially at a baud rate of 9600 in mode 1 and send the received data to Port 1 using a serial interrupt.
Code Explanation:
- Receive Data: After a byte is received, the RI flag is set, generating a serial interrupt.
- ISR Routine: The 8051 will jump to the serial ISR routine when the interrupt occurs.
- Sending Data: In the ISR routine, we will send the received data to Port 1.
Here’s a basic example of how this can be implemented in C:
/*
* 8051_Serial_Interrupt
* http://www.electronicwings.com
*/
#include <reg51.h> /* Include x51 header file */
void Ext_int_Init()
{
EA = 1; /* Enable global interrupt */
ES = 1; /* Enable serial interrupt */
}
void UART_Init()
{
TMOD = 0x20; /* Timer 1, 8-bit auto reload mode */
TH1 = 0xFD; /* Load value for 9600 baud rate */
SCON = 0x50; /* Mode 1, reception enable */
TR1 = 1; /* Start timer 1 */
}
void Serial_ISR() interrupt 4
{
P1 = SBUF; /* Give received data on port 1 */
RI = 0; /* Clear RI flag */
}
void main()
{
P1 = 0x00; /* Make P1 output */
Ext_int_Init(); /* Call Ext. interrupt initialize */
UART_Init();
while(1);
}