Single-chip microcomputer and (serial screen) touch screen communication based on Modbus Protocol (graphic)
Introduction: Touch screen can be intuitive, vivid display of operating parameters and operating state, and through the touch screen screen can directly modify the system operating parameters, human-machine interaction is good. Touch screen and single-chip communication, the need for a single-chip microcomputer based on the communication protocol used to write the corresponding communication program. The Modbus protocol is an effective protocol for MODICON companies to support communication between controllers and controllers across networks (e.g. Ethernet) and other devices.
Keywords: touch screen, MCS-51 MCU, Modbus protocol, communication
In industrial control, it is often necessary to observe the operating state of the system or modify the operating parameters. The touch screen can display the operating parameters and operation status intuitively and vividly, and the operation parameters of the system are directly modified by the touch screen screen, and the human-machine interaction is good. Single-chip microcomputer is widely used in the field of industrial control, with the touch screen, can compose a good human-computer interaction environment. Touch screen and single-chip communication, the need for a single-chip microcomputer based on the communication protocol used to write the corresponding communication program. The Modbus protocol is an effective protocol for MODICON companies to support communication between controllers and controllers across networks (e.g. Ethernet) and other devices. This paper introduces the development process of the communication program with the Hmimaker touch screen of AT89S51 single chip microcomputer and Guangzhou Easy Display Technology Co., Ltd as an example.
First, the system structure
realizes the communication between the touch screen and the monolithic computer, mainly is solves the communication protocol question. This paper uses the Open Modbus Communication protocol, the touch screen as the host (master), single chip microcomputer as Slave (slaver). Hmimaker touch screen itself support Modbus communication protocol, as long as the single-chip microcomputer in accordance with the Modbus protocol to send and receive data, you can communicate. The touch screen and MCU are connected directly with RS-232 or RS-485 standard interface, and the transfer rate is set to 9600bps. Figure 1 is the circuit diagram of the system.
Figure 1 System circuit diagram
Set AT89S51 microcontroller Txd, rxd port to asynchronous serial communication mode, after the MAX485 chip to convert TTL level to RS485 level, and hmimaker touch screen RS-485 The 2W interface is connected, which completes the hardware connection, wherein the P3.2 is used as the transceiver control signal. Hmimaker Touch screen RS-485 2W connector pin 2 is 485A, pin 1 is 485B, pin 5 is a common ground.
Second, how to set the Hmimaker touch screen to a Modbus device
to set the hmimaker touch screen to a Modbus device (known as Modbus RTU Master), single-chip or other devices only use the Modbus protocol, The data on the Hmimaker touch screen can be read and written via the rs232/485 interface. The method of setting Hmimaker touch screen to Modbus device is as follows:
first need to add a new device to the interface protocol of the program used by the Hmimaker touch screen, at this time the PLC type should choose "Modbus RTU Master", PLC interface can choose RS232 , rs4852w, RS485 4W or Ethernet, choose RS485 2 W here. As shown in 2.
Figure 2 Setting the Hmimaker touch screen to a modbus device
When the interface chooses to use RS485, it is necessary to select the connection port (COM1~COM3) used and set the correct communication parameters and station number. Here choose COM1 (9600,n,8,1), station number is set to 1.
After pressing the "OK" key, a new device can be found in the device list: Modbus RTU Master, at this time, the completion of the Modbus device settings, the completion of the interface design compiled and the obtained Nandflash file downloaded to the touch screen, Can read and write the data on the Hmimaker touch screen through the Modbus protocol.
Iii. Introduction of Modbus Communication protocol
Modbus Communication protocol is a serial master-slave communication Protocol, the network only one set can be used as a host (called Master), other devices as slaves (called slaver), the host does not need to number, slave must be numbered. The protocol defines the format of the information frame of the host query and the slave response. Paper Daquan. When communicating, the host first sends the request information to the slave, receives the communication command according to the corresponding address code, and removes the address code, reads the information, if there is no error, executes the corresponding task, and then returns the execution result to the host machine. If the check code received from the machine is different from the local calculation, then the data in the communication process error, the slave to the request as invalid, the function code will be modified to indicate that the response message is wrong, and the data segment contains the code that describes the error message. When the host receives this information, the request information is resent. The calibration method is divided into LCR (when transmitting data to acsii) check or CRC (transmit data is binary) check, here the CRC check method is used. The information is transmitted asynchronously, in bytes. The word format for communication between master and slave stations is as follows:
Word format (serial data) 10 bit binary
Starting bit 1 bit
Data bits 8 bits
Parity bit None
Stop bit 1 bit
Modbus protocol can support data transmission in ASCII or binary format. Acsii code mode each byte of data is transferred in two bytes, binary mode with one byte transmission, in order to reduce the amount of data transmission, the general choice of binary data mode. Each zone is allowed to send bytes that are 16 binary 0~9,a~f. The standard structure for binary information frames is:
Initial structure ≥ 4 bytes of time
Address Code 1 bytes
Function Code 1 bytes
Data area N bytes
Error checking 16-bit CRC code
End structure ≥ 4 bytes of time
In binary mode, the information starts at least 3.5 characters in a stationary time, and it is easy to calculate the rest time based on the baud rate used. After the last character is sent, there is also a static time of at least 3.5 characters before a new message can be sent. The device on the network continuously monitors information on the network, including standstill time.
Iv. Development of communication procedures
Hmimaker touch screen only supports bit and Word address type, and the actual microcontroller or other controller also has a byte, Double Word and other representations, so in the communication, the microcontroller and touch screen data to be transformed. The device type of the touch screen element reading address is LW, 0x, 1x, 3x, 4x, RWI, RW and other options. Wherein, LW indicates that the component reads the address inside the touchscreen, such as the address of other components, 0x means the output signal of the controller is read, 1x indicates the input signal of the controller read, 3x and 4x refer to the controller's register address type, where 4x is readable and writable, 3x is read-only; RWI, RW are the internal address of the touch screen, play an auxiliary role. The reading and writing process is illustrated only by the data of 0 addresses that read and write LW.
Hmimaker Touch screen and MCU to the Modbus protocol communication, the use of the Modbus function code is as follows:
function code Name function 01 Read coil status Get a set of logic coil current status (on/off) 02 read input status get a set of switch input current status (on/off) 03 read hold register in one or more hold registers to get current binary value 04 read input register in one or Getting the current binary value in multiple input registers 05 strong single Coil strongly resets the on-off state of a logic coil 06 the preset single register loads the specific binary value into a hold register 1. Initialization of the serial port program
Using timer 1 as the baud rate generator, the initialization procedure for the serial port is as follows:
void Initial (void)
{
scon=0x50; Serial port mode 1, allow to receive
tmod=0x20; Timer 1 Way 2
TL1=0XFD; Baud rate set to 9600
TH1=0XFD; Storage Timer Initial Value
Ea=1; Global interrupt Allow
Tr1=1; Start timer
}
2. Write data to the 0 address of the hmimaker touch screen LW
To the Hmimaker touch screen LW 0 Address write Data 0x3a4b, function code 6, the program is as follows:
void Datewrite (void)
{
es=0; Off interrupt
unsigned int A;
unsigned int CRC;
Indata[0]=1; Station number
indata[1]=6; function code
indata[2]=0; Start Address high
indata[3]=0; Start Address Low
indata[4]=0; High number of registers
Indata[5]=1; Number of registers low
indata[6]=2; Number of bytes
indata[7]=0x3a; To write high data
indata[8]=0x4b; To write Data low
CRC=0XFFFF;
CRC=CALCCRC (9,&indata[0]); Calculate CRC values based on the first 9 bytes
indata[9]=crc&0xff; CRC value Low 8 bits
indata[10]=crc>>8; High CRC value 8 bits
for (a=0;a<11;a++)//A total of 11 bytes
{
Sbuf=indata[a]; Send data
while (! TI);
ti=0;
}
Es=1; Open serial port Interrupt
}
3. Read data from the 0 address of the Hmimaker touchscreen LW
From the Hmimaker touch screen LW 0 address reading data, function code 3, the program is as follows:
void Datewrite (void)
{
es=0; Off interrupt
unsigned int A;
unsigned int CRC;
Indata[0]=1; Station number
indata[1]=3; function code
indata[2]=0; Start Address high
indata[3]=0; Start Address Low
indata[4]=0; High number of registers
Indata[5]=1; Number of registers low
CRC=0XFFFF;
CRC=CALCCRC (6,&indata[0]); Calculate CRC values based on the first 6 bytes
indata[6]=crc&0xff; CRC value Low 8 bits
indata[7]=crc>>8; High CRC value 8 bits
for (a=0;a<8;a++)//A total of 8 bytes
{
Sbuf=indata[a]; Send data
while (! TI);
ti=0;
}
Es=1; Open serial port Interrupt
}
After sending the information to read the data, once the Hmimaker touch screen receives the information, after the CRC check, if there is no error, the LW 0 address data sent to the microcontroller, a total of 7 bytes, the data format is as follows:
Byte Data segment name
Station No. 1
2 function code
3 Number of bytes read
4 Register Data High
5 Register Data Low
6 CRC check Code low
7 CRC Check Code high
The microcontroller receives these 7 bytes, according to station number, function code and CRC check is correct, if received correctly, then the 4th byte and 5th byte value is required hmimaker touch screen LW 0 address data.
The system has the advantages of simple connection circuit, flexible configuration and high reliability of communication. Because of the openness of Modbus communication protocol, the Protocol is supported by the serial communication of foreign import controller, so the serial communication with Modbus protocol is of great application value in this paper.
Single-chip microcomputer and (serial screen) touch screen communication based on Modbus Protocol (graphic)