51 single chip microcomputer-ultrasonic module

Publisher:RainbowGardenLatest update time:2021-10-14 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Ultrasonic module model

Ultrasonic module is a commonly used distance measurement tool, generally used in obstacle avoidance vehicles and water level monitoring systems. The module model used in this tutorial is HC-SR04, and its appearance is as follows:

9.3.png

In addition to two power pins, the module also has TRIG and ECHO pins, which are connected to P2.0 and P2.1 of our development board respectively.

The working principle is to pull both pins low at first, then pull the TRIG pin high for more than 10 microseconds and then pull it low to generate a pulse start signal (in the program, we pull it high for 20 microseconds). Once the start signal is turned on, the ECHO pin will be pulled high and the ultrasonic wave will start to emit. When the ECHO pin detects the reflected signal, it will be pulled low because the speed of sound is 340m/s. Then the measured distance is

(The duration of the high level of the ECHO pin * 340m/s)/2.

In middle school, we learned to use sound waves to measure the depth of the seabed. Time * speed of sound = round trip distance, so it needs to be divided by 2 to get the true depth. The same is true for ultrasonic modules.

 

2. Software Analysis

The timer count function is used to calculate the duration of the ECHO pin high level. If the timer count overflows, it proves that the measured distance is too far and exceeds the module's distance measurement range (up to 4 meters). We will display 999 on the digital tube to indicate that the distance is too far. Within the measurement range, our digital tube will display the measured number of centimeters.

For example, if the timer counts 9216, then the duration of the high level is 9216*(12/11059200)=0.01s

The measured distance is 0.01*340/2=1.7m

The digital tube displays 170.

However, in order to simplify the calculation process of the microcontroller, we can calculate it like this

“( (X*12)/11059200 )*340*100/2” is the number of centimeters, which can be simplified to “X/54”, where X is the count value of the timer.

The code in this lecture requires the use of "_nop_();" in "#include", which means a delay of about 1 microsecond. Please refer to the second half of Section 14.2 of the document "Hand-in-hand teaching you to learn 51 single-chip microcomputers".


3. Code

#include  

#include //See Chapter 6, Lecture 8 for details

#include //See Chapter 8, Lecture 11 for details

#include  

 

sbit TRIG = P2^0; 

sbit ECHO = P2^1; 

u8 FLAG = 0;

void delay_20us() 

{

    _nop_();_nop_();_nop_();_nop_();_nop_(); 

    _nop_();_nop_();_nop_();_nop_();_nop_();

    _nop_();_nop_();_nop_();_nop_();_nop_();

    _nop_();_nop_();_nop_();_nop_();_nop_();

}

  

void main()

{  

    u16 time_data,CM_data;

    TRIG = 0; 

    ECHO = 0;

 

    LED_Init(); //Initialize LED hardware module

    EA = 1; //Close the main interrupt switch

    TIM1_Init(1000,0); //1ms timing, used to refresh the digital tube display, no fine-tuning is required if the timing accuracy is not high

 

    TMOD &= 0xF0;

    TMOD |= 0x01; 

    ET0 = 1;

    while(1)

    {   

        TH0 = 0; 

        TL0 = 0; 

        FLAG = 0; //If the measurement was out of range last time, FLAG was set to 1, and it should be cleared to 0 this time.

 

        //Start transmitting sound waves

        TRIG = 1;    

        delay_20us();

        TRIG = 0;

 

        while(!ECHO); //The sound wave starts to be emitted, waiting for the ECHO pin to be pulled high to exit this loop

        TR0 = 1; //When the ECHO pin is pulled high, the timer is turned on to count 

        while(ECHO==1 && FLAG==0); //When receiving the signal from the sound wave, the ECHO pin is pulled low and the loop is exited; if the timer overflows, the interrupt function will be executed "FLAG=1;"

                                   //That is, the measured distance is too far, and no reflected signal is received. The condition of "FLAG==0" is not met, so this loop statement can only be ended.  

        TR0 = 0; //Close the timer and end the count

 

        time_data =TH0;

        time_data=(time_data<<8)|TL0; //Combining the values ​​in two eight-bit registers into a 16-bit variable value, which is the timer count value

        CM_data = time_data/54; //Get the expression of centimeters

 

        if(FLAG==1){ShowNumber(999); delay_ms(100);} //Show 999 when out of range. The purpose of delay: for example, when measuring between 5cm and 6cm, the digital tube will frequently display between 5 and 6. Adding delay will make them display less frequently.   

        else {ShowNumber(CM_data); delay_ms(100);}     

    }

}

  

void TIM0_IRQHandler() interrupt 1   

{

    FLAG=1;

}

 

void TIM1_IRQHandler() interrupt 3

{

    TH1 = T1RH; //Reload the reload value

    TL1 = T1RL;

    SEG_Scan();

}


Reference address:51 single chip microcomputer-ultrasonic module

Previous article:51 single chip microcomputer-servo and buttons
Next article:51 MCU-Serial Communication Introduction

Latest Microcontroller 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号