Realization of communication between touch screen and single chip microcomputer

Publisher:幸福之星Latest update time:2011-10-24 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

As the application and output of touch screens increase, prices fall. Therefore, it is possible to use touch screens as keyboards and display devices for single-chip microcomputer controlled devices, thus improving the quality of single-chip microcomputer controlled devices. When the touch screen is used in conjunction with PLC, the main functions of the touch screen are:

①Display the switch status of PLC input, output terminal 13 or auxiliary relay.

② Use touch buttons to force the PLC input, output port or auxiliary relay to open/close.

③Display the contents of the timer, counter and data register in the PLC.

④ Use the touch screen keyboard to send the set data into the PLC data register.

It can be classified into the data bit and word reading and writing of the corresponding address between the touch screen and PLC. Therefore, the MOD-BUS communication protocol can be used to realize the communication and control between the touch screen and the single-chip microcomputer, or the touch screen can communicate with multiple single-chip microcomputers to form a distributed control system.

1 Hardware connection between touch screen and microcontroller

Use MT500 touch screen to communicate with AT89C52 microcontroller one-to-one. Connect the PLC232 9-pin socket of the touch screen to the AT89C52 microcontroller with RS232 interface. See Figure 1. Note: The communication cable DB9 is 2-2, 3-3, 5-5.

This type of cable can also be used to communicate with a PC or perform communication simulation. When communicating with a PC, short 4-6 and 7-8 on the DB9 connected to the PC. Since the AT89C52 microcontroller does not have an RS232 interface, it is necessary to expand a MAX232 to convert the TTL level of RXD and TXD into RS232 level.

Figure 1 Touch screen and microcontroller communication connection method

2 Establish the correspondence between the touch screen and the internal memory address of the microcontroller

Open the touch screen configuration software, select [System Parameters] from the [Edit] drop-down menu, and the parameter setting dialog box shown in Figure 2 will pop up. In the system parameters of the touch screen, the PLC type is set to MODBUS RTU, and the communication parameter settings must be consistent with the communication parameter settings of the microcontroller. The communication port type is set to RS232, the data bit is set to 8 bits, 1 stop bit, the baud rate is 9600, and the check bit is set to be consistent with the microcontroller programming. The PLC station number is the same as the station address defined by the microcontroller, and the station number must start from 1. After the parameter settings are completed, press the OK key.

Figure 2 Parameter Setting Dialog Box

At this time, the operable address range of the touch screen is shown in Table 1.

Table 1 Operable address range of the touch screen when set to MODBUS RTU mode

Ox1-Ox9999, 1x1-1x9999 are used for bit operation memory, 3x1-3x9999, 4x1-4x9999 are used for word operation memory. lx1-1x9999 is used as input node (read-only) bit operation memory, and is used as the bit status indicator on the touch screen. Ox1~oX9999 are readable and writable, and can be used as bit status indicator, button, trigger control touch, etc. 3x1-3x9999 is used as input data memory, and can be used as numerical display element and multi-state indicator in the touch screen. 4x1-4x9999 is readable and writable, and can be used as numerical input element and multi-state setting element in the touch screen. Its address is expressed in decimal. Ox0, lx0, 3x0, 4x0 cannot be used, otherwise an error will occur during compilation.

The register address corresponding to the touch screen can be freely defined and allocated in the microcontroller.

For example, assume that P2 and P3 are input ports, and P0 and P1 are output ports. To facilitate single-chip microcomputer programming, the pin status of P2 and P3 ports can be mapped to 20H and 21H, corresponding to the touch screen lx1-1x16. Store the output data of P0 and P1 in registers 22H and 23H, corresponding to the touch screen 0x1-0x16. 24H-28H is used as the bit operation storage area, corresponding to the touch screen 0x16-1x56. Set 30H-3F:H to 8 data display registers, corresponding to the touch screen 3x1-3x8. Set 40H-4FH to 8 data setting registers, corresponding to the touch screen 4x1-4x8. In this way, the corresponding table of the communication address between the touch screen and the single-chip microcomputer can be listed, and the bit address is shown in Table 2. The word address is shown in Table 3.

Table 2 Touch screen and microcontroller communication bit address correspondence table

Table 3 Touch screen and microcontroller communication word address correspondence table

3 Touch screen configuration software editing

Open the touch screen configuration software, first set the [System Parameters] according to the above method, move the cursor to the [Window/Component Selection List Box], right-click the mouse, create a new window, and fill in the dialog box as required. Press the OK button, and an editing window will appear. The following takes the setting of a bit status display component (indicator light) and a value display component (data display window) as an example to explain the method of establishing a connection between the address edited by the touch screen and the address in the microcontroller. Now take the example of displaying the status of the 20H (24H.0) bit in the microcontroller and the display of the values ​​of 40H and 41H. Click the mouse on the component diagram in the component toolbox or drag a bit status indicator component from the [Component] menu option. The bit status display component property dialog box pops up.

Select 0x in the [Device Type] of the read address column, write 17 in the [Device Address], see Table 2, 0x17 corresponds to 20H, select the appropriate graphics, labels, size and position. Press the OK key. The dialog box disappears, and a bit status display element L1 appears in the editing window. Once the correct communication relationship is established, the different display states of this bit status display element reflect the changes in the state of bit 20H in the microcontroller.

Click the component diagram in the component toolbox or drag a numerical display component from the [Component] menu. The numerical display component property dialog box pops up, as shown in Figure 3.

Figure 3 Numerical display component properties dialog box

Select [Device Type] in the read address column, write 1 in [Device Address], see Table 3, 3xl corresponds to 40H (high), 41H (low), select the appropriate graphics, label, size and position. Press the OK button and the dialog box disappears.

A numerical display component appears in the editing window. Once the correct communication relationship is established, this numerical display component will display the values ​​of the microcontroller's middle bits 40H (high bit) and 41H (low bit).

4 Introduction to MODBUS RTU (Remote Terminal Unit) Communication Protocol

The communication between the touch screen and the single-chip microcomputer is a master-slave communication mode, the touch screen is the host, and the single-chip microcomputer is the slave. The touch screen continuously sends the command of reading or writing bits and words according to the control edited on the screen. The single-chip microcomputer continuously responds to the command.

This article introduces the MODBUS RTU communication format. The character frame consists of 8 data bits, 1 start bit, 1 stop bit, and 1 parity bit (when the parity bit is not used, 2 stop bits are set). Each byte sent or received must undergo a CRC error check operation. The calculation method is detailed in the reference.

A command frame consists of a slave address, function code, data field and CRC check code. The data field consists of a 16-bit first address (high bit first, low bit last) of the read or write bit or word data, a 16-bit operand (high bit first, low bit last), and finally a 16-bit CRC check code (low bit first, high bit last). There must be a delay of more than 3.5MS before and after sending or receiving to indicate the start or end of a command frame. The address code and function code are both one byte.

The following example illustrates the communication format using the MODBUS RTU communication protocol:

1) Read the bit status of a word (sixteen bits) of the first address 20H in the microcontroller corresponding to the first address 0x17 of the touch screen.

Touch screen sends communication format:

MCU response communication format:

The touch screen bit address 0x17 corresponds to the microcontroller first address 24H.0, and returns 24H.

0~20H.7, 25H.0-25H.7, sixteen-bit status.

2) Read the value of a word (40H, 41H) of the touch screen's designated first address 4x1 corresponding to the first address 40H in the microcontroller.

Touch screen sends communication format:

MCU response communication format:

3) Set the touch screen address bit 0x18 corresponding to the 21H (24H.1) position in the microcontroller to 1 or clear it.

Touch screen sends communication format:

MCU response communication format:

In the table, the set command 0FF00H is set to 1, and 0000H is cleared to zero.

4) Write a set value oal64H into a word 42H and 43H of the touch screen's designated first address 4x2 corresponding to the first address 42H in the microcontroller.

Touch screen sends communication format:

MCU response communication format:

In a similar way, select controls and edit control properties according to project requirements.

Establish the address connection between the touch screen and the single-chip microcomputer, and write the communication program between the single-chip microcomputer and the touch screen according to the MODBUS protocol. Then, the touch screen can be used to display the state change of the single-chip microcomputer, set the value or display the value.

Reference address:Realization of communication between touch screen and single chip microcomputer

Previous article:Working Principle of DDS Chip AD9850 and Analysis of Its Interface with Single Chip Microcomputer
Next article:Design of colored lights based on single chip microcomputer

Latest Industrial Control Articles
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号