MCU dual-machine communication Very detailed UART serial port asynchronous communication

Publisher:星辰小鹿Latest update time:2022-01-13 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Overall design

1. Design requirements:

Serial communication is carried out between the two microcontrollers. The sending end sends 0~f loop to the receiving end, and it is displayed on the receiving end.


2. Design plan:

In this design, for two 89C51 chips, RS232 is used for dual-machine communication. The data of the sender is output from the TXD segment of the serial port, and the TTL level is converted to RS232 level output through the level conversion chip MAX232, and the signal is transmitted to the receiving end through the transmission line. The receiver also uses the MAX232 chip to convert the level, and the signal reaches the receiving end of the serial port of the receiver. After the receiver receives it, the received information is displayed on the digital tube. In order to improve the anti-interference ability, optical coupling can also be added to the input and output ends for photoelectric isolation.


In the software part, the communication protocol is used for sending and receiving. The host first sends AAH to the slave. When the slave receives AAH, it replies BBH to the host. After receiving BBH, the host sends 10 data in the digital table TAB[16] to the slave and sends a checksum. The slave receives 16 data and calculates the checksum of the received data, and compares it with the checksum sent by the host. If the checksum is the same, it sends 00H to the host; otherwise, it sends FFH to the host and receives it again. After receiving 16 correct data, the slave sends it to a digital tube for display.


2. Hardware Design

1.51 MCU serial communication function

insert image description here

The information exchange between a computer and the outside world is called communication. There are two common communication methods: parallel communication and serial communication. The 51 single-chip microcomputer uses four interfaces to input and output data with the outside world, which is parallel communication. The characteristics of parallel communication are fast signal transmission speed, but more signal lines are used, the cost is high, and the transmission distance is short. The characteristic of serial communication is that only two signal lines (one signal line and one ground line as a signal loop) can be used to complete the communication, which is low cost and has a long transmission distance.


The serial interface of the 51 microcontroller is a full-duplex interface. It can be used as a UART (universal asynchronous receiver and transmitter) or as a synchronous shift register. The structure of the 51 microcontroller serial interface is as follows:

(1) Data buffer (SBUF)

The data received or sent must first be sent to the SBUF buffer. There are two, one for buffering and the other for receiving, using the same direct address 99H. When sending, use the instruction to send the data to SBUF to start sending; when receiving, use the instruction to take the received data out of SBUF.

(2) Serial control register (PCON)

insert image description here

SCON is used for serial communication mode selection, receiving and sending control and status indication. The meanings of each bit are as follows:


SM0, SM1: Serial interface working mode selection bits, these two bits are combined into 00, 01, 10, 11 corresponding to working modes 0, 1, 2, 3. The characteristics of the serial interface working mode are shown in the table below

insert image description here

SM2: Multi-machine communication control bit.

REN: Receive enable control bit. Software sets it to 1 to enable reception; software sets it to 0 to disable reception.

TB8: In mode 2 or 3, TB8 is the 9th bit of data to be sent and is set to 1 or cleared to 0 by software as needed.

RB9: In mode 2 or 3, the 9th data bit received by RB8 is actually the 9th data bit TB8 sent by the host. The slave device uses this bit to determine whether the data sent by the host is a call address or data to be transmitted.

TI: Transmit interrupt flag. It is automatically set by hardware after a frame of data is sent, and an interrupt is requested. It must be cleared by software before it can continue to be sent.

RI: Receive interrupt flag. It is automatically set by hardware after receiving a frame of data and requests an interrupt. It must be cleared by software before continuing to receive.

(3) Input shift register

The received data first enters the input shift register serially, and after all 8 bits of data are shifted in, they are sent in parallel to the receive SBUF.

(4) Baud rate generator

The baud rate generator is used to control the data transmission rate of serial communication. The 51 series microcontroller uses timer T1 as the baud rate generator, and T1 is set in timing mode. The baud rate is a physical quantity used to indicate the speed of serial communication data transmission, which is defined as the number of data bits transmitted per second.

(5) Power control register PCON

The highest bit is SMOD.

(6) Baud rate calculation

When timer T1 works in timing mode, the overflow rate of timer T1 = (T1 count rate) / (machine cycles required to generate overflow). Because it is in timing mode, T1 count rate = fORC/12. The number of machine cycles required to generate overflow = modulo M-count initial value X.


2.MAX232 chip

When communicating with the 8051 serial interface, if the distance between two 8051 microcontrollers is very close (no more than 1.5m), you can directly connect the serial interfaces of the two 8051 microcontrollers and use their own TTL level (0-5V) to directly transmit data information. If the transmission distance is far (more than 1.5m), due to the impedance and distributed capacitance of the transmission line, level loss and waveform distortion will occur, so that the data cannot be detected or the data is wrong. At this time, the RS232 standard bus interface can be used to convert the TTL level output by the microcontroller into the RS232 standard level (logic 1 is -15--5V; logic 0 is +5--+15V). RS232 can increase the transmission distance to 15m. If you want to transmit over long distances, you can use RS422 or RS485.


The level conversion chip MAX232 is produced by MAXIM and is specially used for converting TTL level to RS232 level. MAX232 has an internal pump power supply that can increase the +5V power supply voltage inside the chip to the +10V or -10V level required by RS232 level.

insert image description here

Figure 2. Level conversion chip MAX232


3. Overall circuit design

The final design circuit is shown in Figure 3. The data of the sender is output from the TXD segment of the serial port. The level conversion chip MAX232 converts the TTL level to the RS232 level output, and transmits the signal to the receiving end through the transmission line. The receiver also uses the MAX232 chip to convert the level, and the signal reaches the receiving end of the serial port of the receiver. After the receiver receives it, it displays the received information on the digital tube through the P1 port.

insert image description here

Figure 3. Serial communication circuit


3. Software Design

The communication protocol is used for transmission and reception. The host first sends AAH to the slave. When the slave receives AAH, it replies BBH to the host. After receiving BBH, the host sends 10 data in the digital table TAB[16] to the slave and sends a checksum. The slave receives 16 data and calculates the checksum of the received data, and compares it with the checksum sent by the host. If the checksum is the same, it sends 00H to the host; otherwise, it sends FFH to the host and receives it again. After receiving 16 correct data, the slave sends it to a digital tube for display.


1. Serial communication software implementation

(1) The serial port operates in mode 1. Use timer 1 to generate a baud rate of 9600 bit/s and operate in mode 2.

(2) Function: Send the 16 numbers in the digital table TAB[16] in the local ROM to the slave and save them in the slave's internal ROM. After receiving these 16 data, the slave sends them to a digital tube for cyclic display.

(3) Communication protocol: The host first sends an acknowledgment signal (AAH), and after receiving it, the slave returns a acknowledgment signal (BBH) to indicate that the slave is ready to receive.

(4) The communication process uses the ninth bit to send the parity bit.

(5) After receiving a data, the slave immediately performs a parity check. If there is no error in the data, it returns 00H, otherwise it returns FFH.

(6) After the host sends a data, it waits for the slave to return data; if it is 00H, it continues to send the next data; if it is FFH, it resends the data.


2. Program flow chart

(1) Sending end program flow chart

insert image description here

(2) Receiver program flow chart


insert image description here

4. Joint debugging

Conduct simulation experiments on protues. First, use KeilC to compile the written program to generate a HEX file, burn the HEX file into two MCUs, and conduct simulation experiments. The results are shown in the figure below. It can be seen that the receiving end has completely displayed the received data.

insert image description here

Figure 4. Simulation diagram


5. Design Summary

After a busy and intense course design, I finally successfully completed the design task. Although every day was so busy during this period, I gained a lot in this busy process.


Through course design, in the process of consulting materials, I learned C language programming based on single-chip microcomputers and understood the basic knowledge of single-chip microcomputer serial communication, which will be of great benefit for future study and work.


During the learning process, I also encountered some difficulties. For example, at the beginning, the data could not be transmitted correctly because the communication protocol between the sender and the receiver was not done well. In the process of solving the problem, I gained a deep understanding of the implementation of the communication protocol.


Through this course design, I have exercised my ability to think independently.


1. Host sends program

#include // 51 MCU preprocessor

#define uchar unsigned char // Macro definition of unsigned character type variable

void init(); //Define initialization function

void send(); //Define the sending function

[1] [2]
Reference address:MCU dual-machine communication Very detailed UART serial port asynchronous communication

Previous article:Design of 51 single chip ultrasonic water level controller
Next article:AT89C51 single-chip microcomputer water lamp C language program and detailed explanation (literacy tutorial)

Recommended ReadingLatest update time:2024-11-24 18:16

Another UART implementation of MSP430
  1 #include "msp430g2553.h"   2    3 typedef unsigned char uchar;   4 typedef unsigned int  uint;   5    6 #define LED     BIT0   7 #define TXD     BIT1                            // TXD on P1.1   8 #define RXD    BIT2                             // RXD on P1.2   9 #define POUT    P1OUT  10   11 #define BITTIME_1b 13
[Microcontroller]
MSP430--UART module
UART is the abbreviation of Universal Asynchronous Serial Interface. The serial communication interface is used to communicate with the external system of the microcontroller. For example, the data converted by the ADC of the microcontroller can be sent to the PC (host computer) through the serial port, and then sent b
[Microcontroller]
MSP430--UART module
s3c2440 bare metal - UART programming - 2 - UART programming implementation
UART Programming 1. Initialization Our 2440 supports 3 UART serial ports, taking uart0 as an example. Then we need to implement the following functions to complete the most basic functions of the serial port: (1) uart0_init() is used to initialize the serial port (2) p
[Microcontroller]
PIC16F877A-UART
#include #define unchar unsigned char #define uint unsigned intunchar RC_label; //The starting bit of the flag is 1 and the ending bit is 0 unchar caiji_label;//Acquisition frame format flag is 1 if correct and 0 if wrong unchar data RC ;//Store received data unchar data TX1 ;//Store own parameters unchar dat
[Microcontroller]
STM32F103 UART interrupt receive DMA send
Take USART3 as an example, (1) First, enable the DMA module in and add stm32f1xx_hal_dma.c to the project. (2) Add the following declaration to the header of main.c UART_HandleTypeDef huart3; DMA_HandleTypeDef hdma1_uart3; (3) Add USART initialization in main.c /* USART3 init function */ static void MX_USART3
[Microcontroller]
STM32F103xC USART/UART decomposition
Chip: STM32F103RCT6 (64pin, 256K, LQFP, -40~85) Object: USART         The STM32F103xC chip embeds three universal synchronous/asynchronous receiver transmitters (USART1, USART2 and USART3) and two universal asynchronous receiver transmitters (UART4 and UART5).         These five interfaces provide asynchronous communi
[Microcontroller]
STM32 UART commonly used 3 types of interrupt reception
#include "sys.h" #include "usart.h"   #include "main.h" //If you use ucos, just include the following header files. #if SYSTEM_SUPPORT_OS #include "includes.h" //ucos use  #endif //Note that reading USARTx- SR can avoid inexplicable errors    u8 USART1_RX_BUF ; //Receive buffer, up to USART_RX_LEN bytes u8 USART2_R
[Microcontroller]
Implementation of embedded UART communication based on Lua scripting language
   introduction   With the gradual improvement of the intelligence level of substations, there is an increasing demand for the collection of field status parameters such as temperature and humidity. At present, in field applications, such devices mostly use UART serial communication methods such as RS232 or RS485
[Microcontroller]
Implementation of embedded UART communication based on Lua scripting language

Recommended posts

Single lead handheld ECG
ProductIntroduction Thesingle-leadhandheldECGisaportable,handheldECGmonitoringdevice.ThroughECGwaveformdataacquisitiontechnologyandWIFItransmissiontechnology,theECGsignalistransmittedtothecloudplatformormobileph
wuzfeng Medical Electronics
AD20 Select PCB Components cannot be used?
Afterselectingtheschematic,usingSelectPCBComponentsdoesnotjumptothePCBdiagramtoselectit.Thetwofilesarealsointhesamefolder.Whatistheproblem? Generally,whenyouswitchtoschematicmode,itwillbeavailableinthetoo
Memory01 PCB Design
The impact of common mode noise on EMI
MostconductedEMIproblemsarecausedbycommon-modenoise.And,mostcommon-modenoiseproblemsarecausedbyparasiticcapacitanceinthepowersupply.ForPart1ofthisdiscussiontopic,wefocusonwhathappenswhenparasiticcapacitancei
Aguilera Analogue and Mixed Signal
【XMC4800 Relax EtherCAT Kit Review】+ Hardware Interpretation
MainhardwareofthemaincontrolboardTheoverallblockdiagramofthemaincontrolboardisasfollows1,ThemaincontrolchipusesXMC4800F144K2048,LQFP144package.ItcanbereplacedwithXMC4700.Thisfullcompatibilityisp
anger0925 Industrial Control Electronics
Can the differential pairs be wound with equal lengths like this?
Canthedifferentialpairsbewoundwithequallengthslikethis? Theoretically,itisnotpossible,becausetheimpedancewillbediscontinuous.Butfromapracticalpointofview,itdependsonhowfastyoursignalis.Itdoesn'tmatterifthes
asionl PCB Design
[November 22 | Shanghai] You are invited to attend the High-Speed Interconnect Innovation Technology Seminar!
Meetingtime:13:30-17:30,November22,2024 Meetingplace:ShanghaiBoyaHotel,BiboHallABonthefirstfloor Aboutthisactivity AsthescaleofAImodelscontinuestogrow,thenumberofinterconnect
eric_wang Integrated technical exchanges
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号