STC15 MCU uses HC-SR04 ultrasonic ranging module

Publisher:PeacefulWarriorLatest update time:2019-05-22 Source: eefocusKeywords:STC15 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

【HC-SR04】

HC-HR04 ultrasonic distance measurement can provide non-contact distance sensing function of 2cm-400cm, and the ranging accuracy can reach 3mm.


Basic working principle

1. Use IO port TRIG to trigger the 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 returned.


3. When a signal is returned, a high level is output through the IO port ECHO. The duration of the high level is the time from the ultrasonic wave being emitted to the return. Test distance = (high level time * sound speed (340m/s))/2.



Timing diagram


The above timing diagram shows that you only need to provide a pulse trigger signal of more than 10uS, and the module will: send out 8 40kHz cycle levels and detect echoes. Once an echo signal is detected, an echo signal is output. The pulse width of the echo signal is proportional to the measured distance. Therefore, the distance can be calculated by the time interval from the transmitted signal to the received echo signal. Formula: uS/58=cm or uS/148=inch; or: distance = high level time * sound speed (340M/S) /2; It is recommended that the measurement cycle be more than 60ms to prevent the influence of the transmitted signal on the echo signal.



【program】

I use the STC15F2K60S2 microcontroller, and the internal crystal oscillator is set to 12M. The following program omits irrelevant statements such as pin definitions, and only provides program ideas for reference.


uchar Flag_hypertelorism = 0; //Ultrasonic distance is too far flag

float distance = 0;

uchar Flag_Fucker = 0; //Ultrasonic on flag

 

void main()

{

    Time0_Init();

    Time1_Init();

    while(1)

    {

        if(Flag_Fucker == 1)

            Ultrasonic();

    }

}

 

/*Initialize timer 0*/

void Time0_Init(void)

{

// AUXR |= 0x80; //Timer 0 is in 1T mode

AUXR &= 0x7f; //Timer 0 is in 12T mode

    TMOD = 0x00; //Set the timer to 16-bit auto-reload

    TL0 = 0x60; //Initialize the timing value to 4ms

    TH0 = 0xF0;

    TR0 = 1; //Timer 0 starts timing

    ET0 = 1; // Enable timer 0 interrupt

    EA = 1;

}

 

//Timer 1 initialization

void Time1_Init()

{

// AUXR |= 0x40; //Timer 1 is in 1T mode

AUXR &= 0xdf; //Timer 1 is in 12T mode

    TMOD = 0x00; //Set timer 1 to 16-bit auto-reload

    TL1 = 0x60; //Initialize the timing value to 4ms

    TH1 = 0xF0;

// TR1 = 1; // Temporarily disable timer 1

ET1 = 1; // Enable timer 1 interrupt

    EA = 1;

}

 

/*Timer 0 interrupt*/

void Time0 () interrupt 1

{

static uchar time = 0;

// Can be omitted in automatic loading mode

// TL0 = T0MS; 

// TH0 = T0MS >> 8;

time++;

if(time == 25) //Measure distance once every 100ms

{

time = 0;

Flag_Fucker = 1;

}

}

 

//Timer 1 overflow interrupt

void Time1() interrupt 3

{

Flag_hypertelorism = 1; //The ultrasonic distance is too far

}

 

//Ultrasonic distance measurement

void Ultrasonic()

{

uchar i;

Trig = 1; //Ultrasonic trigger signal is turned on

_nop_(); //Delay more than 10us

i = 30;

while(--i);

Trig = 0; //Ultrasonic trigger signal is turned off

while(!Echo); //Wait when Echo is 0

TR1 = 1; //Start timer 1

while(Echo == 1 && TF1 == 0); //Wait when Echo is 1

TR1 = 0; // Turn off timer 1

Distance_Count(); //Calculate distance

Flag_Fucker = 0; //Turn off ultrasonic ranging

}

 

// Distance value calculation

void Distance_Count()

{

float Text_time = 0;

if(Flag_hypertelorism == 0)

{

Text_time = ((TH1 - temp_TH) * 256 + (TL1 - temp_TL)) / 2.0;

TL1 = 0x60; //Initialize the timing value to 4ms

TH1 = 0xF0;

distance = Text_time * 0.34; //Unit: mm

}

else

{

distance = 9999;

Flag_hypertelorism = 0;

}

}



Keywords:STC15 Reference address:STC15 MCU uses HC-SR04 ultrasonic ranging module

Previous article:51 MCU uses slot type optical coupler speed measurement module
Next article:STC15F2K60S2 sets the internal clock operating frequency

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号