STC89C52RC MCU Ultrasonic Distance Measurement Program

Publisher:dandan666Latest update time:2017-09-02 Source: elecfansKeywords:STC89C52RC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The STC89C52RC single-chip ultrasonic ranging program and electronic production object collected on the Internet are tested using the single-chip minimum system. The ultrasonic part uses the HC-SR04 module, with a measurement range of 4-300cm. The actual measurement can reach up to 5.70m. The following is a physical picture, and the source program is attached at the end of the article.

STC89C52RC MCU Ultrasonic Distance Measurement Program

4-300cm, the longest actual measurement can reach 5.7m

#include 'AT89X51.H' //header file

unsigned char code fseg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

unsigned char code segbit[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

unsigned char disbuf[8]={0,0,0,0,0,0,0,0};

//------------------------------------------------ --------------------------

// Function prototype definition

#define uchar unsigned char

#define uint unsigned int

void main (void); // Main function

void LED4_Display (void); // LED display

void LED_OUT(uchar X); //LED single byte serial shift function

void delayms(uint); //delay sub-function ms

void jisuan(void);

unsigned char code LED_0F[]; //LED font table

sbit DIO=P1^0; //Serial data input

sbit RCLK=P1^1; //Clock pulse signal——rising edge is valid

sbit SCLK=P1^2; //Input signal————Rising edge is valid

sbit echo=P3^2; //echo

sbit trig=P3^3; //trig

//------------------------------------------------ --------------------------

// Global variables

uchar LED[8]; //8-bit display buffer for LED

uint temp;                                                                                        

uint temp1;

uint a,b; //define a variable a, b, the latter is used to determine whether the signal is received

//

// Main program

//

void main(void) //main function starts

{    

        uint f;

    echo=0; //Pull down the echo and trig pins first

    trig=0;

        f=500;

    while(f>0); //Start delay to eliminate the fluctuation caused by the first power-on

        {

           LED[3]=16;

      LED[2]=15;

      LED[1]=15;

      LED[0]=16;

         f--;

       }

    EA=1; //Open the general interrupt

    TMOD=0x11; //Set the timer to mode 1

    ET0=1; //Enable timer interrupt This is mainly to prevent the ultrasonic module from not sending a signal

    ET1=1;

    while(1)

    {

        echo=0;//a assignment

        a=0;        

        b=1;

        TH0=0; //Initial value of timer

        TL0=0;

        TH1=(65536-25000)/256; //Timer initial value

        TL1=(65536-25000)%6;

        trig=1; //trig sends high

        LED4_Display (); // Delay 3ms

        LED4_Display();

        LED4_Display();

        LED4_Display();

        LED4_Display();

        LED4_Display();

        LED4_Display();

        trig=0; //trig is sent low

        TR1=1;

        while(echo==0); //Wait for echo to become high level

                if(b==1) determines whether the signal is received

        {

                                                                

            TR1=0; //Turn off timer 1

            EX0=1; //Open external interrupt

            TR0=1; //Start the timer

            while(a==0); //Note here! a is assigned 0 before. The program stops here and waits for interruption.

//Originally I added a screen scanning program here, but I found a major BUG so I deleted it  

        }

        else

        {

        

            LED[3]=16;

            LED[2]=15;

            LED[1]=15;

            LED[0]=16;

        LED4_Display();

        LED4_Display();

        LED4_Display();

        LED4_Display();

        LED4_Display();

        LED4_Display();

        

        }

    }

}

void waibu() interrupt 0 //External interrupt service subroutine

{

    temp=TH0; //Get the value of the timer

    temp1=TL0;                                                                                    

    EX0=0; //Disable external interrupt

    TR0=0; //Turn off the timer

    jisuan(); //Run calculation subroutine

    a=1; //a is assigned a value of 1. The program returns to while(a) because the value of a has become 1, and the program starts from the beginning.

}

void time1() interrupt 1 //Timer interrupt service subroutine

{

    TH0=0; //Reload initial value

    TL0=0;

}

void time2() interrupt 3

{

    TR1=0;

    TH0=(65536-25000)/256; //Timer initial value

    TL0=(65536-25000)%6;

    b=0;

    echo=1;

    

}

void jisuan(void) //Calculation subroutine

{

    uint c,d; //define a variable c, d to determine the distance

    c=0; //assign value 0

    d=0; //Reassign value to b

    LED4_Display (); //Scan the digital tube

    temp=(temp<<8)+temp1; //TH0 TL0 merge

    temp=temp/5; //I didn't calculate it exactly, so I just divided it by 5 to get an approximate value

    

    if(temp>40) //Judge whether the distance is too close

        {

            c=1;

        }

        LED4_Display (); //Scan the digital tube

    if(temp<40)

        {

         c=0;

        }

        LED4_Display();

        if(temp<3000) //Judge whether the distance is too far

        {

            d=1;

        }

        LED4_Display();

    if(temp>3000)

        {

         d=0;

        }

    c=c&d; //AND operation

    if(c==1) //Judge whether the distance is normal

        {

                                                            

            LED[3]=temp/1000; //Separate display of values

            LED[2]=temp00/100;

            LED[1]=(temp000/10)+20; //Why do we need to add 20? Because this is the unit digit and needs to display a decimal point  

            LED[0]=temp000;

        }

    if(c==0) //Judge whether the distance is normal

        {

    

            LED[3]=16;

            LED[2]=14;

            LED[1]=14;

            LED[0]=16;

        }

    

}

void delayms(uint xms) //delay sub-function ms

    {

        uint i,j;

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

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

    }

    

    //The following program is the hc595 module display program

    //The program of each module can be transplanted to the routine of the data

    //

void LED4_Display (void) //Screen scanning sub-function   

{

    unsigned char code *led_table; // table pointer

    uchar i;

    // Display the first

    led_table = LED_0F + LED[0];

    i = *led_table;

    LED_OUT(i);            

    LED_OUT(0x01);        

    RCLK = 0;

    RCLK = 1;

    //Display the second digit

    led_table = LED_0F + LED[1];

    i = *led_table;

    LED_OUT(i);        

    LED_OUT(0x02);        

    RCLK = 0;

    RCLK = 1;

    //Display the 3rd digit

    led_table = LED_0F + LED[2];

    i = *led_table;

    LED_OUT(i);            

    LED_OUT(0x04);    

    RCLK = 0;

    RCLK = 1;

    //Display the 4th digit

    led_table = LED_0F + LED[3];

    i = *led_table;

    LED_OUT(i);            

    LED_OUT(0x08);        

    RCLK = 0;

    RCLK = 1;

}

void LED_OUT(uchar X)

{

    uchar i;

    for(i=8;i>=1;i--)

    {

        if (X&0x80) DIO=1; else DIO=0;

        X<<=1;

        SCLK = 0;

        SCLK = 1;

    }

}

//The following is the display array

unsigned char code LED_0F[] = 

{// 0 1 2 3 4 5 6 7 8 9 A b C d E F - .

   0xc0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x8C,0xBF,0xC6,0xA1,0x86,0x8e,0xbf,0x7f,0x00,0x00,

// 20 21 22 23 24 25 26 27 28 29 30

    0x40,0x79,0x24,0x30,0x19,0x12,0x12,0x78,0x00,0x10,0xbf,

};


Keywords:STC89C52RC Reference address:STC89C52RC MCU Ultrasonic Distance Measurement Program

Previous article:STC12C2052 single chip LED dot matrix electronic clock production
Next article:Analysis of simulation results of ultrasonic distance measurement using 51 single chip microcomputer

Recommended ReadingLatest update time:2024-11-23 13:47

Design of automotive LED center grid scanning light using STC89C52RC
This article introduces the design and production method of a car grille scanning light. It mainly uses the common STC89C52RC microcontroller as the main controller, uses a photocoupler for signal conversion, and uses the piranha LED light strip purchased on the market to modify eight groups of LED scanning lights. It
[Microcontroller]
Design of automotive LED center grid scanning light using STC89C52RC
STC89C52RC features and pin introduction
STC89C52RC is a small single-chip microcomputer launched by Hongjing Company. It is a commonly used device for electronic engineers . Its main features are the use of Flash memory technology, which reduces the manufacturing cost. Its software and hardware are fully compatible with MCS-51, and it is manufactured using h
[Microcontroller]
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号