51 MCU HCSR04 ultrasonic rangefinder basic code

Publisher:雅逸之风Latest update time:2016-05-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
>_<: Ultrasonic rangefinder module:

51 MCU HCSR04 ultrasonic rangefinder basic code

>_<:51 MCU, 11.0592MHz crystal oscillator, basic example of sending collected data to the serial port:

51 MCU HCSR04 ultrasonic rangefinder basic code

>_<:Code:

  1 /************************************************ *************************************************** *********/
  2 //HC-SR04 Ultrasonic Distance Measurement Module DEMO Program
  3 //Crystal: 11.0592
  4 // Wiring: module TRIG connects to P1.2 ECH0 connects to P1.1
  5 //Serial port baud rate 9600
  6/************************************************ *************************************************** *********/      
  7 #include 
  8 #include 
  9 #include 
 10
 11 #define uchar unsigned char
 12 #define uint unsigned int  
 13 #define RX P1_1
 14 #define TX P1_2
 15
 16
 17 unsigned int time=0;
 18 unsigned int timer=0;
 19 float S=0;
 20 bit flag =0;
 twenty one
 twenty two
 twenty three /************************************************ **********/
 24 void Conut(void)
 25 {
 26 time=TH0*256+TL0;
 27 TH0=0;
 28 TL0=0;
 29 S=(time*1.87)/100; //The calculated value is CM
 30 if(flag==1) //Out of measurement
 31 {
 32 flag=0;
 33 printf("--\n");
 34 }    
 35 printf("S=%f\n",S);
 36 }
 37 /************************************************ **********/
 38 void delayms(unsigned int ms)
 39 {
 40 unsigned char i=100,j;
 41 for(;ms;ms--)
 42 {
 43 while(--i)
 44 {
 45 j=10;
 46 while(--j);
 47 }
 48 }
 49 }
 50 /************************************************ **********/
 51 void zd0() interrupt 1 //T0 interrupt is used for counter overflow, exceeding the ranging range
 52 {
 53 flag=1; //interrupt overflow flag
 54 }
 55 /************************************************ **********/
 56 void StartModule() //T1 interrupt is used to scan the digital tube and count 800MS to start the module
 57 {
 58 TX=1; //800MS start the module once
 59 _nop_();
 60 _nop_();
 61 _nop_();
 62 _nop_();
 63 _nop_();
 64 _nop_();
 65 _nop_();
 66 _nop_();
 67 _nop_();
 68 _nop_();
 69 _nop_();
 70 _nop_();
 71 _nop_();
 72 _nop_();
 73 _nop_();
 74 _nop_();
 75 _nop_();
 76 _nop_();
 77 _nop_();
 78 _nop_();
 79 _nop_();
 80 TX=0;
 81 }
 82 /************************************************ **********/
 83 void main(void)
 84 {  
 85 TMOD=0x21; //Set T0 to mode 1, GATE=1;
 86 SCON=0x50;
 87 TH1=0xFD;
 88 TL1=0xFD;
 89 TH0=0;
 90 TL0=0;
 91 TR0=1;  
 92 ET0=1; //Enable T0 interrupt
 93 TR1=1; //Start the timer
 94 TI=1;
 95     
 96 EA=1; // Enable general interrupt
 97     
 98     
 99 while(1)
100 {
101 StartModule();
102 while(!RX); //Wait when RX is zero
103 TR0=1; //Start counting
104 while(RX); //When RX is 1, count and wait
105 TR0=0; //Close counting
106 Conut(); //Calculation
107 delayms(10); //100MS
108 }
109 }              

 

Reference address:51 MCU HCSR04 ultrasonic rangefinder basic code

Previous article:MCU Xiaobai's Learning Series 3 Encountering 51 MCU
Next article:Analysis of startup code provided by Keil (STARTUP.A51)

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号