超声波模块控制电机前进 但超声波不工作 求大神指点迷津
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
unsigned int time=0;
unsigned int timer=0;
float S=0;
bit flag=0;
sbit SHINENG1=P1^0;
sbit SHINENG2=P1^5;
sbit IN1=P1^1;
sbit IN2=P1^2;
sbit IN3=P1^3;
sbit IN4=P1^4;
sbit echo=P3^2;
sbit trig=P3^3;
void delay20us(void) //?? -0.46875us
{
unsigned char a,b;
for(b=3;b>0;b--)
for(a=1;a>0;a--);
}
void delay10ms(void) //?? -0.000000000002us
{
unsigned char a,b;
for(b=249;b>0;b--)
for(a=17;a>0;a--);
}
void juli(void)
{
time=0;
S=0;
time=TH0*256+TH0;
TH0=0;
TL0=0;
S=(time*1.87)/100;
if(S>50)
{
SHINENG1=1;
SHINENG2=1;
IN1=0;
IN2=1;
IN3=1;
IN4=0;
}
else
{
SHINENG1=0;
SHINENG2=0;
}
}
void time0() interrupt 1
{
flag=1;
}
void QDCSB()
{
echo=1;
delay20us();
echo=0;
}
void main(void)
{
TMOD=0x01;
TH1=0;
TL1=0;
TH0=0;
TL0=0;
ET0=1;
EA=1;
while(1)
{
QDCSB();
while(!echo);
TR0=1;
while(echo);
TR0=0;
juli();
delay10ms();
}
}
|