“Based on the consideration of the versatility of the monitoring communication system, we adopted a modular design method, treating the communication program as a module of the main program. Because the BCD code starts with the numbers “0” to “9”, and the control commands start with the letters “A” to “F”, the data is converted into a BCD code and sent at the sending end. In this way, the two types of signals can be distinguished, and they can be decoded and processed separately at the receiving end.
Author: Liu Huiwen, Su Jianhui
Winbond’s new SCM W77E58 contains 2 enhanced serial ports and 32KB large-capacity Flash memory. The instruction set is fully compatible with the 51 series single-chip microcomputer, which is very suitable for use in the intelligent monitoring system. This system connects 3 single-phase inverters into a token network, and uses a standard RS485 interface to communicate with the Inverter system and the host computer at the same time. It can Display the operating status of the inverter system on the LCD screen or the host computer in real time. And can easily modify the system parameters.
1 System hardware design
Figure 1 shows the hardware block diagram of the W77E58 monitoring system single-machine dual serial port communication. This circuit complies with the simplest RS485 bus interface standard. Among them, the inverter system uses Motorola’s dedicated motor control chip 68HC908MR16 (hereinafter referred to as MR16) as the main control chip. The chip contains a standard programmable serial communication interface SCI module, which communicates with serial port 0 of W77E58 through the RS485 interface chip MAX3082. At the same time, the data transmitted by W77E58 serial port 1 through MAX3082 communicates with the host computer through a RS485 to RS232 level converter. The communication circuits all use optocoupler isolation.
Figure 1 W77E58 monitoring system single-machine dual-serial communication hardware block diagram
In Figure 1, Maxim’s MAX3082 is used as the RS485 interface chip, which is suitable for half-duplex communication. It adopts balanced drive and differential reception, and has the ability to suppress common mode interference. It can be used in harsh environments with a maximum communication distance of 1200m. It has transmit enable and receive enable control. When the enable is invalid, the output terminals of the sending and receiving are in a high-impedance state. When using MAX3082 to form a communication system, the maximum communication rate is 0.125Mb/s, and up to 128 transceivers can be connected to the transmission line.
This system uses a non-coding keyboard with four keys: OK, CANCEL, UP, and DOWN. The use of one-key multiplexing technology enables these four keys to complete functions such as system setting, parameter modification, and running status display.
The liquid crystal display LCD used here is CAG1264-4 dot matrix liquid crystal. The LCD has a backlight with adjustable brightness, which is light, ultra-thin, and flexible in programming. The entire screen can display 8×4 national standard Chinese characters or 16×4 ASCII codes. The hardware interface adopts 8-bit parallel data format, and the handshake protocol is simple and reliable.
Since three-phase AC power is commonly used in the industry, this system also has the function of connecting three single-phase inverters through the network to realize the three-phase AC output in self-synchronization. As shown in Figure 2, the serial port outputs of three single-phase inverters are connected to form a simple token network, and then unified by the upper computer through the RS485 to RS232 level converter for management and control.
Figure 2 Block diagram of multi-machine networking hardware structure
2 Serial port introduction and initial settings
In this communication system, we stipulate that each frame of data is 10 bits: 1 start bit, 8 data bits, and 1 stop bit. The baud rate is set to 9600bps.
2.1 W77E58 serial port
The enhanced serial port of W77E58 single-chip microcomputer is characterized by its unique automatic address recognition and frame error diagnosis functions. It should be noted that serial port 0 can use timer T1 or timer T2 as the baud rate generator; while serial port 1 can only use timer T1 as its baud rate generator. The following takes serial port 0 as an example to briefly introduce the working process of the W77E58 serial port.
Serial port 0 carries on serial communication through pins RXD (Serial Data Receiver) and TXD (Serial Data Transmitter). It can send and receive data at the same time. It has two physically independent receiving and sending buffers SBUF, which occupy The same address. The sending and receiving of the serial port are read or written in the name of the special function register SBUF. After the data is sent or received, the hardware will set the sending interrupt flag TI or the receiving interrupt flag RI. It is worth noting that both the interrupt flags TI and RI need to be cleared by software. The working process of serial port 1 is similar to that of serial port 0, and only needs to perform similar operations on its corresponding registers and flag bits. The W77E58 serial port is a programmable interface. To initialize and program it, you only need to write the corresponding control word to the special function register SCON or SCON1 and the power control register PCON. Here, timer T2 is used as the baud rate transmitter of serial port 0, and timer T1 is used as the baud rate generator of serial port 1. The baud rate is set to 9600bps.
2.2 MR16 serial port
2.3 Host computer serial port
Because the serial port of the upper computer adopts the RS232 standard interface, a level converter between RS485 and RS232 is needed to realize the communication with the lower computer. Here, the level conversion device used is Newton-7520, the input voltage range is 10 ~ 30V, and the allowable frequency range is 300Hz ~ 115kHz.
3 communication protocol
In this system, MR16, W77E58 and the upper computer are both the sender and receiver, and they all work in half-duplex mode. The sending and receiving of MR16 and the upper computer adopt the inquiry mode, the serial port 0 and the serial port 1 of W77E58 all adopt the inquiry method, and the receiving adopts the interrupt method. To ensure accurate and reliable communication, both parties must define a communication protocol in addition to specifying the character format and baud rate. Through analysis and experiments, it is finally determined that the communication between MR16 and W77E58 is mainly 77E58. The communication protocol between W77E58 and the host computer is the “Master-Slave Communication Method” communication protocol based on the host computer. Take the W77E58 serial port 0 and the host computer as an example, and introduce this communication protocol as follows.
◆ W77E58 serial port 0 communication protocol (take W77E58 as host)
Send (query method):
fe+fe+fe+ code. … .. request to send back the data packet of the corresponding code
fe+fe+ef+code+high byte+low byte+checksum. … .. Send the packet to be modified
Receiving (interrupt mode):
fe+fe+aa+fault code. … .. Receive fault code
fe+fe+fe+code+high byte+low byte+checksum. … .. Receive the packet to be displayed
◆ Host computer communication protocol (upper computer is the host)
Send (query method):
fe+fe+ station number. … .. Query the corresponding site
fe+fe+fe. … .. request to send back the data packet to be displayed
Receiving (inquiry method):
fe+fe+ station number. … ..Receive and send back verification station number
fe+fe+high byte+low byte+checksum. … .. Receive the packet to be displayed
The italics in the protocol is the header code, bold and underlined are the command codes, and the station number is the token used to select single-phase inverters during multi-machine communication. The station number of each single-phase inverter is different. The header of the data packet is 0xfe and 0xfe. Take W77E58 serial port 0 as an example, when W77E58 uses the command 0xfe to request the MR16 to send back a data packet with the corresponding code, if the command 0xaa is received, it indicates that there is a fault, and then the corresponding fault display is performed according to the received fault code; To the command 0xfe, the received parameter value will be sent to the LCD for display.
It can be seen from the data packet format that the communication between MR16 and W77E58 is mainly W77E58, and the communication between W77E58 and the upper computer is mainly the upper computer. The sending and receiving of MR16 is controlled by W77E58 serial port 0, while W77E58 serial port The sending and receiving of 1 is controlled by the upper computer. This master-slave mode is a mode often used in the RS485 standard. Here, the data synchronization is achieved by sending the header. The sender must send the header first and then send the data every time; the receiver only starts to receive the data after receiving the header correctly. When the received data is wrong or the sent data is lost, the current reception will be abandoned, and the communication will be resumed in the next main loop. In this way, it can automatically recover after a communication error, and at the same time avoid the occurrence of “crash”.
4 software implementation
Based on the consideration of the versatility of the monitoring communication system, we adopted a modular design method, treating the communication program as a module of the main program. Because the BCD code starts with the numbers “0” to “9”, and the control commands start with the letters “A” to “F”, the data is converted into a BCD code and sent at the sending end. In this way, the two types of signals can be distinguished, and they can be decoded and processed separately at the receiving end.
The W77E58 serial port 0 query sending program, interrupt receiving program, and host computer query receiving program are given here. The program flow is shown in Figures 3, 4, and 5. MR16 communication module includes query receiving program and query sending program. The program flow is similar to Figure 3; W77E58 serial port 1 communication module includes interrupt receiving program and query sending program. The program flow is similar to that of serial port 0 and will not be given here.
Figure 3 W77E58 serial port 0 query sending program
Figure 4 W77E58 serial port 0 interrupt receiving program
Figure 5 The inquiry receiving program of the upper computer