Design of SRS232/RS485 Converter Based on Single Chip Microcomputer

Publisher:美丽的1号Latest update time:2015-06-26 Source: eepwKeywords:MCU  SRS232  RS485  Converter Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
In order to facilitate the centralized control of distributed field measurement and control equipment, fieldbus technology has been widely used. RS485 bus has become one of the most widely used fieldbuses in China due to its advantages such as simple hardware design and communication protocol, convenient control, low cost, long transmission distance and the ability to connect multiple slave devices.

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.
Keywords:MCU  SRS232  RS485  Converter Reference address:Design of SRS232/RS485 Converter Based on Single Chip Microcomputer

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

Mouser launches sales of Infineon MOTIX™ TLE989x MCU: single-chip power IC, more suitable for automotive/BLDC motor control
October 23 , 2023 – Industry-renowned New Product Introduction (NPI) agent Mouser Electronics, offering an ultra-wide range of semiconductors and electronic components ™ Mouser launches sales of Infineon MOTIX™ TLE989x MCU: Single-chip power IC equipped with CAN (FD) interface, more suitable for automotiv
[Industrial Control]
Mouser launches sales of Infineon MOTIX™ TLE989x MCU: single-chip power IC, more suitable for automotive/BLDC motor control
AT89C51 single chip microcomputer drives LCD to display Chinese characters C language
Download the complete project file of this program: http://www.51hei.com/f/8952lkj_c.rar The above picture is the schematic diagram and its simulation effect. /*******************************************************************  AT89C51 MCU drives LCD to display Chinese characters C language ***************
[Microcontroller]
AT89C51 single chip microcomputer drives LCD to display Chinese characters C language
Microcontroller C language tutorial Lesson 7 Operators and expressions (1)
In the last two lessons, we talked about constants and variables. Let's first add a statement to redefine data types. This statement is typedef, which is a very useful statement, but I don't often use it. Usually, I use standard keywords when defining the data type of a variable, so that others can easily study your p
[Microcontroller]
Microcontroller C language tutorial Lesson 7 Operators and expressions (1)
MCU---HLK-W801 Bluetooth remote control lighting
Yesterday, we understood how to run the demo. Today, let's see how to remotely light up the LED of the development board via Bluetooth. Although this is not a typical use of low-power Bluetooth, it is a blessing to be able to quickly learn the disgusting SDK code of W801 through this method. Oh, this chip is produced
[Microcontroller]
MCU---HLK-W801 Bluetooth remote control lighting
51 MCU IO port expansion
We all know that there are two major types of communication: serial and parallel. The biggest advantage of serial is that it occupies less bus, but the transmission rate is low; parallel is just the opposite, occupying more bus and high transmission rate. There are many such chips on the market, some are serial input
[Microcontroller]
51 MCU IO port expansion
8051 MCU (STC89C52) stably displays two digits
#include STC89C5xRC.H   void delay() { int i, j; for(i = 0; i 15; i++) for(j = 0; j 15; j++) ; }   void disp_digit() { int d = 60; //number to be displayed unsigned char code DIG_CODE = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f}; while(1) { //Display the single digit P2 = 0; // P2 = 0
[Microcontroller]
8051 MCU (STC89C52) stably displays two digits
51 MCU-Serial Port & LCD Screen Ⅱ
1. Question requirements In the code test in the previous lecture, the data sent from the computer needs to be accessed by the microcontroller using an array, and then the data is displayed on the LCD screen. However, we can also write the received data directly into the RAM of the LCD screen to display it in real t
[Microcontroller]
32-bit MCU Knowledge Explanation: Microchip PIC32 Series (Part 2)
  PIC32 (General Purpose) Starter Kit Development Board Block Diagram   PIC32 (General Purpose) Starter Kit Circuit Diagram (1): PIC32MX CPU   PIC32 (General Purpose) Starter Kit Circuit Diagram (2): PIC18LF4550 DEBUG CPU   PIC32 (General Purpose) Starter Kit Circuit Diagram (3): Application Board Co
[Analog Electronics]
32-bit MCU Knowledge Explanation: Microchip PIC32 Series (Part 2)
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号