51单片机控制超声波模块

电路连接

在这里插入图片描述

超声波模块 单片机 备注
VCC VCC 电源
GND GND 地线
Echo P2^0 接收信号
Trig P2^1 发射信号

测量范围 2—400cm

  • HCSR04模块有四个引脚,VCC,GND,TRIG和ECHO;这些引脚有不同的功能。
  • VCC和GND是HSCR04的直接驱动电源。这些引脚需要分别连接到+5v电压和地。
  • TRIG引脚负责发射超声波信号的信号引脚。这个引脚需要用超过10us的高电平来启动,每一点HCSR04会发射8个40khz的方波。方波发射后,ECHO引脚会输出高电平。
  • ECHO引脚是用来测量距离的数据引脚。当一个超声波信号发射后,ECHO引脚会输出高电平。当ECHO引脚直到检测到超声波信号回来的时,ECHO引脚输出低电平。

The HC­SR04 has four pins, VCC, GND, TRIG and ECHO; these pins all have different functions. The VCC and GND pins are the simplest ­­ they power the HC­SR04.These pins need to be attached to a +5 volt source and ground respectively. There is asingle control pin: the TRIG pin. The TRIG pin is responsible for sending the ultrasonic burst. This pin should be set to HIGH for 10 μs, at which point the HC­SR04 will send out an eight cycle sonic burst at 40 kHZ. After a sonic burst has been sent the ECHO pin will go HIGH. The ECHO pin is the data pin ­­it is used in taking distance measurements. After an ultrasonic burst is sent the pin will go HIGH, it will stay high until an ultrasonic burst is detected back, at which point it will go LOW.

参考代码

#include <REG52.H>

/*     自定义变量类型    */
typedef unsigned char uchar;   //0~255
typedef unsigned int uint;     //0~65535

sbit RX = P2^0;
sbit TX = P2^1;
uchar flag;
uchar date_distance[5];
uchar kk;

/* 用于控制数码管的引脚 */
sbit LSA = P2^2;
sbit LSB = P2^3;
sbit LSC = P2^4;

uchar LSA_NODE1[] = {0,1,0,1,0,1,0,1,0};
uchar LSB_NODE1[] = {0,1,1,0,0,1,1,0,0};
uchar LSC_NODE1[] = {0,1,1,1,1,0,0,0,0};

//共阴数码管码表 0-F
uchar SMG[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0X00};

//延时10us
void delay_10us(uchar i)
{
    while(i--);
}
//延迟1ms
void delay_ms(uint c)   //误差 0us
{
    uint a,b;
    for(;c>0;c--)
        for(b=102;b>0;b--)
            for(a=3;a>0;a--);
}
/* 数码管选择函数 */
void init_smg(uchar i)
{
    LSA = LSA_NODE1[i];
    LSB = LSB_NODE1[i];
    LSC = LSC_NODE1[i];
}
//初始化定时器
void init_timer()
{
	TMOD = 0x11; 	//打开定时器0 1
	//初始化定时器 0
    TH0 = 0x00;
    TL0 = 0x00;
    EA = 1;
    ET0 = 1;
    //TR0 = 1;
	//初始化定时器 1
	TH1 = 0xF8; //定时2ms
    TL1 = 0xCD;
    ET1 = 1;
    TR1 = 1;
}
//计算距离
void count_distance()
{
	//s = 340m/s * time us /2 = 170*time *10^-6 m = 0.17*time mm
	uint distance = (TH0*256 + TL0*1)*0.17;  //单位mm
	TH0 = 0;
	TL0 = 0;
	if(distance>=4000 || distance < 20)     //超出测量范围显示“ERR0”
	{
	  	flag = 1;
	}
	else
	{
		date_distance[1] = distance%10000/1000;
		date_distance[2] = distance%1000/100;
		date_distance[3] = distance%100/10;
		date_distance[4] = distance%10/1;
	}
}
void display()
{
	uchar i;
	if(flag == 1)
	{
		flag = 0;
		date_distance[1] = 14;
		date_distance[2] = 14;
		date_distance[3] = 14;
		date_distance[4] = 14;
	}

	for(i = 1;i<=4;i++)
	{
		init_smg(i);
		if(i==1)
			P0 = SMG[date_distance[i]] |0x80;
		else
			P0 = SMG[date_distance[i]];
		delay_ms(1);
		P0 = 0x00;
	}

}

void main()
{
	init_timer();
	while (1)
	{
		while(!RX);		//等待TX发出
		TR0 = 1;        //开始计时
		while(RX);      //当TX发出时,RX为高电平
		TR0 = 0;		//终止计时
		count_distance();
	}
}
//定时器 0 用来计算距离
void timer0() interrupt 1
{
	flag = 1;
}
//定时器 1 用来数码管显示数据和发射超声波信号
void timer1() interrupt 3
{
	TR1 = 0; //终止定时
	TH1 = 0xF8; //定时2ms
    TL1 = 0xCD;

	kk ++;
	display();
	if(kk>100)   //每200ms启动一次超声波模块
	{
		kk = 0;
		//启动超声波模块
		TX = 1;
		delay_10us(2);
		TX = 0;
	}
	TR1 = 1;
}

猜你喜欢

转载自blog.csdn.net/qq_39592312/article/details/107500591