当前位置:首页 > 单片机课程设计超声波测距离 - 图文
while(t--) {
//大致延时1mS DelayUs2x(245);
DelayUs2x(245);
} }
3.2.2、键盘扫描程序
void key_scanf(void)
{
if(p10==0) {
DelayMs(10); if(p10==0) a=1;
} if(p11==0) { DelayMs(10); if(p11==0) a=0;
}
if(p12==0) {
DelayMs(10);
if(p12==0)
13
{ a=0;
disbuff[0]=0; disbuff[1]=0; disbuff[2]=0; disbuff[3]=0; } }
}
3.2.3、超声波模块驱动程序void zd3() interrupt 3
{ key_scanf(); TH1=0xf8; TL1=0x30; timer++; if(timer>=400) {
timer=0; TX=1;
//800MS _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
_nop_();
中断用来驱动超声波模块
每800ms就发送一次超声波 这样就能进行连续多次测距 启动一次模块
14
//T1
_nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();
TX=0;//给超声波模块发送端一个10us的高电平,就能发送40khz的超声波
}
}
3.2.4、计算程序 void Conut(void)
{ float S1;
time=TH0*256+TL0; TH0=0; TL0=0;
S1=(time*1.7)/100; //算出来是CM S=S1*10;
if((S>=7000)||flag==1) //超出测量范围显示“-”
15
{ flag=0;
disbuff[0]=10; //“-” disbuff[1]=10; //“-” disbuff[2]=10; //“-” disbuff[3]=10; //“-” } else {
disbuff[0]=S/1000; disbuff[1]=S/100; disbuff[2]=S/10; disbuff[3]=S; }
}
3.2.5、数码管显示程序
4位共阳数码管的驱动程序如下: void Display(void) //扫描数码管
{ p20=0; p21=1; p22=1; p23=1;
P0=discode[disbuff[0]];//第1个数码管点亮 DelayUs2x(20); P0=0xff;
p20=1;
16
共分享92篇相关文档