#51 MCU #UART serial communication initial understanding

Publisher:CuriousMind123Latest update time:2021-10-19 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

UART serial communication

UART, the full name of Universal Asynchronous Receiver/Transmitter, is a universal asynchronous receiver/transmitter.


Serial port communication is the most commonly used technology in microcontrollers, and is usually used for communication between microcontrollers and computers and between microcontrollers.


UART communication types can be divided into two types, parallel communication and serial communication.


Parallel communication: Each bit of data is transmitted at the same time, which can realize communication in bytes. Disadvantages: Communication lines occupy a lot of resources and are costly.


Serial communication: only one byte of data is transmitted at a time.


STC89C52 has two dedicated UART communication pins, P3.0 (RXD) and P3.1 (TXD). The communication interface composed of them is called serial interface, or serial port for short.


The following figure shows the process of two microcontrollers sending and receiving information to each other:

insert image description here

In the figure, the GNDs of the two MCUs are interconnected because the power supply reference must be consistent for communication between the MCUs.


GND——The reference ground of the microcontroller system power supply.


TXD - Serial transmit pin.


RXD - serial receive pin.


To learn UART communication, you need to understand the baud rate.


The baud rate is the rate at which binary data bits are sent, usually expressed in baud. The duration of sending one bit of binary data = 1/baud.

(Common baud rate values ​​for UART serial ports are 300, 600, 1200, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200, etc.)


Before communication, the two microcontrollers need to clearly agree on the communication baud rate. Only by keeping the baud rate consistent can the sender and receiver achieve normal communication.

UART communication principle: low bits are sent first, high bits are sent later.

UART communication stipulates that when there is no communication signal, the communication line should maintain a high level. Before sending data, a 0 should be sent first to mark the data start bit. After the data is sent, a 1 should be sent to mark the data end bit.

The following figure is a schematic diagram of serial port data transmission:

insert image description here

The modified figure shows the corresponding relationship of the signal changing over time, which is actually a time domain schematic diagram.


For example, on the microcontroller's transmit pin, the one on the left is sent first, and the one on the right is sent later. The switching time of the data bit is 1/baud rate.


Mastering the concept of time domain helps to understand the timing diagrams of different communications


In order to better understand the principle of UART serial communication, a program for IO port to simulate UART serial communication is given below.


The IO port simulation UART serial communication program is a simple demonstration program. Use the serial port debugging assistant in the burning tool to send a data, and it will automatically return after the data is added by 1.


The essence of the serial port debugging assistant is to use the UART communication interface on the computer to send data to the microcontroller or receive data from the microcontroller and display it on the debugging assistant interface. (The microcontroller is connected to the computer via USB, and the computer has a USB to UART chip)


#include


sbit PIN_RXD = P3^0; //define the receiving pin

sbit PIN_TXD = P3^1; //define the transmit pin


bit RxdOrTxd = 0; //Mark the current state (sending or receiving)

bit RxdEnd = 0; //Mark the end of receiving

bit TxdEnd = 0; //Mark the end of sending

unsigned char RxdBuf = 0; //Receive buffer

unsigned char TxdBuf = 0; //send buffer


void ConfigUART(unsigned int baud); //Configure baud rate

void StartTXD(unsigned char dat); //Start sending data

void StartRXD(); //Start receiving data


void main()

{

EA = 1; //Interrupt enable switch

ConfigUART(9600); //Configure the baud rate to 9600

while(1)

{

while(PIN_RXD); //Wait for the receiving pin to be low level, i.e. start bit

StartRXD(); //Start receiving

while(!RxdEnd); //Wait for receiving to complete

StartTXD(RxdBuf+1); //Add 1 to the received data and send it back

while(!TxdEnd); //Wait for sending to complete

}

}


void ConfigUART(unsigned int baud)

{

TMOD &= 0XF0; // Clear the control bit of T0

TMOD = 0X02;      //Set T0 to mode 2

TH0 = 256 - (11059200/12)/baud; //Calculate T0 reload value

}


void StartTXD(unsigned char dat)

{

TxdBuf = dat; //Save the data to be sent to the send buffer

TL0 = TH0; //T0 count initial value is reload value

ET0 = 1; // Enable T0 interrupt

TR0 = 1; //Start T0

PIN_TXD = 0; //Send start bit

TxdEnd = 0; // Clear the send end flag

RxdOrTxd = 1; //Set the current state to send

}


void StartRXD()

{

TL0 = 256 - ((256-TH0)>>1); //When starting, set T0 to 1/2 of the baud rate period

ET0 = 1; // Enable T0 interrupt

TR0 = 1; //Start T0

RxdEnd = 0; // Clear the receiving end flag H7YHB 

RxdOrTxd = 0; //Set the current state to receive

}


void InterruptTimer0() interrupt 1

{

static unsigned char cnt = 0; //bit receive or send count


if(RxdOrTxd)

{

if(cnt <= 8)

{

PIN_TXD = TxdBuf & 0X01;

TxdBuf >>= 1;

}

else if(cnt == 9)

{

PIN_TXD = 1;


}

else

{

cnt = 0; //Reset bit counter

TR0 = 0; //Turn off T0

TxdEnd = 1; //Sign sending end

}

}

else

{

if(cnt == 0)      //Process the start bit

{

if(!PIN_RXD) //When the start bit is 0, clear the receive buffer and prepare to receive data bits

{

RxdBuf = 0;

cnt++;

}

else    //The start bit is not 0, abort reception

{

TR0 = 0; //Turn off T0

}

}

else if(cnt <= 8)

{

RxdBuf >>= 1; //The low bit is in front, so the previously received bit is shifted

if(PIN_RXD)     //When the receiving pin is 1, the buffer's highest position is 1

{

RxdBuf |= 0X80;

}

cnt++;

}

else //Stop bit processing

{

cnt = 0; //Reset bit counter

TR0 = 0; //Turn off T0

if(PIN_RXD) //If the stop bit is 1, the data is valid

{

RxdEnd = 1; //Mark the end of reception

}

}

}


It can be observed that when the receiving function starts, the timing is half a baud rate cycle. This is a little trick for UART communication. Looking back at the serial port data diagram, if you read data when the data bit level changes, you may read the wrong data due to timing errors or unstable signals. You need to find a position where the signal does not change to read the level data. So there is this convention: choose the middle position of the signal to read the level state, so as to ensure the correct reading of the data.

Reference address:#51 MCU #UART serial communication initial understanding

Previous article:How to drive the 51 microcontroller serial port (uart communication)
Next article:51 MCU serial communication uart notes

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号