MCU+LCD1602+HR-04 display screen shows the working principle of ultrasonic ranging

Publisher:初入茅庐Latest update time:2020-08-31 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Basic working principle
(1) Use IO port TRIG to trigger ranging and give a high-level signal of at least 10us.
(2) The module automatically sends 8 40khz square waves and automatically detects whether there is a signal return.
(3) If there is a signal return, a high level is output through the IO port ECHO. The duration of the high level is the time from the ultrasonic wave to the return. The test distance = (high level time * sound speed (340M/s) / 2 (the time for the ultrasonic wave to be transmitted and returned, so it must be divided by 2))




2. Module physical picture

3. Driver
There is a link below the article. Since there is no HR04 module in proteus, another microcontroller can be used to simulate the HR-04 module during simulation, and then the microcontroller and the microcontroller communicate. Serial communication cannot be used because the ultrasonic ranging is using a timer for timing, and the baud rate setting uses timer 1. There is no way to use timer 1 to set the baud rate. The process is a bit troublesome, so I didn't build it, but the others are built.
4. Effect diagram
 

The AT89C52 microcontroller source program is as follows:

/*

Experimental description:

         STC89C52 Crystal: 12Mhz Module: HR04 LCD1602

        LCD1602 LCD module-->MCU pins

        Refer to the LCD1602 liquid crystal display experiment wiring (the experimental phenomenon in the corresponding experimental chapter of the development strategy has wiring instructions)

        Ultrasonic module-->MCU pins

        TRIG-->P21

        ECHO-->P20

        LCD1602_RW-->P2^5

        LCD1602_RS-->P2^6

    LCD1602_E-->P2^7

    Because there is no HR04 module in proteus, HR04 is an analog signal, we can use an analog signal to replace HR04, the processing method is the same

   

Experimental phenomena:

        The distance detected by ultrasonic wave is displayed on LCD1602 in mm.

        Only for communication and learning

*/



#include

#include         

#include "lcd.h"


sbit Trig = P2^1;

sbit Echo = P2^0;




unsigned char PuZh[]=" Pechin Science "; //Display distance

unsigned char code ASCII[15] = {'0','1','2','3','4','5','6','7','8','9','.','-','M'};


static unsigned char DisNum = 0; //display pointer                                 

       unsigned int time=0;

           unsigned long S=0;

           bit flag =0;

           unsigned char disbuff[4] ={ 0,0,0,0,};

/***********************************************************************************

* Function name: main

* Function: Main function

* Input: None

* Output: None

***********************************************************************************/

void Conut(void)

        {

         time=TH0*256+TL0;

         TH0=0;

         TL0=0;

        

         S=(time*1.7)/100; //The calculated value is CM

         if((S>=700)||flag==1) //Out of measurement range, display "-"

         {         

          flag=0;

         

          DisplayOneChar(0, 1, ASCII[11]);

          DisplayOneChar(1, 1, ASCII[10]); //Display dot

          DisplayOneChar(2, 1, ASCII[11]);

          DisplayOneChar(3, 1, ASCII[11]);

          DisplayOneChar(4, 1, ASCII[12]); //Display M

         }

         else

         {

          disbuff[0]=S%1000/100;

          disbuff[1]=S%1000%100/10;

          disbuff[2]=S%1000%10%10;

          DisplayOneChar(0, 1, ASCII[disbuff[0]]);

          DisplayOneChar(1, 1, ASCII[10]); //Display dot

          DisplayOneChar(2, 1, ASCII[disbuff[1]]);

          DisplayOneChar(3, 1, ASCII[disbuff[2]]);

          DisplayOneChar(4, 1, ASCII[12]); //Display M

         }

        }


void zd0() interrupt 1 //T0 interrupt is used for counter overflow, exceeding the ranging range

  {

    flag=1; //interrupt overflow flag

  }


void StartModule() //Start module

  {

          Trig=1; //Start the module once

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          _nop_();

          Trig=0;

  }




void delayms(unsigned int ms)

{

        unsigned char i=100,j;

        for(;ms;ms--)

        {

                while(--i)

                {

                        j=10;

                        while(--j);

                }

        }

}


void main(void)

{


         TMOD=0x01; //Set T0 to mode 1, GATE=1;

         TH0=0;

         TL0=0;         

         ET0=1; //Enable T0 interrupt

         EA=1; // Enable general interrupt        


        LcdInit();

        LcdShowStr(0,0,PuZh);

        while(1)

        {

                 StartModule();

             while(!Echo); //Wait when RX is zero

             TR0=1; //Start counting

             while(Echo); //When RX is 1 count and wait

             TR0=0; //Close the count

         Conut(); //Calculation

                 delayms(80);

        }

                        

}


Keywords:MCU Reference address:MCU+LCD1602+HR-04 display screen shows the working principle of ultrasonic ranging

Previous article:The production of a simple calculator based on 51 single chip microcomputer matrix keyboard
Next article:Use a single-chip microcomputer to create the highest precision time system, which can be transplanted into any clock program

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号