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.
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
- Popular Resources
- Popular amplifiers
- Mission-oriented wireless communications for cooperative sensing in intelligent unmanned systems
- CVPR 2023 Paper Summary: Embodied Vision: Active Agents, Simulation
- ICCV2023 Paper Summary: Fairness, Privacy, Ethics, Social-good, Transparency, Accountability in Vision
- Introduction to Artificial Intelligence and Robotics (Murphy)
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
- Thank you + thank you EEWORLD
- msp430 library serial PWM
- EEWORLD University Hall----Live Replay: Using NI CompactDAQ and LabVIEW to Build a Sensor-Based Test System
- Application ends - [TI E2E community year-end benefits] CC3200-LAUNCHXL is given away for free, write a review and win a gift!
- UC2845 power supply problem
- Magnetic components to solve EMC problems in electronic products - Magnetic beads (Part 2)
- Burn files to the development board
- Basic classification of PCB boards. Does anyone know what FR-4 is?
- The role of fast recovery diode
- How to efficiently dissipate heat from lithium-ion batteries?