标题:
超声波测距 51单片机程序
[打印本页]
作者:
17190187950
时间:
2018-6-21 12:12
标题:
超声波测距 51单片机程序
超声波测距并显示在LCD1602上。
#include<reg51.h>
#include<intrins.h>
#include"lcd1602.h"
uchar s;
sbit TRIG=P1^0;
sbit ECHO=P3^2;
uint time=0;
bit flag=0;
/********************************************************/
void StartModule() //启动模块
{
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;
}
void Init_Timer()
{
TMOD=0x09; //设T0为方式1,GATE=1;
TH0=0;
TL0=0;
TR0=1;
ET0=1; //允许T0中断
EA=1; //开启总中断
}
void zd0() interrupt 1 //T0中断用来计数器溢出,超过测距范围
{
flag=1; //中断溢出标志
}
/********************************************************/
void Conut()
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
s=(time*1.7)/100; //算出来是CM
if((s>=700)) //超出测量范围显示“-”
flag=1;
}
/****************************************/
void WPartDistance_1602(uchar dat)
{
uchar bai,shi,ge;
bai=dat/100;
shi=dat/10%10;
ge=dat%10;
WData_1602(0x30+bai);
Delay_ms(5);
WData_1602(0x30+shi);
Delay_ms(5);
WData_1602(0x30+ge);
Delay_ms(5);
WData_1602(0x43);
Delay_ms(5);
WData_1602(0x4d);
Delay_ms(5);
}
void WText_1602()
{
WAdd_1602(0x00+0);
WData_1602(0x44);
Delay_ms(5);
WData_1602(0x69);
Delay_ms(5);
WData_1602(0x73);
Delay_ms(5);
WData_1602(0x74);
Delay_ms(5);
WData_1602(0x61);
Delay_ms(5);
WData_1602(0x6e);
Delay_ms(5);
WData_1602(0x63);
Delay_ms(5);
WData_1602(0x65);
Delay_ms(5);
WData_1602(0x3a);
Delay_ms(5);
}
void WDistance_1602()
{
WAdd_1602(0x40+3);
WPartDistance_1602(s);
}
void WError_1602()
{
WAdd_1602(0x40+3);
WData_1602(0xb0);
Delay_ms(5);
WData_1602(0xb0);
Delay_ms(5);
WData_1602(0xb0);
Delay_ms(5);
}
void Display_1602()
{
WText_1602();
if(flag)
{
flag=0;
WError_1602();
}
else
{
WDistance_1602();
}
}
void main()
{
Init_1602();
Init_Timer();
while(1)
{
StartModule();
while(ECHO); //当RX为1计数并等待
Conut(); //计算
Display_1602();
Delay_ms(80); //80MS
}
}
复制代码
超声波测距2.rar
2018-6-21 12:11 上传
点击文件名下载附件
下载积分: 黑币 -5
42.76 KB, 下载次数: 61, 下载积分: 黑币 -5
作者:
Avenger
时间:
2019-3-29 14:30
值得学习!
作者:
dats333
时间:
2019-5-8 18:20
感谢分享
作者:
spiritdust
时间:
2019-5-13 12:07
用的是什么测距模块?
作者:
无衣
时间:
2019-5-23 19:21
延时程序在????、TH0=0;
TL0=0;用不了呀
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1