想做一個小車,stc89c52單片機藍牙模塊+兩個超聲波程序出現問題,藍牙可以通信,電機可以運轉,但是超聲波沒有反應,請教各位教教我,程序應該怎么改!
#include "reg52.h"
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit IN1=P1^0;
sbit IN2=P1^1;
sbit IN3=P1^2;
sbit IN4=P1^3;
sbit IN5=P1^4;
sbit IN6=P1^5;
sbit IN7=P1^6;
sbit IN8=P1^7;
uchar receive_data;
unsigned int time=0;
unsigned long S=0,S1=0,S2=0;
sbit Trig = P2^1;
sbit Echo = P2^0;
sbit Trig1 = P2^4;
sbit Echo1 = P2^5;
void delayms(unsigned int ms)
{
unsigned char i=100,j;
for(;ms;ms--)
{
while(--i)
{
j=10;
while(--j);
}
}
}
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=(time*1.7)/100;
}
void go(void)
{
P1=0xAA;
if(S1<30&&S2<30)
{P1=0x5A;}
}
void stop (void)
{
P1=0x00;
}
void StartModule()
{
Trig=1;
Trig1=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
Trig=0;
Trig1=0;
}
void UsartConfiguration()
{
SCON=0X50;
TMOD=0X20;
PCON=0X00;
TH1=0XFd;
TL1=0XFd;
TR1=1;
ES = 1;
EA = 1;
}
void Com_Int(void) interrupt 4
{
EA=0;
if(RI == 1)
{
receive_data = SBUF;
RI = 0;
if(receive_data == '1')
{
go();
}
if(receive_data == '2')
{
stop();
}
EA=1;
}
}
void main(void)
{
TMOD=0x01;
TH0=0;
TL0=0;
UsartConfiguration();
while(1)
{
StartModule();
while(!Echo);
TR0=1;
while(Echo);
TR0=0;
Conut();
S1=S;
delayms(10);
StartModule();
while(!Echo1);
TR0=1;
while(Echo1);
TR0=0;
Conut();
S2=S;
delayms(10);
}
}
|