最近在做超声波测距,写完代码后,把买来的模块插到
单片机发现固定一个值不变,求各位大神解答!
#include<reg52.h>
unsigned int i=0;
sbit tring=P2^0;
sbit echo=P2^1;
sbit beep=P1^5;
unsigned int qian,bai,shi,ge,
time,s,time1,time2,flag=0;
unsigned char code DIG_CODE[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
bit succeed_flag;
void delay_20us()
{
unsigned int bt ;
for(bt=0;bt<100;bt++);
}
void delay(unsigned int x)
{
unsigned int y;
for(;x>0;x--)
for(y=110;y>0;y--);
}
void INT0_time() interrupt 0 //外部中断
{
time1=TH1;
time2=TL1;
time=time1*256+time2;
succeed_flag=1;
EX0=0;
}
void T1_time() interrupt 3 //计时
{
TH1=0;
TL1=0;
}
void display() //显示
{
P2=0x00;
P0=DIG_CODE[ge];
delay(3);
P2=0x04;
P0=DIG_CODE[shi];
delay(3);
P2=0x08;
P0=DIG_CODE[bai];
delay(3);
P2=0x0c;
P0=DIG_CODE[qian];
delay(3);
}
void main() //主函数
{
flag=0;
tring=0;
TMOD=0x11;
TR0=1;
IT0=0;
ET0=0;
EX0=0;
EA=1;
while(1)
{
EA=0;
tring=1;
delay_20us();
tring=0; //发送一个20us的高电平
while(echo==0); //检测回波信号是否为高
succeed_flag=0;
TH1=0;
TL1=0;
TF1=0;
TR1=1;
while(echo==1);
EA=1;
EX0=1;
IE0=1;
EX0=0;
TR1=0;
if(succeed_flag==1)
{
s=(long)(time*0.17);
qian=s/1000;
bai=s/100%10;
shi=s%100/10;
ge=s%10;
}
display();
}
}
一周热门 更多>