超声波测距的经验
各位谁有超声波测距将数据显示在数码管上的成功经验,或是程序,或者是需要注意的问题。板子是普中科技V3.0 HC6800-ES-V2.0 发表于 2017-11-8 08:23
我的板子是HC6800-ES-V2.0的,就超声波测距实验,用法是一样的。
买板子时送的例程中有这个例子。
#inclu ...
有连线图吗 我的板子是HC6800-ES-V2.0的,就超声波测距实验,用法是一样的。
买板子时送的例程中有这个例子。
#include"reg51.h"
#include <intrins.h>
sbit RX=P2^1;
sbit TX=P2^0;
unsigned inttime=0;
unsigned inttimer=0;
unsigned char posit=0;
unsigned long S=0;
bit flag =0;
//--定义使用的IO--//
#define GPIO_DIG P0
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
//--定义全局变量--//
unsigned char code DIG_CODE={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的显示码
unsigned char disbuff ={ 0,0,0,0,};
/*******************************************************************************
* 函 数 名 : DigDisplay
* 函数功能 : 使用数码管显示
* 输 入 : 无
* 输 出 : 无
*******************************************************************************/
void DigDisplay()
{
unsigned char i;
unsigned int j;
for(i=0;i<8;i++)
{
switch(i) //位选,选择点亮的数码管,
{
case(0):
LSA=0;LSB=0;LSC=0; break;//显示第0位
case(1):
LSA=1;LSB=0;LSC=0; break;//显示第1位
case(2):
LSA=0;LSB=1;LSC=0; break;//显示第2位
case(3):
LSA=1;LSB=1;LSC=0; break;//显示第3位
case(4):
LSA=0;LSB=0;LSC=1; break;//显示第4位
case(5):
LSA=1;LSB=0;LSC=1; break;//显示第5位
case(6):
LSA=0;LSB=1;LSC=1; break;//显示第6位
case(7):
LSA=1;LSB=1;LSC=1; break;//显示第7位
}
GPIO_DIG=disbuff;//发送段码
j=10; //扫描间隔时间设定
while(j--);
GPIO_DIG=0x00;//消隐
}
}
/********************************************************/
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S= (long)(time*0.17); //算出来是CM
if((S>=4000)||flag==1) //超出测量范围显示“ERR0”
{
flag=0;
disbuff=0x3f; //“-”
disbuff=0x50; //“-”
disbuff=0x50; //“-”
disbuff=0x79; //“-”
}
else
{
disbuff=DIG_CODE;
disbuff=DIG_CODE;
disbuff=DIG_CODE;
disbuff=DIG_CODE;
}
}
/********************************************************/
void zd0() interrupt 1 //T0中断用来计数器溢出,超过测距范围
{
flag=1; //中断溢出标志
}
/********************************************************/
voidzd3()interrupt 3 //T1中断用来扫描数码管和计800MS启动模块
{
TH1=0xf8;
TL1=0x30;
DigDisplay();
timer++;
if(timer>=100)
{
timer=0;
TX=1; //800MS启动一次模块
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TX=0;
}
}
/*********************************************************/
voidmain(void)
{
TMOD=0x11; //设T0为方式1,GATE=1;
TH0=0;
TL0=0;
TH1=0xf8; //2MS定时
TL1=0x30;
ET0=1; //允许T0中断
ET1=1; //允许T1中断
TR1=1; //开启定时器
EA=1; //开启总中断
while(1)
{
while(!RX); //当RX为零时等待
TR0=1; //开启计数
while(RX); //当RX为1计数并等待
TR0=0; //关闭计数
Conut(); //计算
}
}
页:
[1]