标题:
超声波测距显示程序
[打印本页]
作者:
十三省
时间:
2018-6-23 10:47
标题:
超声波测距显示程序
超声波测距显示程序
#include <reg52.h>
#include <intrins.h>
#include <math.h>
/************************************宏定义************************************/
#define VELOCITY_30C 3495 //30摄氏度时的声速,声速V= 331.5 + 0.6*温度;
#define VELOCITY_23C 3453 //23摄氏度时的声速,声速V= 331.5 + 0.6*温度;
#define uchar unsigned char
#define uint unsigned int
/************************************位定义************************************/
sbit LED = P1^4; //危险距离指示灯
sbit rs = P3^5; //LCD显示器重置端
sbit lcden = P3^4; //LCD显示器使能端
sbit INPUT = P3^6; //回声接收端口 echo
sbit OUTPUT = P3^7; //超声触发端口trig
sbit ds = P1^0; //温度传感器通讯
sbit Ztai=P1^1; //电机运行状态传输通道1 U5,P2.5
sbit Cju=P1^2; //测距状态传输通道2 U5,P2.4
sbit warn=P1^3; //连接至U5 P3.2 INT_0
/********************************定义变量和数组********************************/
uchar table1[]="There's no echo.";
uchar table2[]="system online^_^";
uchar table3[]="Distance:";
uchar count;
long int distance=0; //距离变量
uint Di; //报警距离
/***********************************函数声明***********************************/
extern void initLCD();
extern void write_date(uchar date);
extern void write_com(uchar com);
extern void delay(uint x);
/******************************************************************************/
void delay(uint x)
{
uint a,b;
for(a = x;a > 0;a--)
{
for(b = 10;b > 0;b--)
{
;
}
}
}
void delayt(uint x)
{
uchar j;
while(x-- > 0)
{
for(j = 0;j < 125;j++)
{
;
}
}
}
void write_com(uchar com)
{
P2 = com;
rs = 0;
lcden = 0;
delay(10);
lcden = 1;
delay(10);
lcden = 0;
}
void write_date(uchar date)
{
P2 = date;
rs = 1;
lcden = 0;
delay(10);
lcden = 1;
delay(10);
lcden = 0;
}
void writeString(uchar * str, uchar length)
{
uchar i;
for(i = 0; i < length; i++)
{
write_date(str[i]);
}
}
void initLCD(void)
{
write_com(0x38);
delay(20);
write_com(0x0f);
delay(20);
write_com(0x06);
delay(20);
write_com(0x01);
delay(20);
}
/*********温度传感器**************/
void dsInit()
{
unsigned int i;
ds = 0;
i = 100;
while(i>0) i--;
ds = 1;
i = 4;
while(i>0) i--;
}
void dsWait()
{
unsigned int i;
while(ds);
while(~ds);
i = 4;
while(i > 0) i--;
}
bit readBit()
{
unsigned int i;
bit b;
ds = 0;
i++;
ds = 1;
i++; i++;
b = ds;
i = 8;
while(i>0) i--;
return b;
}
unsigned char readByte()
{
unsigned int i;
unsigned char j, dat;
dat = 0;
for(i=0; i<8; i++)
{
j = readBit();
dat = (j << 7) | (dat >> 1);
}
return dat;
}
void writeByte(unsigned char dat)
{
unsigned int i;
unsigned char j;
bit b;
for(j = 0; j < 8; j++)
{
b = dat & 0x01;
dat >>= 1;
if(b)
{
ds = 0; i++; i++;
ds = 1;
i = 8; while(i>0) i--;
}
else
{
ds = 0;
i = 8; while(i>0) i--;
ds = 1;
i++; i++;
}
}
}
void sendChangeCmd() //发出改变指令
{
dsInit();
dsWait();
delayt(1);
writeByte(0xcc);
writeByte(0x44);
}
void sendReadCmd() //发出读取指令
{
dsInit();
dsWait();
delayt(1);
writeByte(0xcc);
writeByte(0xbe);
}
int getTmpValue()
{
unsigned int tmpvalue;
int value;
float t;
unsigned char low, high;
sendReadCmd();
low = readByte();
high = readByte();
tmpvalue = high;
tmpvalue <<= 8;
tmpvalue |= low;
value = tmpvalue;
t = value * 0.0625;
value = t * 100 + (value > 0 ? 0.5 : -0.5); //大于0加0.5, 小于0减0.5
return value;
}
void display1(int v)
{
unsigned char Count;
unsigned char datas[] = {0, 0, 0, 0, 0};
unsigned int tmp = abs(v);
datas[0] = tmp / 10000;
datas[1] = tmp % 10000 / 1000;
datas[2] = tmp % 1000 / 100;
datas[3] = tmp % 100 / 10;
datas[4] = tmp % 10;
write_com(0xc0);
if(v < 0)
{
writeString("- ", 2);
}
else
{
writeString("+ ", 2);
}
if(datas[0] != 0)
{
write_date('0'+datas[0]);
}
for(Count = 1; Count != 5; Count++)
{
write_date('0'+datas[Count]);
if(Count == 2)
{
write_date('.');
}
}
write_date('A'+2); //"c"的ASCII
}
/*********************************/
void Init_MCU(void)
{
TMOD=0x11; //定时器0,1初始化,设置为16位自动重装模式
TL0=0x66;
TH0=0xfc; //1ms
EA=1; //总中断使能
ET0=1; //开定时器0
}
void Init_Parameter(void) //I/O口初始化
{
OUTPUT =1;
INPUT = 1;
count = 0;
distance = 0;
}
void display_char(uchar *point,uchar address) //显示字符串函数,写入的字符串的地址指针 1602显示对应的地址
{
uchar i;
write_com(0x80 + address);
for(i = 0;i < 16; i++)
{
write_date(*point);
point++;
}
}
void display(int number,uchar address) //显示数字,number写入的数据,address地址
{
uchar b,c,d,e;
b= (number / 1000);
c= (number / 100) % 10;
d= (number / 10) % 10;
e= number % 10;
Di=b*100+c*10+d; //获得警报距离
write_com(0x80 + address);
write_date(b + 48);
write_date(c + 48);
write_date(d + 48);
write_date(46); //小数点的ASCII
write_date(e + 48);
write_date(99); //"c"的ASCII
write_date(109); //"m"的ASCII
if(Di<20) //发出警报
{
if(Di==0)//提示为假警报
{
LED=1;
warn=1;
Cju=1; //关闭中断控制
}
if(Di!=0)
{
LED=0;
warn=0; //开启中断控制
Cju=0;
}
}
if(Di>=20)
{
LED=1;
warn=1;
Cju=1; //关闭中断控制
}
}
void Trig_SuperSonic(void) //发出声波
{
OUTPUT = 1;
delayt(1);
OUTPUT = 0;
}
void Measure_Distance(void)
{
uchar l;
uint h,y;
TR0 = 1;
while(INPUT)
{
;
}
TR0 = 0;
l = TL0;
h = TH0;
y = (h << 8) + l;
y = y - 0xfc66;//us部分
distance = y + 1000 * count;//计算总时间
TL0 = 0x66;
TH0 = 0xfc;
delayt(30);
distance = VELOCITY_30C * distance / 20000; //单位是cm
}
void main(void)
{
initLCD();
Init_MCU();
Init_Parameter();
display_char(table2,0x00);
sendChangeCmd();
delayt(1000); //温度转换时间需要750ms以上
write_com(0xc0);
display1(getTmpValue());
while(1)
{
//电机运转的时候不测距
if(Ztai==1)
{
Trig_SuperSonic(); //触发超声波发射
while(INPUT == 0) //等待回声
{
;
}
Measure_Distance(); //计算脉宽并转换为距离
display(distance,0x49); //显示距离
Init_Parameter(); //参数重新初始化
delayt(100); //延时,两次发射之间要至少有10ms间隔
}
if(Ztai==0)
{
;
}
}
}
/********************************中断程序*****************************************/
void Timer0 (void) interrupt 1 //回收超声波回声
{
TF0 = 0;
TL0 = 0x66;
TH0 = 0xfc;
count++;
if(count == 18)//超声波回声脉宽最多18ms
{
TR0 =0;
TL0 = 0x66;
TH0 = 0xfc;
count = 0;
}
}
复制代码
超声波测距显示.zip
2018-6-23 10:46 上传
点击文件名下载附件
下载积分: 黑币 -5
60.31 KB, 下载次数: 4, 下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1