Introduction: Serial TFT LCD + STM32 + Car Reversing Ultrasonic Distance Measurement

This article describes how to use ultrasonic sensors to measure distance and display distance in real-time via Serial TFT LCD. It can be widely used in automobile reversing radar and other scenes.

Step 1: Hardware

First, prepare the hardware


(1) Control system: STM32 minimum system board; (the 1st picture)

(2) Measurement sensor: ultrasonic sensor; (the 2nd picture)

(3) STONE LCD display: stone STVC070WT-01; (the 3rd picture)

(4) Power supply and cable.

Step 2: Principle of Ultrasonic Ranging Sensor

Principle of ultrasonic ranging sensor


Working principle of the module:

(1) Use IO port TRIG trigger ranging, to at least 10US high power flat message.

(2) The module automatically sends 8 40khz square waves and automatically detects whether a signal is returned;

(3) When a signal returns, ECHO outputs a high level through the IO port, and the duration of high level is ultrasound The time from emission to return of the wave.

Test distance =(high level time * sound speed (340M/S))/2

Step 3: Edit the LCD Screen

Edit the LCD screen

Since the display resolution is 800 by 480, you need to create a background image first. Use the computer's own "drawing tool" to edit, set the canvas size to 800*480. Place the material you want to render in the background image. After editing, name the file number, for example, 0.jpg. (as shown in the 1st picture)

Open display manufacturer to provide the interface editor software "TOOL 2019. Exe", click "New" project in New projects, after creating the project engineering, first delete the project's own background images, click the "Add" New good background picture, click the button "Text variable" continue to Add Text control, the control in the proper position of the background image, used to display the ultrasonic measurements. (as shown in the 2nd、3rd and 4th picture)

You need to set the parameters of the text control when you finish adding.
Memory address: 0645. Only one text control is used this time. The memory address of the control remains unchanged by default. Initial value: -.-, the sensor has not started working when the display screen is just turned on, so the initial value of the text display is set to the content "-.-" in the case of no obstacle detected. Byte length: 3. Since only one decimal point is displayed, the text content shows only three words, so the byte length is set to 3. (as shown in the 5th picture)

Compile the project after adding and setting.
Note: The start screen needs to be set in the Settings interface. The number to be set should be the same as the filename of the background image. Otherwise, a warning will appear after compilation and the display screen will not display properly. This is where I often make mistakes when I am new to this screen. I would like to point out that, please pay attention! Download the display screen project, insert the USB flash drive into the computer, and click to download to the USB flash drive. After downloading, the display screen will be powered on and the U disk will be inserted into the download interface on the display screen to complete the download.

Step 4: Write the Control System Code

Before writing the control system code, it is necessary to understand clearly the interaction model between the sensor and how to obtain the distance data of measurement results, and secondly, it is necessary to understand the control mode of the display screen and how to display the distance data on the screen.

(1) According to the timing diagram of the ultrasonic sensor, the STM32 MCU needs to provide a trigger signal to the ultrasonic module, and the ultrasonic module will transmit the measurement results to the STM32 MCU IO pin through IO pulse. (as shown in the 1st picture)

Write the corresponding ultrasonic data reading function according to the time sequence diagram

Ultrasonic data reading function ****/

/* Return value: test distance, unit M, type floating point */

float SR_DATA()

{

float SR_Test_L; // Define measurement result variables

SR_OUT = 1; // Pull up the ultrasonic sensor pin

delay_us(20); // The lifting time is 20US

SR_OUT = 0; // Pull down the ultrasonic sensor enable pin to complete a request data pulse

while(!SR_IN); // Wait for ultrasonic sensor transmission pin to be at high level

SR_add_flge = 1; // The timer count marker position 1 is set to indicate the time to start calculating the high level

while(SR_IN); // Wait for the transmission pin of the ultrasonic sensor to be low, indicating that the distance information has been measured

SR_add_flge = 0; // The position of the timer count marker is 0, indicating that the measurement has been completed

if(SR_add_count < 2647) // If it exceeds the measurement result range, it means the test result is invalid and the calculated result of 2647 is 4.5m

{

SR_Test_L = (SR_add_count * 0.00001 * 340) / 2;//SR_add_count * 0.00001s * 340m/s / 2

}

SR_add_count=0; // Reset timer counting variable

return SR_Test_L; // Return test distance

}

(2) The control mode of the text control can be found in the operation manual provided by the display screen manufacturer. (as shown in the 2nd picture)

According to the provided case to write the corresponding display text control function

Display text control function ****/

Parameters: data units, the first bit of data */

void TFT_SEND_TEXT(u8 data1, u8 data2)

{

SendChar(0xa5);

SendChar(0x5a);

SendChar(0x06);

SendChar(0x82);

SendChar(0x06);

SendChar(0x45);

SendChar(data1);

SendChar('.');

SendChar(data2);

}

(3) Read the ultrasonic measurement results cyclically and display the measurement results on the display screen.

while(1)

{

SR_DATA_L = SR_DATA(); // Collect ultrasonic measurement results

delay_ms(100); // Sampling period: 100ms

if(SR_DATA_L < 0.1) // Invalid data will be displayed if the measurement result is less than 0.1m

{

TFT_SEND_TEXT('-','-');

}

The else

{

TFT_SEND_TEXT((u8)SR_DATA_L+48,(u8)(SR_DATA_L*10)%10+48); // To display the measurement results and calculate the method, take the units bit of SR_DATA_L and the first bit of SR_DATA_L

}

}

Step 5: Download the Program and Connect the Circuit for Testing

Download the engineering code to STM32 MCU, and use the du Pont line to connect all lines.

STM32 minimum system plate-PC8 -& GT; Ultrasonic module -Trig

STM32 minimum system plate-PC9 -& GT; Ultrasonic module -Echo

STM32 Minimum system plate-3V3-& GT; Ultrasonic module -VCC

STM32 Minimum system plate-GND -& GT; Ultrasonic module -GND

STM32 minimum system plate-PA2 -& GT; STVC070WT-01-DIN

STM32 minimum system plate-PA3 -& GT; STVC070WT-01-DOUT

STM32 Minimum system plate-GND -& GT; STVC070WT-01-GND


It should be noted that the default serial communication mode of the display screen is RS232, while STM32 serial communication mode is TTL communication mode. It is necessary to change the default communication mode of the display screen to TTL on the hardware. The modification method is as follows: use the solder wire in the figure for conducting.

Step 6: Test Results

After the wiring is connected, the battery is energized for the test. The test results are shown in the figure and the effect is good.

Microcontroller Contest

Participated in the
Microcontroller Contest