PIC microcontroller and touch screen serial communication MODBUS protocol

Publisher:GoldenHarmonyLatest update time:2012-02-15 Source: 中数休闲 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In industrial control, it is often necessary to observe the operating status of the system or modify the operating parameters. The touch screen can display the operating parameters and operating status intuitively and vividly, and the system operating parameters can be directly modified through the touch screen screen, with good human-computer interaction. Single-chip microcomputers are widely used in the field of industrial control, and can form a good human-computer interaction environment with touch screens. For the touch screen to communicate with the single-chip microcomputer, it is necessary to write a corresponding communication program for the single-chip microcomputer according to the communication protocol adopted by the touch screen. The Modbus protocol is a protocol launched by Modicon in the United States that effectively supports communication between controllers and between controllers and other devices via a network (such as Ethernet). This article takes the PIC16F877 single-chip microcomputer and the eView MT510T touch screen of Human-Machine Electronics Co., Ltd. as an example to introduce the development process of its communication program.

1 System Structure

To achieve communication between the touch screen and the single-chip microcomputer, the main problem is to solve the communication protocol. This article uses the open Modbus communication protocol, with the touch screen as the master station and the single-chip microcomputer as the slave station. The eView touch screen itself supports the Modbus communication protocol. If the single-chip microcomputer also supports the Modbus protocol, communication can be carried out. The touch screen and the single-chip microcomputer are directly connected using an RS-232C compatible interface, and the transmission rate is set to 9600kb/s. Figure 1 is the circuit diagram of the system.

Set the PIC16F877 microcontroller RC6 and RC7 ports to asynchronous serial communication mode, convert the TTL level to RS232 level through the MAX232 chip, and then connect it to the eView touch screen PLC [RS-232] interface to complete the hardware connection. The pin 2 of the eView touch screen PLC [RS-232] interface is TXD, and the pin 3 is RXD.

2 Introduction to Modbus Communication Protocol

The Modbus communication protocol is a serial master-slave communication protocol. There is only one device in the network that can be used as the host (called Master), and the other devices are slaves (called Slavers). The host does not need to be numbered, but the slave must be numbered. The protocol defines the information frame format of the host query and the slave response. During communication, the host first sends a request message to the slave. The slave that meets the corresponding address code receives the communication command, removes the address code, reads the information, and executes the corresponding task if there is no error. Then, the execution result is returned to the host. If the check code received by the slave is different from the one calculated by the local machine, it means that the data has an error during the communication process. The slave regards this request as invalid, and the function code will be modified to indicate that the response message is wrong. At the same time, the data segment contains the code describing this error information. When the host receives this information, it will resend the request information. The check method is divided into LCR (when the data is transmitted in ACSII) check or CRC (when the data is transmitted in binary) check. The CRC check method is used here.

Information transmission is asynchronous and in bytes. The word format of communication between master and slave stations is shown in Table 1.


Modbus protocol can support data transmission in ASCII code or binary format. In ASCII code mode, each byte of data is transmitted in two bytes, and in binary mode, it is transmitted in one byte. In order to reduce the amount of data transmission, binary data mode is generally selected. The bytes allowed to be sent in each area are all hexadecimal 0~9, A~F. The standard structure of binary information frame is:

When the initial structure is ≥ 4 bytes

Address code 1 byte

Function code 1 byte

Data area N bytes

Error checking 16-bit CRC code

End structure ≥ 4 bytes

In binary mode, a minimum of 3.5 character quiet time is required at the beginning of a message. This quiet time is easily calculated based on the baud rate used. After the last character is sent, there is also a minimum of 3.5 character quiet time before a new message can be sent. Devices on the network continuously monitor the messages on the network, including quiet time.

3. Correspondence between MCU and touch screen address

The eView touch screen only supports Bit and Word address types, while the actual MCU or other controllers also have Byte, Double Word and other representation methods. Therefore, when communicating, the data of the MCU and the touch screen need to be converted. The device types for reading addresses of touch screen screen components include LW, 0x, 1x, 3x, 4x, RWI, RW and other options. Among them, LW means that the component reads the address inside the touch screen, such as the address of other components; 0x means that the output signal of the controller is read; 1x means that the input signal of the controller is read; 3x and 4x refer to the register address type of the controller, where 4x is readable and writable, and 3x is read-only; RWI and RW are both internal addresses of the touch screen and play an auxiliary role. Using these device types for reading addresses, the touch screen can display or set the value of the controller register or I/O port. Taking the "Number Input" component as an example, the value of the MCU register or I/O port can be set by entering data through the "Keypad". If the "Trigger Address" is enabled, then when it is touched, the input data will be stored in the specified MCU address.

4 Development of communication programs

The communication program between the touch screen and the microcontroller is written in PIC microcontroller C language. The compilation tool selects Hitech's PICC compiler. The program is divided into three modules, namely the initialization module, the data receiving module, and the data processing and sending module. The main program flow chart is shown in Figure 2.

The USART function module of the PIC16F877 microcontroller has an eight-bit baud rate generator BRG, which supports the synchronous and asynchronous working modes of USART. The SPBRG register is used to control the cycle of an independent eight-bit timer. In asynchronous mode, the BRGH bit (i.e. D2) of the transmit status/control register TXSTA is used to control the baud rate. The initialization of the serial port is as follows: [page]

GIE=1; %Global interrupt enabled;

SPBRG=25; %Set the baud rate to 9600;

TXSTA=0x04; %Select asynchronous high-speed mode;

RCSTA=0x80; % Allow the synchronous serial port to work;

RTISC6=1;

TRISC7=1; %Set RC6 and RC7 of port C to asynchronous serial communication mode;

PFIE=1; %Peripheral interface interruption enabled;

RCIE=1; %USART receive interrupt enabled;

TXIE=1; %USART transmit interrupt enable;

The data receiving module consists of an interrupt function. This interrupt function stores the Modbus data frame sent by the touch screen in a custom array. When a frame of data is received, the reception end flag is set to 1 and transferred to the data processing and sending module. The core of the serial communication receiver is the receiving shift register RSR. When the reception stops, if the RCREG buffer is empty, RSR sends the received data to RCREG. After the transmission is completed, the receiving interrupt flag RCIF is set to 1. Whether the actual interrupt is responded to by the CPU can be controlled by setting the interrupt enable bit RCIE of the peripheral interface interrupt enable register PIE1.

The data processing and sending module executes the corresponding application of the Modbus protocol function code. The eView touch screen communicates with the microcontroller using the Modbus protocol. The Modbus function codes used are shown in Table 2.

Table 2 Function codes used in communication programs


Take Modbus function code 03 as an example to illustrate the actual communication data format of the touch screen and the microcontroller. If the microcontroller address number is 01H, the touch screen needs to check the value of the register with address 0031H in the microcontroller, and the sending command format is shown in Table 3.


When the baud rate reaches 38400kb/s, the communication is still stable and reliable. This system has the characteristics of simple connection circuit, flexible configuration, high communication reliability, etc., and has been successfully applied to the material screening control system project. Due to the openness of the Modbus communication protocol, most of the serial communication of imported controllers abroad supports this protocol, so the serial communication using the Modbus protocol implemented in this article has a wide range of application value.
Reference address:PIC microcontroller and touch screen serial communication MODBUS protocol

Previous article:Telephone remote control device for household appliances based on PIC microcontroller
Next article:Split air conditioner controller based on single chip microcomputer PIC16C72

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号