Two-way communication between USART HMI intelligent serial screen and MCU

Table of contents

·HMI serial screen introduction

·HMI serial screen development practice

·Prepare

·Interface knowledge

·Write command

·download

· Two-way communication between HMI serial screen and [microcontroller] (stm32)

[Function realization:

  1. STM32 controls the corresponding value and text of the serial screen to complete the corresponding changes
  2. Serial screen controls the LED flip of STM32  
  3. The serial port screen inputs the value through the keys and sends it to the microcontroller, and the microcontroller accepts it and displays it on the serial port screen in real time]

First effect video

Display of overall functions of USART HMI intelligent serial screen

  • Introduction of HMI serial screen

What is HMI?

HMI is the abbreviation of Human Machine Interface, “human-machine interface”, also called machine interface . Human-machine interface is a medium for interaction and information exchange between the system and users, which realizes the conversion between the internal form of information and the form acceptable to human beings.

What is a serial HMI?

Serial HMI means that after the device encapsulates the underlying functions of the HMI, it interacts with the user’s MCU through the serial port (USART 232) . For example, the MCU can send commands through USART at any time to notify the device to switch a page or change the properties of a component. The device can also notify the user through the USART at any time that the MCU operator currently touches a component on the page or that the device currently enters a certain page.

in short

USART HMI intelligent serial screen is a liquid crystal screen (TFT-LCD) with serial port control

Then why use HMI serial screen? Isn’t TFT-LCD touchscreen enough?

silly treasure  

The touch screen is like this

And the serial screen

it’s like this

You may have to figure out how to build the buttons on the touch screen, and you may need to clear the screen to redisplay the content when the page returns. The HMI serial screen only needs to import the pictures designed by itself, and on this basis, add the page switching password (page) and so on. Because the operation on the screen without communication does not require a single chip at all.

In short && personal opinion

USART HMI smart serial screen

The most direct advantage is that it is easy to use – the design of the interface and some operations of the serial screen itself can be directly operated on the software

The most intuitive advantage is interface specialization – designing different styles of interfaces according to different themes.

Probably the only disadvantage compared to the TFT-LCD display is that it is more expensive

  • HMI serial screen development practice

1. Prepare

Before developing the serial screen, that is, designing the interface , you need to prepare the following software and hardware.

Host software (development software): USART HMI + Framework 3.5 (language development [software]: USART HMI + Framework 3.5 (language development software )

Link: http://tjcfile.iicp.net:885/USARTHMI/USARTHMIsetup_1.63.1.exe ( Framework does not need to download the commonly downloaded USART HMI, it will automatically pop up to remind you to download )

Download tool: ①SD card + card reader or ②USB to serial port or ③USB to TTL ( or !!!! )

2. Interface understanding

First explain some commonly used or more important tools

  1. New/Open file
  2. Compile: Check whether the entire project is wrong, such as whether the command format/use is wrong, whether the image coordinate value is invalid, etc.
  3. Debug: Simulate the project
  4. Download: The second type mentioned above / the third type of download
  5. Tool: Create a new font library (must have a font library if you want to write text on the serial screen)
  6. Help: There are instruction sets and data centers. The instruction set includes all its instruction guides, and the data center jumps to the Tao Jingchi data center. There are software downloads, project samples, video tutorials, collections of frequently asked questions, etc.

②Enter the page of HMI file

Before entering, you need to choose according to your own serial screen model size and set horizontal or vertical screen.

3. Write command

There is a very complete instruction set in the instruction set of the help. The functions, how to use it, and the routines are all detailed. Here are just a few examples of the effects of the commands.

  • Page refresh the page
    • The toolbox houses a random control (because there is a press event and a key up). Change something in the control properties to make the control more visible/better looking. (eg. txt for text, val for numbers, pic for pictures) Attention! txt, val must build a font, pic must add pictures.
    • Create a new page (you can name it eg.page1 yourself)
    • In the popup event of the command window, enter page page1 (page is the command page1 is the page name) or page 1 (1 is the id number of the page)

The square circle is the ID number, and the circle circle is the name

The last step is to debug get√ (the effect is as follows) 

Serial screen refresh page function display (debugging)

prints prints a variable/constant from the serial port

printh prints a Hex from the serial port

Place a digital control, press the event as above, and the pop-up event is n0.val+=1

The debugging effect is as follows (for a detailed explanation, please go to the help, if you don’t understand, ask again, you must read more help and write more commands! )

[The default variable returned by prints is four-byte integer data hex data storage method is little endian mode, that is, the first position

, the function of the following 0 is to send the length to the automatic length, that is, the actual length]

Display of the function of sending commands on the serial screen

4. Download

①SD card + card reader

Power on the serial screen: VCC, GND and 5V and GND of the microcontroller are connected, and the microcontroller can be powered on. This is one way. In short, VCC and GND should be connected correspondingly.

File → output production files (to SD card, that is, the card reader is inserted into a disk displayed) → insert the SD card into the serial screen → power on the serial screen → download complete 100% → power off the serial screen → pull out the SD card → power on again Just √

Pay attention to the order! SD card must be inserted before power on, SD card must be pulled out after power off

  • ②/③

    • Two-way communication connection   VCC-VCC GND-GND RX-TX TX-RX

    • There are details in the online operation help

    • Software download → select the corresponding serial port and baud rate → download (Note: the speed is extremely slow)

First hex sends ff ff ff (the command before the end)

String send (cancel hex send) connect 0xff 0xff 0xff

Receive comok 1,101,TJC4024T032_011R,52,61488,D264B8204F0E1828,16777216 as success

Generally, the serial port screen defaults to a baud rate of 9600. Downloading can also make it automatically search.

  • Two-way communication between HMI serial screen and microcontroller (stm32)

  • STM32 controls the corresponding value and text of the serial screen to complete the corresponding changes

stm32 one-way control only needs to master the command to change the properties of the serial screen control, and will use the serial port to send it

void  HMI_SendNum (USART_TypeDef* USARTx,u8 *data,u8 len,u16 Num,u8 len2)
 {
    u8 t;
    for(t=0;t<len;t++)
    {   
        while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET);
        USART_SendData(USARTx,data[t]);                     
    }
    for(t=0;t<len2;t++) 
    {
        while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET);
        USART_SendData(USARTx,'0'+(Num/HMI_Pow(len2-t-1))%10); 
    }

    while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET);
    USART_SendData(USARTx, 0XFF );

    while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET);
    USART_SendData(USARTx, 0XFF );

    while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET);
    USART_SendData(USARTx, 0XFF );
}


void  HMI_SendText (USART_TypeDef* USARTx,u8 *data,u8 len,u8 *text,u8 len2)
 {
    u8 t;
    for(t=0;t<len;t++)
    {   
        while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET);
        USART_SendData(USARTx,data[t]);                     
    }
    while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET);
    USART_SendData(USARTx,0X22); 

    for(t=0;t<len2;t++) 
    {
        while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET);
        USART_SendData(USARTx,text[t]); 
    }
    while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET);
    USART_SendData(USARTx,0X22); 

    while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET);
    USART_SendData(USARTx, 0XFF );

    while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET);
    USART_SendData(USARTx, 0XFF );

    while(USART_GetFlagStatus(USARTx,USART_FLAG_TC)!=SET);
    USART_SendData(USARTx, 0XFF );
}

use:

if (Buttonflag)   //Button flag 
            HMI_SendNum(USART3, "q0.picc=" , 8 , 5 , 1 );
         else 
            HMI_SendNum(USART3, "q0.picc=" , 8 , 4 , 1 );

if (USART3_RX_STA& 0x8000 ) //accept complete
        {
                if(USART3_RX_BUF[0]==0x01&&USART3_RX_BUF[1]==0x03) 
            {
                Event_1(); //Event one 
                USART3_RX_STA= 0 ; //Clear the flag bit
            }
            if(USART3_RX_BUF[0]==0x02&&USART3_RX_BUF[1]==0x12) 
            {
                Event_2(); //Event 2 
                USART3_RX_STA= 0 ; //Clear the flag
            }
            if(USART3_RX_BUF[0]==0x03&&USART3_RX_BUF[1]==0x03&&USART3_RX_BUF[2]==0&&USART3_RX_BUF[3]==0)  
            {
                Event_3(); //Event 3 
                USART3_RX_STA= 0 ;   //Clear the flag bit
            }
        }

  1. The serial screen controls the LED flip of STM32 

  2. The serial port screen inputs the value through the keys and sends it to the single-chip microcomputer, and the single-chip computer accepts it and displays it on the serial port screen in real time

To realize the two-way communication between the two, we need to formulate a data format (convenient to write code, etc.)

The data format I made is:

Page id + control id + ten’s value + one’s value + 0D + 0A

What is the role of 0D 0A?

For details, you can go to the usart folder of stm32 based on library functions.

Here is a brief explanation: the serial port will mark the 15th bit of USART_RX_STA when the serial port receives a carriage return (0D 0A) , which means that a reception is completed . And we clear the bit after executing the corresponding program , so that the next reception can be started .

reflected in the code is

if (USART3_RX_STA& 0x8000 ) //accept complete
        {
                if(USART3_RX_BUF[0]==0x01&&USART3_RX_BUF[1]==0x03) 
            {
                Event_1(); //Event one 
                USART3_RX_STA= 0 ; //Clear the flag bit
            }
            if(USART3_RX_BUF[0]==0x02&&USART3_RX_BUF[1]==0x12) 
            {
                Event_2(); //Event 2 
                USART3_RX_STA= 0 ; //Clear the flag
            }
            if(USART3_RX_BUF[0]==0x03&&USART3_RX_BUF[1]==0x03&&USART3_RX_BUF[2]==0&&USART3_RX_BUF[3]==0)  
            {
                Event_3(); //Event 3 
                USART3_RX_STA= 0 ;   //Clear the flag bit
            }
        }

Now that we can successfully accept the data, let’s focus on parsing the data.

You can set the header of a uniform format (for example, the return value of the get command must start with 0x70/0x71), and judge by detecting the header

Whether the data starts. You can also simply and rudely directly if, and the operation can be performed by satisfying a unique value or all values ​​of the returned command (write a function that meets our corresponding function in the function, such as LED inversion, numerical calculation and then display). Since the function is not complicated, we choose the second one, which is simple and rude. and we order

Usually the exit screen needs to be repeatedly debugged and modified. You need to master debugging and use the serial port assistant to monitor what the serial port screen sends to the serial port. (ps: Use the connection method of ②/③ above, and use the serial port assistant to monitor the serial port)

Finally, what if there is no serial screen and no microcontroller?

This paper introduces a method of setting up a virtual serial port so that the data of two-way communication can be observed by software.

Commonly used virtual serial port software: Virtual Serial Port Driver (VSPD)

Download link: [vspd7.2.308.zip-embedded document resources-CSDN download] (direct download! No points required)

1. Set a pair of serial ports as virtual serial ports

2. Set as shown in the figure (the virtual serial ports are COM1 and COM2)

Then sending it on the serial port is equivalent to sending the microcontroller to the serial port screen.

One more thing to add:

The single-chip microcomputer has a lot of serial ports, as we mentioned earlier, VCC-VCC GND-GND RX-TX TX-RX

RX can be PA10(USART 1) or PB11(USART 3)

corresponding to it

TX can be PA9(USART 1) or PB10(USART 3)

But the microcontroller burn-in program requires PA9 to be connected to RX PA10 to TX

So we have to unplug the jumper cap when we want the serial screen to communicate with it in two directions.

So you’d better use another serial port 

last of the last!

Put the resources for the HMI and the two-way communication between the microcontroller and the serial screen:

Also give a like~

You can leave a message if you have any questions!

Leave a Comment

Your email address will not be published. Required fields are marked *