The realization of ultrasonic distance measurement and display (with prompt tone) based on 51 single-chip computer key control

Source: Internet
Author: User
Tags clear screen bit definition

First introduce myself, I am an undergraduate student of Changchun Polytechnic University, technical or a small white, the main learned or single-chip has been hardware these things, but are not common, in view of my professional photoelectric, so I want to go the main direction or laser, now in the effort to learn laser, this blog is my first blog post , is also a freshman last semester when a similar to the assessment of works such things, and then sent out to prove that they have done something, I hope everyone can accommodate me this small (CAI) White (JI), I will learn a lot from the experience of Daniel, accept the views.

The following is the text:

1. Programme design:

1.1 Selection of devices :

HC-SR04 Ultrasonic module has the characteristics of high detection range precision, stable performance and small blind area, which makes the ranging more accurate and more reasonable.

The single-chip microcomputer of the system using Acer Crystal Company's stc90c516rd+ Single-chip, is the original STC89 series of upgraded version, with stronger anti-interference, anti-static ability, better reset effect, low power consumption, fast.

The buzzer uses the passive buzzer, compared with the active buzzer, although the difficulty becomes big, must use the square wave of the 2k~5k to drive it, but the sound frequency is controllable, and the price is more inexpensive.

Lcd1602 Industrial Character LCD can display 16x02 32 characters at the same time, compared with other liquid crystal displays, the display of letters and numbers is more convenient, simple control and low cost.

Press the button with a separate button.

As shown in figure:


1.2 Module Improvements

Ultrasonic module Processing: As the problem requires 7 meters, so the general ultrasonic ranging module is certainly not up to, so either through hardware improvements, or through software improvements. Here's how to improve your hardware:

Physical drawing:



Schematic diagram:


The above figure marks the resistance R3, can adjust the maximum detection distance. The R3 resistance is 392, the detection distance is about 4.5M, the detection angle is less than 15 degrees, the R3 resistance is 472, the detection distance is about 7M, the detection angle is less than 30 degrees, the factory default 392, namely the maximum detection distance of 4.5M. The R3 resistor is big, the receiving part gain is high, the detection distance is big, but the detection angle will correspondingly become big, easy to detect the object next to the front. So the precision is lowered to 2mm to 1cm.

The ultrasonic module can be fixed by welding the row seat and the needle in the hole board: simultaneously more convenient connection pin and the replacement ultrasonic module.

2. Circuit schematic diagram, hardware connection diagram

Lcd1602 's RW, RS, e-foot and d0~d7 pins are connected by manual welding to the P0~P7 pin of the SCM. Figure:


Buzzer:

Key: through software delay shake

Hardware Connection diagram:


Description: The hc-sr04 trig foot connection P2.3,echo foot connection P2.4,VCC, GND respectively connected to the Single-chip microcomputer VCC, GND.

P1 connects 8 separate buttons to start ranging.

P0 connection D0~d7 foot, e-foot connection P2.7,RW = P2^5,rs foot Connection P2.6 (the internal circuit has been welded in advance, no external connection required).

The buzzer picks up the P2.1.

3. Algorithm part

By detecting whether the key is pressed or pressed, start detection and then detect if the key is released. At the same time buzzer sound prompts to start detection, to trig feet at least 10us high level signal, the module automatically send 8 40khz square wave, automatic detection of whether there is a signal return, there is a signal return, through the echo foot output a high level, high level duration is the ultrasound from the launch to the return time. Just use the timer to record the Echo's high level time t, and then by Formula S= (T*V)/2, you can calculate the distance s,

And then through the lcd1602 to display.

4. The procedure

#include <reg52.h> #include <intrins.h> #define UCHAR unsigned char #define UINT unsigned int #define KEY p1//	Define keys for P1 #define LCD1602_DATAPINS P0//Definition d0~d7 sbit lcd1602_e = p2^7;
Bit definition E, RW, RS sbit lcd1602_rw = p2^5;
Sbit lcd1602_rs = p2^6; Sbit trig = p2^3; 
Define ultrasonic hair, receive sbit echo = p2^4;	Sbit BF = p0^7; Definition read busy sbit beep=p2^1;//buzzer void check ()//Declaration detection function void delay1ms (UINT c);//Delay error 0us void lcdwritecom (Uchar com);/write instruction function Vo ID lcdwritedata (uchar dat);/write Data function void Lcdinit ();//1602 Initialize function void count ();//Compute Distance UCHAR busytest ();//Read busy function uint Scank (  )//scan keyboard uint Time,keyvalue,s; Define global variable time, key value, distance void Main () {Uchar x[]= "0123456789cm";//tmod=0x01;//set working mode ea=1;//Open total interrupt et0=1;//Open timer allow interrupt th0=0;//
	Assign initial value tl0=0;
		while (1) {int a=100;  Keyvalue=scank ();
				Scan keyboard if (keyvalue!=0) {while (a--)/buzzer ring {beep=1;
				DELAY1MS (1);
				Beep=0;
				DELAY1MS (1);	   Check ();  Ultrasonic ranging count (); Calculate distance lcdinit (); 1602 initialization function Lcdwritedata (x[s/100]);//display of hundreds of LcdwriTedata (X[S%100/10]);	10-bit Lcdwritedata (x[s%100%10]);
			Single-digit Lcdwritedata (x[10]); Lcdwritedata (x[11]);
		
			Unit delay1ms (100);  while (1)//Keep display {Keyvalue=scank ();
			Scan keyboard if (keyvalue!=0) break; }}}/**************************************************************************************/void Check ()//detection
	function {trig=1;
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
	_nop_ ();
trig=0;
   } UINT Scank () {UINT i=0;
   keyvalue=0;
	   if (KEY!=0XFF)//detect key is pressed {delay1ms (5);//Shake} if (Key!=0xff)//again to detect whether the key press {Keyvalue=key;
	   while ((Key!=0xff) && (I&LT;50))//Detection button is sent to open {i++;
  } delay1ms (5);//Shake Back keyvalue;//return key value} void count ()//Compute function {while (!echo);////When echo foot is 1, start to remember Tr0=1;
  while (echo);//When the echo foot is 0, close the timer tr0=0;
  Time=th0*256+tl0; Th0=0;
  tl0=0;	s= (time*1.7)/100;
	Unit cm} void lcdwritecom (Uchar com)//write command {while (Busytest () ==1);     lcd1602_e = 0;	   To enable lcd1602_rs = 0;	   Select input Command LCD1602_RW = 0;     Select Write Lcd1602_datapins = com;		Put into command delay1ms (1);	          Wait for data to stabilize lcd1602_e = 1;	  Write timing delay1ms (5);
Keep Time lcd1602_e = 0;
	} void Lcdwritedata (Uchar dat)//write Data {while (Busytest () ==1);	lcd1602_e = 0;	Enable to clear zero lcd1602_rs = 1;	Select input data LCD1602_RW = 0; Select Write Lcd1602_datapins = dat;

	Write Data delay1ms (1);   Lcd1602_e = 1;   Write timing delay1ms (5);
Keep Time lcd1602_e = 0;  } void Lcdinit ()//lcd initialization subroutine {lcdwritecom (0x38);  function setting [8-bit data],[dual row display],[5*8 dot matrix display mode] lcdwritecom (0x0c);  Open display does not show cursor lcdwritecom (0x06);  Write a pointer plus 1 lcdwritecom (0x01);  Clear Screen lcdwritecom (0x80);
	Set data pointer start} void delay1ms (UINT c)//error 0US {Uchar a,b;
		 for (; c>0; c--) {for (b=199;b>0;b--) {for (a=1;a>0;a--); }} Uchar Busytest (void)/Read busy, 1 is busy, 0 is not busy {bit result;//bitDefinition, one byte lcd1602_e = 1;	
	lcd1602_rs = 0;	
    LCD1602_RW = 1;
	DELAY1MS (1); RESULT=BF;
	Determine whether the busy state lcd1602_e=0;


return result; }
The premise is that you have to add 51 SCM Library Reg52.h, I am using the Keil compilation environment.

5. The problems encountered

By changing the resistance of the method to enlarge the gain, increase the measurement distance, the original measurement range from 4.5 to 7 meters, but the angle will increase to 30 degrees, will detect the front side of the object. The results show that the stability is reduced, and the error data are large and the precision is reduced.

6. Conclusion

This paper expounds the improved method of ultrasonic module and the method of connecting various modules, using the key, lcd1602, buzzer and other modules, change the resistance of the ultrasonic module to solve the problem of short distance measurement, based on the 51 SCM key to control ultrasonic ranging and display (with a hint) has a large range of measurement, high stability characteristics , can be better to complete the ranging work.

Related Article

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.