Communication technology between two single-chip computers based on MAX232

Publisher:开国古泉Latest update time:2018-03-01 Source: eefocusKeywords:MAX232 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  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.

Block diagram of single-chip dual-machine communication interface


  
  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.

Circuit of serial communication between two single-chip microcomputers

  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.

Keywords:MAX232 Reference address:Communication technology between two single-chip computers based on MAX232

Previous article:Serial communication between multiple microcontrollers
Next article:Several hardware interface designs for realizing serial communication between PC and single-chip microcomputer

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号