我做了个超声波距离感应小车但是有问题,求大佬帮看一下啊问题在哪
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit Trig=P1^6;
sbit Echo=P3^2;
sbit wela=P2^7;
sbit dula=P2^6;
sbit test=P1^5;
sbit PWM1=P2^0;
sbit PWM2=P2^1;
sbit PWM3=P2^3;
sbit PWM4=P2^4;
void delay(uint);
uint time,timeL,timeH,c;
bit succeed_flag=0;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//
void delay_20us()
{
uchar a;
for(a=0;a<100;a++);
}
//***************************************************************
void display(uint temp)
{
uchar ge,shi,bai;
bai=temp/100;
shi=(temp%100)/10;
ge=temp%10;
wela=1;
P0=0xf7;
wela=0;
dula=1;
P0=table[bai];
dula=0;
delay(10);
dula=1;
P0=0x00;
dula=0;
wela=1;
P0=0xef;
wela=0;
dula=1;
P0=table[shi];
dula=0;
delay(10);
dula=1;
P0=0x00;
dula=0;
dula=1;
P0=table[ge];
dula=0;
wela=1;
P0=0xdf;
wela=0;
delay(10);
dula=1;
P0=0x00;
dula=0;
}
//***************************************************************
void main()
{
uint distance;
test =0;
Trig=0;
EA=1;
TMOD=0x10;
while(1)
{
delay(10);
EA=0;
Trig=1;
delay_20us();
Trig=0;
while(Echo==0);
succeed_flag=0;
EA=1;
EX0=1;
TH0=0;
TL0=0;
ET0=1;
TF0=0;
TR0=1;
while(Echo==1);
TR0=0;
EX0=0;
if(succeed_flag==1)
{
distance=timeH*256+timeL;
distance=distance/420;
if(distance>=120)
{
c=70;
TMOD=0x01;
TH1=0xff;
TL1=0xf7;
EA=1;
ET1=1;
TR1=1;
}
else
{
c=80-distance/6;
TMOD=0x01;
TH1=0xff;
TL1=0xf7;
EA=1;
ET1=1;
TR1=1;
}
TR1=0;
display(distance);
}
if(succeed_flag==0)
{
distance=0;
test = !test;
c=70;
TMOD=0x01;
TH1=0xff;
TL1=0xf7;
EA=1;
ET1=1;
TR1=1;
}
}
}
//***************************************************************
void exter() interrupt 0 // ????0?0?
{
timeH =TH0;
timeL =TL0;
succeed_flag=1;
EX0=0;
// num=num+1;
}
//****************************************************************
void tim0() interrupt 3
{
TR1=0;
TH1=0xff;
TL1=0xf7;
TR1=1;
time++;
if(time>=100) time=0;
if(time<=c) PWM2=0,PWM3=0;
else PWM2=1,PWM3=1;
PWM1=0,PWM4=0;
}
|