Design of ultrasonic distance measurement based on 51 single chip microcomputer (with temperature compensation)

Publisher:EternalBlissLatest update time:2019-11-21 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

An ultrasonic rangefinder is made using the STC89C52 microcontroller, ultrasonic module (HC-RS04), DS18B20 digital temperature sensor and LCD1602 liquid crystal display module. The first line of the LCD screen displays the temperature and ultrasonic speed "T:30°C V:349m/s", and the second line displays the measured distance "S=X.XXXm".

The designed rangefinder measures in meters, accurate to 3 decimal places (mm), and has a measuring range of 0.05m~5m.

1. Basic part

1. LCD display function 

(1) When the power is turned on, the first line of the LCD screen displays the temperature and ultrasonic velocity, for example, "T: 30°C V: 349m/s", and the second line displays the measured distance "S = 0.000m"

(2) When you operate the corresponding function button, the first line of the LCD screen displays the temperature and ultrasonic velocity, for example, "T:30°C V:349m/s", and the second line displays the measured distance "S=X.XXXm".

(3) This ultrasonic rangefinder uses 4 independent buttons. The first button is for reset function; the second button is for single measurement function; the third button is for instantaneous continuous measurement function; the fourth button is for average continuous measurement function.

(4) The first button is for reset. Whenever you press this button, the second line of the LCD screen will display the measured distance "S=0.000m".

(5) The second button is for single measurement. Each time you press the button, the ultrasonic wave is emitted and the second line of the LCD screen displays a corresponding measurement distance "S=X.XXXm".

(6) The third button is for instantaneous continuous measurement. Press this button to continuously transmit ultrasonic waves, and the second line of the LCD screen will display the instantaneous measured distance "S=X.XXXm". Press this button again to keep the number for easy reading.

(7) The fourth button is for the average continuous measurement function. Press this button to continuously transmit ultrasonic waves, and the second line of the LCD screen will display the average measured distance "S=X.XXXm". Press this button again to keep the number for easy reading.


(8) The fifth button is the automatic/manual temperature setting button. It switches to manual temperature setting. At this time, you can use the plus and minus keys to adjust the temperature within the range of 0 to 50 degrees. Press the button again to return to the automatic temperature control state, and repeat this cycle.

(9) The sixth button is the temperature increase button. When in manual temperature control mode, pressing this button can increase the temperature to a maximum of 50 degrees Celsius.

(10) The 7th button is the temperature minus button. When in manual temperature control mode, pressing this button can reduce the temperature to a minimum of 0 degrees Celsius.


The program code is as follows:

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

/* Ultrasonic ranging (automatic/manual temperature correction) */

/* (ultrasonic module + 18B20 + 1602 LCD) */

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

#include

#include

#define uchar unsigned char 

#define uint unsigned int

#define LcdData P0 //1602 data port


sbit LCD_RS=P2^6; //1602 RS port

sbit LCD_RW=P2^5; //1602 RW port

sbit LCD_EN=P2^7; //1602 EN port

sbit Echo=P2^4; //HC-SR04 receiving port

sbit Trig=P2^3; //HC-SR04 transmitter port

sbit Resets_Key=P1^0; //Reset key

sbit Single_Key=P1^1; // Single measurement key error is large 

sbit Contin_Key=P1^2; //Continuous measurement key, small error 

sbit Averag_Key=P1^3; //Continuous (average measurement) key press, small error 

sbit Setting_Key=P1^4; //Temperature correction button

sbit Add_Key=P1^5; //Temperature plus 1 button

sbit Sub_Key=P1^6; //Temperature minus 1 button

sbit DQ=P3^7; //DS18B20 single bus interface


bit Temp_Flag; //Positive and negative temperature flag: Temp_Flag=0 if the temperature is positive, otherwise 1

uint temp=25; //temperature value

bit flag_flow=0,flag_one=0,flag_clear=0,flag_con1=0,flag_con2=0,flag_temp=0;

uchar i=0,m,j,k;

uint time=0,S=0,S1=0,totle=0;

float V=346.0;

uint Sav[11]; //10 average values ​​array for continuous measurement

uchar Line1[16]={"T: C V:346m/s"}; //1602 The first line of initial character display array

uchar Line2[16]={"S= m "}; //1602 second line initial character display array


void Delayms(uchar xms); //delay xms function

void WriteLcd(uchar Dat,bit x); //1602 write function

void InitLcd(void); //1602 initialization function

void DisplayLcd(); //1602 display function

void init(); //initialization function

void keyscan(); //key scan function

void StartModule(); //Start module function

void Conut(void); //Measurement calculation function

void Delayus(uchar xus); //us level delay function

bit Init_DS18B20(void); //Initialize DS18B20 function

uchar Read_DS18B20(void); //Read DS18B20 function

void Write_DS18B20(uchar Dat); //write DS18B20 function

void GetTemp(); //Get temperature function

void CalcTestTemp(); //temperature processing function


void main(void) //main function

{

        init();

        InitLcd();

        while(1)

        {

                keyscan(); //key scan function

            DisplayLcd();

            if(flag_temp==0)

            {

                    GetTemp();

                    CalcTestTemp();

            }

            if(flag_one==1||flag_con1==1||flag_con2==1)

            {

                    StartModule(); //Start transmitting ultrasonic waves

                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

            Delayus(200);

                flag_one=0;

            }

        } 

}


void Delayms(uchar xms) //delay ms function

{

    uchar i,j;

    for(i=xms;i>0;i--)

            for(j=110;j>0;j--);

}


void WriteLcd(uchar Dat,bit x) //1602 write function (x=0 when writing instructions, x=1 when writing data)

{

        LCD_EN=0;

        LcdData=Dat;

        LCD_RS=x;

        LCD_RW=0;

        LCD_EN=1;

        Delayms(1);

        LCD_EN=0;

}


void InitLcd(void) //1602 initialization function

{

        WriteLcd(0x38,0); //Function setting (38H), 8-bit data, 2-line display, 5*7 dot matrix

        WriteLcd(0x0C,0); //Display on/off setting (0CH), display on, cursor off, cursor does not flash

        WriteLcd(0x06,0); //Input mode setting (06H), after reading or writing a character, the address pointer increases by 1, and the cursor increases by 1

        WriteLcd(0x01,0); // Clear the display (01H), clear the data in the data RAM

}


void DisplayLcd() //LCD display function

{

    uchar y;

    V=(331.4+temp*0.607);

    Line1[2]=temp/10+0x30;

    Line1[3]=temp%10+0x30;

    Line1[4]=0xDF; //Display the small circle in front of C in ℃

    Line1[10]=(uint)V/100+0x30;

    Line1[11]=(uint)V%100/10+0x30;

    Line1[12]=(uint)V%10+0x30;

    if(flag_clear==1)

    S1=0;

    if((S1>=7000)||flag_flow==1) //Out of measurement range, display "-"

        {         

            flag_flow=0;

            Line2[2]='-';

            Line2[3]='.';

            Line2[4]='-';

            Line2[5]='-';

            Line2[6]='-';

        }

    else

        {

            Line2[2]=S1/1000+0x30;

            Line2[3]='.';

            Line2[4]=S1%1000/100+0x30;

            Line2[5]=S1%100/10+0x30;

            Line2[6]=S1%10+0x30;

        }

    WriteLcd(0x80,0); //Set the first line address

    for(y=0; y<16; y++)

        WriteLcd(Line1[y],1);

    WriteLcd(0xc0,0); //Set the second line address

    for(y=0; y<16; y++)

        WriteLcd(Line2[y],1);

}


void init() //initialization function

{

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

    TH0=0;

    TL0=0;          

    ET0=1; //Enable T0 interrupt

    EA=1; // Enable general interrupt

}


void keyscan() //key scan function

{

    if(Resets_Key==0) //Reset key

    {

        Delayms(5);

        if(Resets_Key==0)

        {

                while(Resets_Key==0);

            flag_clear=1;

            }

    }

    if(Single_Key==0) //Single measurement key

    {

        Delayms(5);

        if(Single_Key==0)

        {

                while(Single_Key==0);

                flag_one=1;

            flag_con1=0;

                flag_con2=0;

                flag_clear=0;

            }

    }

    if(Contin_Key==0) //Continuous instantaneous measurement key

    {

        Delayms(5);

        if(Contin_Key==0)

        {

                while(Contin_Key==0);

                flag_con1=~flag_con1;

                if(flag_con1==1)

                {

                    flag_one=0;

                    flag_con2=0;

                    flag_clear=0;

                }

            }

    }

    if(Averag_Key==0) //Continuous average measurement key

    {

        Delayms(5);

        if(Averag_Key==0)

        {

                while(Averag_Key==0);

                flag_con2=~flag_con2;

                if(flag_con2==1)

                {

                    flag_one=0;

                    flag_con1=0;

                    flag_clear=0;

                }

            }

    }

    if(Setting_Key==0) //Automatic and manual conversion keys

    {

        Delayms(5);

        if(Setting_Key==0)

        {

                 while(Setting_Key==0);

                 flag_temp=~flag_temp;

            }

    }

    if(Add_Key==0) //Temperature reduction key

    {

        Delayms(5);

        if(Add_Key==0)

        {

                while(Add_Key==0);

                if(flag_temp==1)

                {

                temp++;

                    if(temp>=50)

                    temp=50;

                }

            }

    }

    if(Sub_Key==0) //Temperature key

    {

        Delayms(5);

        if(Sub_Key==0)

        {

                while(Sub_Key==0);

                if(flag_temp==1)

                {

                    if(temp>0)

                temp--;

                    if(temp<=0)

                    temp=0;

                }

            }

    }

}


void StartModule() //Start module function (trigger once, provide a high level greater than 10us)

{

    Trig=1; //Start the module once

    Delayus(15); //Delay 2i+5=35us

    Trig=0;

}


void Conut(void) //Measurement calculation function

{

    time=TH0*256+TL0;

[1] [2]
Reference address:Design of ultrasonic distance measurement based on 51 single chip microcomputer (with temperature compensation)

Previous article:Tutorial on single chip servo control program and proteus simulation schematic diagram
Next article:Design of digital FM radio using single chip microcomputer + CXA1019S + phase-locked loop BU2614

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号