HC-SR04 ultrasonic module 51+PIC microcontroller routine

Publisher:lxy64420245Latest update time:2019-10-30 Source: 51heiKeywords:HC-SR04 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The microcontroller source program is as follows:

//Crystal oscillator = 8M

//MCU=STC10F04XE

//P0.0-P0.6 common anode digital tube pin

//Trig = P1^0

//Echo = P3^2

#include //Include a 52 standard kernel header file

#define uchar unsigned char //define for easy use

#define uint unsigned int

#define ulong unsigned long

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

sfr CLK_DIV = 0x97; //Defined for STC microcontroller, system clock division

                     //Set the address definition for the IO port of the STC microcontroller

sfr P0M1 = 0X93;

sfr P0M0 = 0X94;

sfr P1M1 = 0X91;

sfr P1M0 = 0X92;

sfr P2M1 = 0X95;

sfr P2M0 = 0X96;

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

sbit Trig = P1^0; //Generate pulse pin

sbit Echo = P3^2; //Echo pin

sbit test = P1^1; //test pin


uchar code SEG7[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //digital tube 0-9

uint distance[4]; //Distance measurement receiving buffer

uchar ge,shi,bai,temp,flag,outcomeH,outcomeL,i; //Custom register

bit succeed_flag; //Measurement success flag

//********Function declaration

void conversion(uint temp_data);

void delay_20us();

//void pai_xu();


void main(void) // Main program

{ uint distance_data,a,b;

   uchar CONT_1;   

   CLK_DIV=0X03; //The system clock is 1/8 crystal oscillator (pdf-45 pages) 

     P0M1 = 0; //Set the io port to push-pull output

     P1M1 = 0;

     P2M1 = 0;

     P0M0 = 0XFF;

     P1M0 = 0XFF;

     P2M0 = 0XFF;

   i=0;

   flag=0;

        test =0;

        Trig=0; //First pull the pulse input pin low

        TMOD=0x11; //Timer 0, timer 1, 16-bit working mode

        TR0=1; //Start timer 0

   IT0=0; //Change from high level to low level, triggering external interrupt

        ET0=1; //Enable timer 0 interrupt

//ET1=1; //Enable timer 1 interrupt

        EX0=0; //Disable external interrupt

        EA=1; //Open interrupt 0        

  

        

while(1) //Program loop

        {

  EA=0;

             Trig=1;

        delay_20us();

        Trig=0; //Generate a 20us pulse at the Trig pin  

        while(Echo==0); //Wait for the Echo pin to become high

             succeed_flag=0; //Clear measurement success flag

             EX0=1; //Open external interrupt

                   TH1=0; //Timer 1 cleared

        TL1=0; //Timer 1 cleared

             TF1=0; //

        TR1=1; //Start timer 1

   EA=1;


      while(TH1 < 30); //Wait for the measurement result, period 65.535 milliseconds (can be implemented by interruption)  

                  TR1=0; //Turn off timer 1

        EX0=0; //Disable external interrupt


    if(succeed_flag==1)

             {         

                   distance_data=outcomeH; //High 8 bits of the measurement result

           distance_data<<=8; //Put in the high 8 bits of 16 bits

                     distance_data=distance_data|outcomeL; //Merge with the lower 8 bits to form 16-bit result data

            distance_data*=12; //Because the timer defaults to 12 division

           distance_data/=58; //microseconds divided by 58 equals centimeters

         } //Why is divided by 58 equal to centimeters, Y meters = (X seconds * 344) / 2

                                                               // X seconds = (2*Y meters)/344 ==》X seconds = 0.0058*Y meters ==》Centimeters = microseconds/58 

    if(succeed_flag==0)

                   {

            distance_data=0; //clear if no echo

                           test = !test; //Test light changes

           }


     /// distance[i]=distance_data; //Put the measurement result data into the buffer

     /// i++;

            /// if(i==3)

          /// {

          /// distance_data=(distance[0]+distance[1]+distance[2]+distance[3])/4;

     /// pai_xu();

     /// distance_data = distance[1];


      

           a=distance_data;

       if(b==a) CONT_1=0;

       if(b!=a) CONT_1++;

       if(CONT_1>=3)

                   { CONT_1=0;

                          b=a;

                          conversion(b);

                        }       

          /// i=0;

          /// }             

         }

}

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

//External interrupt 0, used to determine the echo level

INTO_() interrupt 0 // External interrupt is number 0

{    

     outcomeH =TH1; //Get the timer value

     outcomeL =TL1; //Get the timer value

     succeed_flag=1; //Flag of successful measurement

     EX0=0; //Disable external interrupt

  }

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

//Timer 0 interrupt, used for display

timer0() interrupt 1 // Timer 0 interrupt is number 1

   {

         TH0=0xfd; //Write timer 0 initial value

         TL0=0x77;                 

         switch(flag)   

      {case 0x00:P0=ge; P2=0xfd;flag++;break;

            case 0x01:P0=shi;P2=0xfe;flag++;break;

            case 0x02:P0=bai;P2=0xfb;flag=0;break;

      }

   }

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

/*

//Timer 1 interrupt, used for ultrasonic ranging timing

timer1() interrupt 3 // Timer 0 interrupt is number 1

    {

TH1=0;

TL1=0;

     }

*/

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

//Display data conversion program

void conversion(uint temp_data)  

{  

    uchar ge_data,shi_data,bai_data;

    bai_data=temp_data/100;

    temp_data=temp_data%100; //Remainder operation

    shi_data=temp_data/10;

    temp_data=temp_data%10; //Remainder operation

    ge_data=temp_data;


    bai_data=SEG7[bai_data];

    shi_data=SEG7[shi_data];

    ge_data =SEG7[ge_data];


    EA=0;

    bai = bai_data;

    shi = shi_data;

    ge = ge_data; 

         EA=1;

}

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

void delay_20us()

{ uchar bt ;

    for(bt=0;bt<100;bt++);

}

/*

void pai_xu()

  { uint t;

  if (distance[0]>distance[1])

    {t=distance[0];distance[0]=distance[1];distance[1]=t;} /*Exchange values

  if(distance[0]>distance[2])

    {t=distance[2];distance[2]=distance[0];distance[0]=t;} /*Exchange values

  if(distance[1]>distance[2])

    {t=distance[1];distance[1]=distance[2];distance[2]=t;} /*Exchange values         

    }

*/



Keywords:HC-SR04 Reference address:HC-SR04 ultrasonic module 51+PIC microcontroller routine

Previous article:PIC12F675 motor control program example source code
Next article:pic16f single chip source code, LED0 - LED5 light up in a row

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号