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:
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:
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.
Previous article:How to drive the 51 microcontroller serial port (uart communication)
Next article:51 MCU serial communication uart notes
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Comparison of 4G and 5G wireless technology details
- EEWORLD University Hall----MCU Peripheral Circuit_Lao Wu MCU Actual Combat
- Audio Circuit Debugging Tips
- [TI recommended course] #Industry's first professional RGB LED driver LP50xx demonstration #
- How to configure the background of Hikvision Ai equipment and thermal imaging equipment? What is the difference between them and ordinary network cameras? Please ask the experts! !
- How to get a multi-channel reference power supply
- TMS320F28335GPIO Example - Light up the LED
- Analysis of 2020 E-sports Contest C: The realization process of the 99-point ramp car
- MSP430F4152 development board schematic diagram
- ON Semiconductor - FOD83xx/T series is coming! Answer the questions to win prizes and apply for free samples