51 Ultrasonic Distance Measurement

Source: Internet
Author: User
#include "Reg51.h" #include <intrins.h> sbit rx=p2^1;
Sbit tx=p2^0;
unsigned int time=0;
unsigned int timer=0;
unsigned char posit=0;
unsigned long s=0;
Bit flag = 0;
--¶¨òåê¹óãµäio--//#define Gpio_dig P0 sbit lsa=p2^2;
Sbit lsb=p2^3;

Sbit lsc=p2^4; --¶¨òå諾ö±äá¿--//unsigned char code dig_code[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c,
0X39,0X5E,0X79,0X71};
0¡¢1¡¢2¡¢3¡¢4¡¢5¡¢6¡¢7¡¢8¡¢9¡¢a¡¢b¡¢c¡¢d¡¢e¡¢fµäïôê¾âëunsigned Char disbuff[4] ={0,0,0,0,}; /******************************************************************************* *º¯êýãû: DigDisplay *º¯êý¹¦ä ü:ê¹óãêýâë¹üïôê¾*êäèë:îþ*êä³ö:îþ***************************************************
	/void Digdisplay () {unsigned char i;
	unsigned int J; for (i=0;i<8;i++) {switch (i)//î»ñ¡£¬ñ¡ôñµãááµäêýâë¹ü£¬{case (0): lsa=0; lsb=0; lsc=0; Break;//ïôê¾µú0î»case (1): lsa=1; lsb=0; lsc=0; Break;//ïôê¾µú1î»case (2): lsa=0; Lsb=1; lsc=0; Break;//ïôê¾µú2î»case (3): lsa=1; Lsb=1; lsc=0; Break;//ïôê¾µú3î»case (4): lsa=0; lsb=0; Lsc=1; Break;//ïôê¾µú4î»case (5): lsa=1; lsb=0; Lsc=1; Break;//ïôê¾µú5î»case (6): lsa=0; Lsb=1; Lsc=1; Break;//ïôê¾µú6î»case (7): lsa=1; Lsb=1; Lsc=1;						 break;//ïôê¾µú7î»} gpio_dig=disbuff[i];//¢ëí¶îâëj=10;	
		ɨãè¼ä¸ôê±¼äé趨while (j--); Gpio_dig=0x00;//ïûòþ}}/********************************************************/void Conut (void) {time=TH0*25
	 6+tl0;
	 th0=0;
	
	 tl0=0;     S= (Long) (time*0.17); ëã³öà´êçcm if ((s>=4000) | |
	  flag==1)//³¬³ö²âá¿¶î§ïô꾡°err0¡±{flag=0;	   disbuff[0]=0x3f;	   ¡°-¡±disbuff[1]=0x50;	   ¡°-¡±disbuff[2]=0x50;	   ¡°-¡±disbuff[3]=0x79;
	  ¡°-¡±} else {disbuff[3]=dig_code[s%10000/1000];
	  DISBUFF[2]=DIG_CODE[S%1000/100];
	  DISBUFF[1]=DIG_CODE[S%100/10];
	 DISBUFF[0]=DIG_CODE[S%10/1]; }
	}
/**************************************/void Zd0 () Interrupt 1//t0öð¶ïóãà´¼æêýæ÷òç³ö,³¬¹ý²â¾à ¶î§{flag=1; Öð¶ïòç³ö±êö¾}/********************************************************/void Zd3 () Interrupt 3 {TH1=0XF
	 8;
	 tl1=0x30;
	 Digdisplay ();
	 timer++;
	  if (timer>=100) {timer=0;			                Tx=1; 
	  800MS Start a module _nop_ (); 
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ ();
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ (); 
	  _nop_ ();
	  _nop_ ();
	 tx=0;		   
	}}/*********************************************************/void Main (void) {tmod=0x11;
	th0=0;          
	tl0=0;		   
	Th1=0xf8;
	tl1=0x30;            
	Et0=1;			   
	Et1=1;			  
	Tr1=1;			   

	Ea=1; while (1) {while (!		RX);			 No ultrasound detected, RX is low level tr0=1;		Timer 0 is used to prevent the distance is too long, the number is too large to display, make a limit while (RX);
	 An ultrasonic wave is detected, which calculates the distance based on the time difference.tr0=0;			
	Conut (); }

  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.