At present, most distributed fieldbus communication systems usually use PC (or industrial computer) as the host to centrally control and transmit data to various field devices. PC only has a standard RS232 interface, and the interface levels of RS232 bus and RS485 bus are incompatible. To realize the connection of the communication network composed of PC and RS485 bus, it is necessary to convert the level between RS232 and RS485. Therefore, it is necessary to design RS232/RS485 converter. Using
single-chip microcomputer to design RS232/RS485 converter is a common method. The traditional RS232/RS485 converter based on single-chip microcomputer design has the following shortcomings:
(1) The number of single-chip microcomputers and peripheral devices used is large, which brings inconvenience to the design and is not conducive to reducing costs and reducing volume;
(2) The setting is not flexible, the baud rate and communication mode cannot be changed, or the parameters are not convenient to change, which limits the versatility.
In view of the above shortcomings, this paper uses the enhanced 51 single-chip microcomputer STC11F02E produced by Hongjing Technology Company as the core to design an RS232/RS485 converter to make up for these defects, reduce costs, reduce volume, and at the same time, it can easily set the baud rate and communication mode, enhancing its versatility; and the programmable mode of the single-chip microcomputer also facilitates the function expansion of the converter, and the flexibility is greatly enhanced.
Overview of the overall structure of the converter
The converter mainly consists of three parts: RS232 level to TTL level module, RS485 level to TTL level module and single-chip microcomputer main control module. The overall structural block diagram of the converter is shown in Figure 1.
Figure 1 Overall structure diagram of the converter
The RS232 level to TTL level module is used to realize the connection between the RS232 bus and the single-chip microcomputer, the RS485 level to TTL level module is used to realize the connection between the RS485 bus and the single-chip microcomputer, and the single-chip microcomputer main control module plays a transitional connection role, which is used to coordinate and control the data transmission between the RS232 bus and the RS485 bus. The
specific hardware circuit design of these three modules is described in detail below.
Hardware circuit design of the converter
Circuit design of the RS232 level to TTL level module
Here, the MAX232 level conversion chip produced by Maxim is used to design the level conversion interface circuit between RS232 and the single-chip microcomputer.
The MAX232 chip only needs to be powered by a single 5V power supply and 4 capacitors to realize the conversion between RS232 level and TTL level, and can directly drive the RS232 bus for data transmission, which is very convenient to use. The chip can easily realize the connection between the single-chip microcomputer and the RS232 bus, and its maximum transmission rate can reach 120kbps. The
specific circuit design diagram of this module is shown in Figure 2.
Figure 2 RS232 level to TTL level circuit
RS485 level to TTL level module circuit design The
circuit of this module is designed using the RS485 interface chip MAX483E produced by Maxim.
The MAX483E chip is a low-power bus transceiver for RS485 and RS422 communications. It has ±15kV electrostatic discharge impact protection and slew rate limited drive characteristics, which reduces electromagnetic interference and terminal cable reflections, allowing error-free data transmission at a rate of up to 250kbps. The chip can work normally with only a single power supply voltage of 5V, and up to 32 loads can be connected on one bus. The use of this chip can greatly simplify the interface circuit design of the microcontroller and the RS485 bus. The
specific circuit design diagram of this module is shown in Figure 3.
Figure 3 RS485 level to TTL level circuit
Circuit design of the microcontroller main control module
This module is the core module of the converter, and the main function of the converter is realized through this module. The whole circuit design is also very simple. It only needs an enhanced 51 single-chip microcomputer STC11F02E plus some basic peripheral circuits (including external crystal oscillator circuit and reset circuit) to complete the design. The
STC11F02E single-chip microcomputer is an enhanced 8051 core with 2KB on-chip Flash program memory, 2KB EEPROM memory and 256KB on-chip SRAM data memory. The biggest feature of the UART (Universal Asynchronous Receiver/Transmitter) module of this single-chip microcomputer is that by configuring the state of the 7th bit UART_P1 in the special function register AUXR1, the two pins (RXD, TXD) of the UART module can be connected to the P3.0 and P3.1 pins of the P3 port, or to the P1.6 and P1.7 pins of the P1 port, so that it can be used as two serial ports. This function is mainly used here to design the RS232/RS485 converter. [page]
The overall hardware circuit diagram of the entire converter is shown in Figure 4.
Figure 4 Overall hardware circuit diagram of the converter
Working principle of the converter
The working principle of the converter hardware circuit is to first convert the RS232 bus signal level output by the PC serial port into a TTL level signal that can be received by STC11F02E through the level conversion chip MAX232, and the single-chip microcomputer receives the data sent by the PC. After receiving all the transmitted data, STC11F02E switches the serial port pins from P3.0 and P3.1 to P1.6 and P1.7 pins, and then sends the received data to the RS485 bus through the RS485 transceiver MAX483E. After the slave responds, it transmits the corresponding data to the RS485 bus, and then converts it to TTL level through the RS485 transceiver and receives it by the single-chip microcomputer STC11F02E. After receiving all the data from the slave, STC11F02E switches the serial port pins back to P3.0 and P3.1 pins, and then transmits it to the RS232 serial port of the PC through the MAX232 level conversion chip, so that the PC receives the data from the slave. The entire data transmission process is repeated in this way. [page]
The general multi-machine communication mode of the RS485 bus is half-duplex, that is, sending and receiving are not carried out at the same time. A terminal device can only be in the sending state or the receiving state at a certain time, so it is reasonable to use this method to design the RS232/RS485 converter. The key issue here is how to coordinate the switching between the two groups of serial port pins P3.0, P3.1 and P1.6, P1.7, which can be achieved through the program design of the single-chip microcomputer.
Converter program design and communication protocol design
Converter program design process
The overall flow chart of the entire converter program is shown in Figure 5, which mainly includes three parts: program initialization module, main program module and serial port interrupt service module.
Figure 5 Converter program flow chart
The program initialization module mainly completes the initialization of some registers, including watchdog setting, setting the host data reception end flag MDE and the slave data reception end flag SDE, initializing the serial port connection pins, initializing the serial port setting registers and communication baud rate, and enabling serial port interrupts and total interrupts. The main task of the main program module is to complete the orderly switching between the serial port connection pins and the transmission of data. The function of the serial port interrupt service module is to complete the reception of data and set the flags MDE and SDE.
Since the pins of the UART module of the microcontroller are always connected to the host after each communication, each communication must be initiated by the host, and the slave is always in the receiving state. Only when the host addresses the slave can the slave send data to the host.
RS485 bus communication protocol design
In order to ensure the reliability of data transmission, the communication protocol of the RS485 bus master-slave communication must be specified. The
communication protocol used by the converter has two data bits, 10 bits and 11 bits, when transmitting each byte of data. The specific communication method can be set manually. Communication data is sent in the format of information frames. Each information frame consists of multiple bytes of data, including address field, function field, data field and CRC check field, as shown in Table 1. The address field is used to select the slave address to be communicated; the function field is used to indicate the specific operation to be performed; the data field contains the data to be transmitted; the CRC check field is used to store the generated CRC error check code to ensure the correctness of data transmission. The
communication baud rate specified in the communication protocol is 2400 bps, 4800 bps, 9600 bps, 19200 bps, 38400bps and 115200bps. The default baud rate is 9600bps, which can be set by software according to actual needs. The setting parameters corresponding to the communication mode and baud rate are stored in the EEPROM memory of the microcontroller. The
function codes and their functions defined in the communication protocol are shown in Table 2. If the function needs to be expanded in the future, the corresponding function of the system can be added by adding the corresponding function code in the application program, making the system function expansion more flexible.
Table 1 Standard structure of information frame
Table 2 Function
Test results of converter
In order to facilitate the debugging of the converter, a 10-bit communication method is used here, namely 1 start bit, 8 data bits and 1 stop bit. The host computer is a PC, and the communication interface is the RS232 interface; the slave computer is the main control circuit board of the water pump comprehensive parameter test platform designed with the C8051F020 single-chip microcomputer as the core, and the communication interface is the RS485 interface. The debugging software uses the serial port debugging assistant. The connection between the host computer and the slave computer is to use the RS232/RS485 converter designed above. Here, the slave address is set to 01H, and the function code set previously is used for debugging. The specific debugging results are shown in Figure 6.
The data on the left side of the vertical line in the first row of Figure 6 is the data returned by the slave when the PC sends the 03H function code to read the data in the 16 units starting from the address unit 2345H of the slave external data memory. The data frame (hexadecimal number) sent by the host is:
01 03 23 45 10 db 12.
The first byte on the left side of the vertical line in the first row of Figure 6 represents the slave address, the second byte represents the function code returned by the slave, the third byte is the number of data sent by the slave to the host, the following 16 bytes are the data of the unit read, and the last 2 bytes are the CRC16 check code. When the same function code is sent repeatedly, it can be seen that the returned data is the same, indicating that the communication is normal.
The data on the right side of the vertical line in the second row of Figure 6 is the data returned by the slave when the PC sends the 10H function code to write data to the 16 units starting from the address unit 2345H of the slave's external data memory. The data frame (hexadecimal number) sent by the host is:
01 10 23 45 10 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff a7 21 In
the right part of the second vertical line in Figure 6, the first byte represents the slave address, the second byte represents the function code returned by the slave, the third and fourth bytes are the high byte and low byte of the external data memory address respectively, the fifth byte represents the number of data written, and the last two bytes are the CRC16 check code. When the same function code is sent repeatedly, it can be seen that the returned data is the same, indicating that the communication is normal at this time. After sending the 10H function code, send the 03H function code for inspection. It can be seen that the data in the 16 units starting from the 2345H address unit of the external data memory returned by the slave is the same as the data just written. It can be seen that the operation of the corresponding function code is correct and the communication is normal.
Figure 6 Results of debugging the designed converter using the serial port debugging assistant
From the above test, it can be seen that the converter can realize normal communication between the PC and the RS485 bus, indicating that the designed converter meets the expected requirements.
Conclusion
This paper uses the characteristics of the microcontroller STC11F02E serial port pins that can be switched between P3.0, P3.1 pins and P1.6, P1.7 pins at will to design a simple and practical RS232/RS485 converter. The structure of the converter is very simple. Only three commonly used integrated chips plus a small number of peripheral devices are used to complete the overall design, which greatly reduces the volume and cost. In addition, the power supply of the converter can also be powered by the 5V power supply provided by the USB interface of the PC, which can save the trouble of configuring a separate power supply.
Previous article:stc12c5a60s2 independent baud rate device - serial port
Next article:Research and design of automatic observation control platform for solar radio telescope
Recommended ReadingLatest update time:2024-11-23 01:44
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Siemens S7-1200-PLC Programming and Application Tutorial (3rd Edition) (Edited by Shi Shouyong)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- [FS-IR02 + D1CS-D54] - 5: Linking with MCU (D1CS-D54)
- Application of high frequency pulse width modulation technology in inverter
- Qorvo Acquires UWB Software Provider 7Hugs Labs SAS
- Three simple steps to send interrupt operation of msp430 serial port
- Please tell me how to draw the differential line of this USB
- How is the burning of OTP chip carried out after SMD?
- 157 Questions (Answers) You Should Know When You Are a DSP Part 1 2
- MSP430FR2355 Mixed-Signal Microcontroller
- Timing Analysis 1 Static Analysis Basics
- MicroPython v1.19 is coming soon