1 Introduction
The popularization of centralized heating and the implementation of metered heating are effective means of building energy conservation, but this needs to be achieved through the automation of heating system operation and management. In order to study the control and regulation methods of the heating network, a heating network simulation experimental platform was established and an automatic monitoring and control system was configured. When selecting the equipment, the Danish Kamstrup heat meter was used as the measurement and collection device for the heat transported by each main pipe section in the experimental pipe network and the heat of the experimental system. However, the data communication interface module equipped with the heat meter can only communicate with the computer or the corresponding data terminal one-to-one through a dedicated external socket RS-232 serial communication, which is difficult to meet the communication requirements on site.
2 Core components of communication module
In the system, the single-chip microcomputer is generally called the lower computer, which is usually used to complete data collection and upload. The background application part composed of PC, network equipment and database server is collectively called the upper computer, which analyzes and processes the uploaded data of the lower computer. The system gives full play to the advantages of single-chip microcomputer in real-time data collection and microcomputer in graphic processing, display and database management, so that the application of single-chip microcomputer is not limited to automatic monitoring or control, but forms a distributed multi-point system with network as the core. In the experimental platform monitoring and control system, the single-chip microcomputer is selected to cooperate with the industrial computer to establish a communication network as shown in Figure 1 to realize the reading of the collected data of the heat meter.
Figure 1 Communication network of industrial computer and heat meter
The RS-485 communication standard has made great improvements in improving the transmission rate, distance and anti-interference characteristics in view of the defects of RS-232 such as short communication distance and poor anti-interference performance. Therefore, considering the reliability of communication, the RS-485 serial port card is selected for the industrial computer to establish communication.
The digital signal of the single-chip microcomputer follows the CMOS level protocol, and the transmission distance is short, which cannot meet the transmission distance requirements of the distributed control system. A level conversion chip is required to form an interface circuit to connect with the host computer. At the same time, when the single-chip microcomputer is connected to the heat meter, the level matching must be achieved through the corresponding interface circuit.
In addition, the single-chip microcomputer is the communication hub for data exchange between the industrial computer and the heat meter, and is responsible for serial communication with the industrial computer and the heat meter at the same time, so two serial communication ports are required. There are 3 methods to choose from:
- Use a single-chip microcomputer with multiple serial ports;
- Adopt universal asynchronous serial port expansion chip;
- Use common I/O port simulation to realize serial communication.
The third method is the simplest and most economical, and can achieve high-speed communication. In addition, considering the full utilization of hardware resources, the 89C2051 microcontroller produced by ATMEL was selected for development.
3 Development of communication module
3.1 Hardware interface
In theory, the software method can make the serial port expanded from the ordinary I/O port have the same function as the standard serial port. However, according to the actual situation, the serial port of the microcontroller can easily realize the RS-485 level connection with the host computer through a dedicated level conversion chip; the heat meter data communication interface has different baud rate requirements for sending and receiving data, which can be met by performing corresponding delay processing in the program of simulating the serial port. Therefore, the standard serial port is connected to the host computer through level conversion (see Figure 2), and the serial port simulated by the ordinary I/O port is connected to the heat meter through the corresponding interface circuit (see Figure 3).
Figure 2 RS-485 level conversion
The standard serial port of the microcontroller in Figure 2 is converted to RS-485 level through the 75176 chip, which has 8 pins and is DIP packaged. RO is the data receiving end; RE is the data receiving enable end, which is valid at low level; DI is the data sending end; DE is the data sending enable end, which is valid at high level; DT+ and DT- are the current loop return ends. In Figure 2, the RE and DE pins are connected together. When the P3.7 pin is at a high level, data transmission is allowed, and when the P3.7 pin is at a low level, data reception is allowed. R is the circuit matching resistor, which can enhance the system's ability to resist differential mode interference.
Figure 3 Microcontroller and heat meter interface circuit
The dotted box in Figure 3 is the data communication interface circuit of the heat meter itself. The heat meter can communicate with the computer or the corresponding data terminal through RS-232 by connecting a dedicated external plug through this interface. The level signal characteristics of the sending and receiving end of the heat meter during the one-to-one communication with the computer are detected by an oscilloscope, and the interface circuit is built using discrete components to achieve level matching with the microcontroller port. In the process of communication between the single-chip microcomputer and the heat meter, in order to ensure the reliability of communication and to ensure that the single-chip microcomputer can receive the data sent by the heat meter in time, the interrupt receiving mode is adopted. P3.2 is the interrupt trigger end, so this port is selected as the receiving end of the simulated serial port, and P3.4 is selected as the sending end of the simulated serial port. [page]
3.2 Serial communication software
The software programming of the common I/O port simulating the serial port is essentially to simulate the asynchronous communication protocol. The asynchronous communication protocol regards a character as an independent information unit, and the relative time when the character appears in the data stream is arbitrary. The data bits in each character are transmitted at a fixed time, so this transmission is synchronous within the same character, but asynchronous between characters. The method of asynchronous communication transmission and reception to achieve synchronization is to set the start bit and stop bit in the character format. However, since the transmitter and receiver do not use the same clock, but use their own clocks to divide the width of each bit, the error of the transmission and reception clocks will inevitably affect the correct sampling of the data, so it is required to accurately identify the leading edge of the start bit. The actual practice is to use a 16-times baud rate clock to determine the beginning of a character within 1/16 of a bit period, and the receiver samples the data input line at the rising edge of each clock. If a low level is found, and then 8 consecutive samples are all low levels, it is considered to be the start bit, and the data is sampled once every 16 clocks. The character resynchronization process is shown in Figure 4.
Figure 4 Character resynchronization process with f = 16 times baud rate
In software programming, the data bit period is calculated according to the baud rate of the data sent by the heat meter, and then the data bit period delay program and the half-bit period delay program are written according to the crystal oscillator frequency of the microcontroller. When the microcontroller detects that INT0 (i.e., P3. 2 port) is low, the external interrupt is triggered and the interrupt handler is entered. First, the half-bit period delay program is called to determine whether it is the start bit. If so, the bit period delay program is called again to determine and receive the data bit, and finally determine whether the stop bit is received. As long as the delay program can accurately guarantee the delay time, the simulation of the working sequence in Figure 4 can be completed. In addition, when receiving each bit of the data frame, it is sampled three times continuously, and the detected value is determined according to the principle of taking 2 out of 3. This greatly suppresses interference and improves
the reliability of signal transmission. Similarly, when the single-chip microcomputer sends data, as long as the transmission time interval between each data bit of the data frame is adjusted to meet the baud rate of the heat meter receiving data, the heat meter can complete the reception of the data sent by the single-chip microcomputer. The program flow chart is shown in Figure 5.
3.3 Communication protocol
When the serial port of the single-chip microcomputer works in mode 2 or mode 3, the data frame consists of 11 bits of data: 1 start bit, 9 data bits, and 1 stop bit. In these two modes, the 9th bit of data is a controllable flag bit. Only when the 9th bit of data is "1" (representing the address frame), the serial port interface circuit will request an interrupt to the CPU after receiving 1 data. Because the asynchronous communication function of the PC does not have such a communication format, this method is only suitable for multi-machine communication between single-chip microcomputers. Generally, the following methods are often used to achieve communication between two heterogeneous machines:
(1) Use a single-chip microcomputer as a "bridge" between the upper PC and the lower single-chip microcomputer to achieve data format conversion;
(2) Install an asynchronous communication adapter card in the PC, and design the operation mode of the programmable chip to make it consistent with the multi-machine communication mode of the single-chip microcomputer;
(3) Set the properties of the serial communication control provided by the upper computer programming environment to distinguish addressing instructions from data transmission instructions, thereby achieving communication.
Methods 1 and 2 have complex hardware structures, and method 3 has cumbersome software code writing. When the system is not complex, the task is not heavy, and the amount of communication data is not large, these methods will not have obvious defects. Otherwise, the "bridge" single-chip microcomputer in method 1 and the communication asynchronous adapter in method 2 will become the bottleneck of the communication channel. In method 3, the parity bit in the data frame is usually used as the characteristic bit of the sending address code (channel number) or data. In this way, the data communication verification will adopt the accumulation and verification method. Using this method not only increases the software overhead and the amount of data for communication, but also affects the real-time performance.
According to the actual situation, the communication information frame does not distinguish between data frame and address frame. This method does not require the microcontroller to work in 11-bit asynchronous receiving and transmitting mode, so that the serial port working mode of the microcontroller and the PC is consistent. Inspired by the communication format, the information frame is designed as shown in Figure 6. 1 to 3 bytes are used as the "header" of data communication to play a synchronization role; 1 or more bytes are used as the address difference code of the microcontroller to identify a certain microcontroller (determined by the number of lower computers in the system); the information code used to mark the working nature of the lower computer can be flexibly set in the information frame according to the actual situation. [page]
Figure 6 General format of information frame
When the upper computer sends an information frame, the lower computer receives all the information frames of the upper computer, and then judges the address code in the information frame. If it is its own address code, it responds accordingly according to the content of the information code in the data frame. The length of the information frame is unlimited (determined by the number of lower computers in the system and the form of the information code), which is multiple bytes. In this communication format, some communication data can also be verified by software.
The communication protocol between the industrial computer and the lower computer communication module is shown in Figure 7.
Figure 7 Communication protocol between the industrial computer and the lower computer communication module
The information frame sent by the upper computer industrial computer to the lower computer should contain the address and control instruction information, so that the communication module of the lower computer can identify whether and how to respond; the communication module, the information frame replied to the upper computer should contain its own address and the data of the corresponding control instruction; the synchronization word and the end word are necessary to complete asynchronous communication, so both the upper computer information frame and the lower computer information frame are indispensable. The specific information frame format is as follows (the number of bytes is marked in brackets, and the number of bytes not marked is 1):
The whole mutual control process of sending and receiving data is as follows:
the upper computer: sends instructions to the lower computer at a certain time;
the lower computer: after receiving the information frame, it determines whether the address in the information frame is the local address. If so, it sends the local address back to the upper computer, and sends corresponding instructions to the heat meter according to the control word, waiting for the simulated serial port interrupt, and transmits the heat meter data to the single-chip microcomputer, and then forwards it to the upper computer; otherwise, it exits the interrupt program;
the upper computer: receives data according to the events triggered by the serial port, and stores it in the database after judging the source and displays it through the human-computer interface.
According to the above communication protocol, the communication module (lower computer) is programmed in combination with the serial port simulation process. The written program mainly includes the main program and the interrupt program; the interrupt program includes the simulated serial port interrupt receiving program and the serial interrupt program. Among them, the main program completes the initialization of various special registers and communication parameters and formats, analyzes and judges the serial interrupt, receives information and sends corresponding instructions to the heat meter; the simulated serial port interrupt program specifically completes the reception of the heat meter data; the serial interrupt program completes the reception of the information frame sent by the upper computer. In addition, considering various random interferences in actual applications, appropriate error correction processing was performed when writing the program, that is, the communication module operates the heat meter after receiving the host computer command. If no response is received within a certain period of time, the program is reset.
The identification method of the communication module address corresponding to each heat meter is shown in Figure 8. The hardware resources of this model of single-chip computer are fully utilized, and the level of each pin of the P1 port is set as the address identification in the form of a jumper. Different on-off combinations of the 8 pins can represent different addresses.
Figure 8 Communication module address identification
The programming of the communication module is simulated and debugged successfully using the environment provided by the simulator.
4 Interface with host computer software
In order to finally realize the reading of heat meter data by the industrial computer through the communication module, it is also necessary to write the communication interface program for the host computer. There are also many ways to write the host computer communication program: (1) by calling the API function provided by WINDOWS; (2) by using assembly language and C language to write dynamic link library functions; (3) by using VB language and its serial communication controls. The third method is the simplest, with a short development cycle and is economical and practical. Just add the MSCOMM control to the project and then set the corresponding properties to complete the data transmission and reception. However, the data transmission and reception in VB is divided into binary form and text form. If text is sent, the text received by the microcontroller is ASC Ⅱ code. In VB6.0, Unicode characters are used, that is, 2 characters are used to represent 1 character. When 1 character is sent, the microcontroller receives 2 bytes. Therefore, in most cases, the communication between the PC and the microcontroller is binary data. Therefore, when using the MSCOMM control to send binary data, the variable must be declared as Byte format.
References
[1] Hu Hancai. Principles of single-chip microcomputers and their interface technology. Beijing: Tsinghua University Press, 1993: 317-320.
[2] Li Hua, Yan Tianfeng. Implementation methods of serial communication software and hardware. Electronic World, 2003 (5): 31-33.
[3] Wang Xiaojun, Hao Yingji, Zhang Bin. Discussion on two issues in single-chip microcomputer serial communication. Modern Electronic Technology, 2002 (1): 69-71.
Previous article:Development of heat meter communication module based on 89C2051 single chip microcomputer
Next article:The realization method of simulating serial communication through I/O port of 89C51 single chip microcomputer
- Popular Resources
- Popular amplifiers
- Learning MCU Technology from Scratch (Liu Jianqing)
- Wireless doorbell alarm based on AT89C2051 single chip microcomputer
- Infrared remote control LED electronic clock using real-time clock chip DS1302+AT89C2051
- Infrared remote control LED electronic clock using real-time clock chip DS1302+AT89C2051.rar
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Simplifying isolated CAN, power interfaces for HEV 48-V systems
- Active and passive filters
- Can I use LPS22HB without connecting SD0/SA0 to anything?
- App Client
- Limitations of Flash_API for C2000 Series (28335) DSP
- EEWORLD University - Power stage protection, current sensing, efficiency analysis and related TI designs
- Amplifier
- Multi-cell 36-48V Battery Management System Reference Design
- EBAZ4205 mining board transformation based on Z7010
- Samsung closes another factory in China! N+5 compensation higher than national standards