标题:
51单片机超声波测距-SR04_12864显示
[打印本页]
作者:
lyczli
时间:
2020-9-18 21:49
标题:
51单片机超声波测距-SR04_12864显示
51单片机;超声波探头用的SR04;显示用的12864;
各个子程序都写成了单独的文件;可用通用;
单片机源程序如下:
#include <reg52.h>
#include <delay.h>
sbit trig=P1^6;
sbit echo=P1^7;
float const Tcy=12/11.0592;//us
float const V0=349.5;//m/s or mm/ms tempreture=30
unsigned int mesure_val,N;
unsigned char mesure_over;
void ini_mesure()
{
float tmax=4000*2/V0;//最大距离4000mm;ms
N=(int)(tmax/Tcy*1000);
TMOD=0x01;
//TH0=(65536-N)/256;
//TL0=(65536-N)%256;
TH0=0;TL0=0;
ET0=1;EA=1;
trig=0;echo=1;//initialize P1
mesure_val=0;
}
void mesure()
{
unsigned int th=0,tl=0;
float time;
trig=1; delay(10);trig=0;//start SR04
while(echo==0);//wait echo high level
TR0=1; //start T0
while(echo==1);//echo high level end
TR0=0;
th=TH0;tl=TL0;
//TH0=(65536-N)/256;
//TL0=(65536-N)%256;//for next
TH0=0;TL0=0;
//time=((th*256+tl)-(65536-N))*Tcy/1000;//ms
time=(th*256+tl)*Tcy/1000;
mesure_val=(int)(time*V0/2);//mm
}
void timer0() interrupt 1//超过最大距离处理
{
//TH0=(65536-N)/256;
//TL0=(65536-N)%256;
TH0=0;TL0=0;
TR0=0;
mesure_over=1;
}
复制代码
所有资料51hei提供下载:
SR04.7z
(13.2 KB, 下载次数: 72)
2020-9-18 21:49 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
daye520
时间:
2020-9-19 09:10
有没有实际做过验证一下?
作者:
lyczli
时间:
2020-9-19 14:48
是实际使用的程序
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1