#include "stc12c5a.h"
#include”LCD.h“
#include "sys.h"
sbit Trag = P1^0;
sbit Echo = P3^2;
sbit Key = P3^3;
bit successed_flag;
unsigned char time,timeH,timeL,count=1;
unsigned char string[16];
unsigned long distance=0;
void delay_us(unsigned char time)
{
while(time--);
}
void delay_ms(unsigned int time)
{
unsigned char a,b;
for(;time>0;time--)
for(b=133;b>0;b--)
for(a=6;a>0;a--);
}
void string_change(unsigned long distance)
{
string[0]=distance/10000+48;
string[1]=distance/1000%10+48;
string[2]=distance/100%100%10+48;
string[3]=distance%100/10+48;
string[4]=distance%100%10+48;
string[5]='.';
string[6]='0';
string[7]='\0';
}
void main()
{
LCD_Init();
P1M1=1;
P1M0=1;
Trag=0;
delay_ms(50);
EA=1;
TMOD=0X10;
write_location(0x00);
LCD_ShowString(" I am OK!",12);
while(1)
{
EA=0;
Trag=1;
delay_us(20);
Trag=0;
while(!Echo);
successed_flag=0;
EA=1;
//IT0=1;
EX0=1;
TH1=0;
TL1=0;
TF1=0;
TR1=1;
delay_ms(20);
TR1=0;
EX0=0;
if(successed_flag==1)
{
time=timeH*256+timeL;
distance=(unsigned int)(time*0.172); //厘米
string_change(distance);
write_location(0x40);
LCD_ShowString(string,16);
delay_ms(50);
}
if(successed_flag==0)
{
distance=0; //没有回波则清零
}
//EA=0;
//EA=1;
}
}
void Int() interrupt 0
{
successed_flag=1;
timeH=TH1;
timeL=TL1;
EX0=0;
//time=timeH*256+timeL;
//distance=(time*1.7)/100; //算出来是CM
}
void timer1() interrupt 3
{
TH1=0;
TL1=0;
}
我用51开发板调试的时候,测出来的数据完全不对,并且资料说测量距离有4米,但我连20厘米距离都不到它就没反应了,还有就是测久了它显示数字时就卡机了,动都不动,查过很多资料,试过很多网上的代码都出现类似情况,想问问是代码出现什么问题呢,还是有可能超声波模块坏了,麻烦帮忙解答,感激不尽啊,这东西困扰我好多天了!!!
|