There are usually two ways of communication between single-chip microcomputers: parallel communication and serial communication. The advantage of parallel communication is the fast transmission speed, but the disadvantage is that it occupies more data transmission lines and has a high cost for long-distance transmission. Serial communication is usually used for communication between single-chip microcomputers. This example implements data transmission between single-chip microcomputer A and single-chip microcomputer B. The
two parties in communication agree that the sender is machine A and the receiver is machine B. First, machine A sends a contact data (0xAA) to machine B. After receiving it, machine B responds with an answer signal (0xDD), and then receives the data sent by machine A. If the data received by machine B is incorrect, it sends 0xFF to machine A. After receiving OxFF, the receiver A retransmits the data.
In serial communication, if the distance between two MCU systems is very short (less than 1 meter), the two-machine communication can be realized by directly connecting the serial ports of the MCUs. When connecting, pay attention to connecting the TXD pin of one party to the RXD pin of the other party. As shown in the figure below.
If the communication distance is far (within 30m), the RS-232C interface can be used to extend the communication distance. At this time, the TTL level of the microcontroller must be converted to the RS-232C standard level. This requires adding an RS-232C level conversion chip to the interface part of the microcontrollers on both sides. Commonly used chips of this type include MAX232, etc. The system block diagram is shown in the figure below.
The block diagram of the dual-machine communication between single-chip microcomputers is shown in the figure below. The hardware of the system mainly includes single-chip microcomputers and level conversion chips. The single-chip microcomputer uses 89C51. Since the signal of the single-chip microcomputer is TTL level (0V~5V), if the RS-232 standard bus interface is used for long-distance communication, the TTL level output by the single-chip microcomputer must be converted to RS-232 standard level. The circuit of using the level conversion chip MAX232 for single-chip dual-machine serial communication is shown in the figure. Only the single-chip microcomputer interface circuit of the communication party is drawn in the figure.
The whole system includes the single-chip minimum system and the MAX232 level conversion circuit. The MAX232 has two transceivers, and only one of them is used here. Note that the connection method of the other party's single-chip RXD and TXD is different from this diagram. The TOUT and RIN of the MAX232 of the two communicating parties should be connected to the RJN and TOUT of the other party respectively. Note that the ground wires of the two communicating parties should also be connected.
In the following program, the crystal oscillator frequency of the microcontroller is 11.0592MHz, the serial port works in mode 1, the baud rate of communication is 9600bit/s, and the sending and receiving data are both in query mode. The program flow chart is shown in the figure below.
It is mainly composed of the following subroutines.
·voidinit(): completes the serial port initialization function.
·voidsend0: completes the data sending function of machine A.
·voidrecv(): the function of machine B receiving data according to the established contact signal.
The specific procedures are as follows:
1. Serial port initialization subroutine voidinit()
The serial port initialization subroutine init() sets the serial port to work in mode 1 and the baud rate to 9600 bit/s. The program code is as follows:
2. Machine A sends subroutine voidsend()
Machine A sends subroutine send() to complete the function of machine A sending data. First, send a contact signal, and then wait for machine B to respond.
When machine B is ready, it responds to machine A's contact, then calculates the checksum and sends data, waiting for machine B to respond. If machine B responds correctly, it returns from the subroutine, otherwise it sends data again and waits for machine B to respond. The program code is as follows:
3. The receiving program of machine B voidrecv()
The receiving program of machine B receives data according to the specified contact signal. After receiving the data, if the received data is not 0×AA, it sends 0×FF data to indicate that the contact signal has not been received and continues to wait. After receiving the contact signal, it receives the data and calculates the checksum. If the checksum is correct, it sends 0×00 to indicate that the data is correct, otherwise it sends 0×FF to indicate that the data is received incorrectly. The program code is as follows:
The microcontroller uses the serial port to send data. It can send data in query mode or in interrupt mode. When receiving data, the serial port interrupt mode is generally used to receive data. In this example, both sending and receiving use the query mode.
To ensure the normal communication, the data frame format and baud rate of the sender and receiver should be set to be consistent, and the crystal oscillator frequency of the microcontroller system of the two communicating parties should be the same as much as possible.
When only the baud rate of the other party is known, the crystal oscillator should be selected reasonably so that the baud rate error between the two communication devices is less than 2.5%.
For example, we use a 11.0592MHz crystal oscillator to obtain the commonly used l200bit/s and 9600bit/s baud rates. For example, when the crystal oscillator is 11.0592MHz, if the communication baud rate is required to be 9600bit/s, SMOD=0, then the initial value TH1 of the timer Tl is calculated to be exactly equal to 253 according to the formula; if a 12MHz crystal oscillator is used, THl=252.74 is obtained, and the baud rate calculated after rounding (253) is 10416bit/s, and there is a large error in the baud rate.
In addition, the two parties in communication must also comply with a certain communication protocol. The communication protocol is a pre-agreement between the two parties in communication, including unified regulations on data format, synchronization mode, transmission speed, transmission method, error correction method, etc. The two parties in communication must strictly comply with the communication protocol.
Previous article:Serial communication between multiple microcontrollers
Next article:Several hardware interface designs for realizing serial communication between PC and single-chip microcomputer
- Popular Resources
- Popular amplifiers
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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- 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
- 2022 Brushless DC Motor Control Technology Seminar (the most rubbish conference I have ever attended)
- 【GD32307E-START】Development practice->Add display to the development board to light up 128*64 LCD
- How to install virtual machine on SinA33 development board
- How to Make DSP Digital Oscillator Generate Phase-Shifted Sine Wave
- Newbie asks for advice on STC89c54RD+, RAM access value application
- The problem of sensor sampling value software returning to zero
- A comprehensive look at Sub-6Ghz massive MIMO infrastructure
- Detailed explanation of the schematic diagram of each part of the flyback switching power supply UC3842 protection circuit
- Several indoor wireless positioning technologies
- NXP Rapid IoT Review] +4. Learn the design ideas of POWER circuits