本帖最后由 jurisheng 于 2010-5-23 13:45 编辑
我编了一个智能车跟踪条纹带的程序,不知哪里出了问题,请高手看看
#include <BoeBot.h>
#include <reg52.h>
sbit P1_7=P1^7; //左边红外接收连接到P158
sbit P3_7=P3^7; //右边红外接收连接到P366
sbit P1_2=P1^2; //左边红外发射连接到P143
sbit P2_2=P2^2; //右边红外发射连接到P333
sbit P1_1=P1^1;
sbit P1_0=P1^0;
#define Kpl -70
#define Kpr 70
#define SetPoint 3
#define CenterPulse 1500
unsigned int time;
int leftdistance,rightdistance;//左边和右边的距离
int delayCount,distanceLeft,distanceRight,irDetectLeft,irDetectRight;
unsigned int frequency[5]={37500,38250,39500,40500,41500};
void timer_init(void)
{
EA=1;
ET0 = 1; //允许定时器0中断
TMOD |=0X01 ;
}
void FreqOut(unsigned int Freq)
{
time = 256 - (50000/Freq);
TH0 = 0XFF ;
TL0 = time ;
TR0 = 1;
delay_nus(800);
TR0 = 0;
}
void Timer0_Interrupt(void) interrupt 1
{
P1_2= ~P1_2;
P2_2= ~P2_2;
TH0 = 0XFF;
TL0 = time;
}
void Get_lr_Distances()
{
unsigned char count;
leftdistance = 0; //初始化左边的距离
rightdistance = 0; //初始化右边的距离
for(count = 0;count<5;count++)
{
FreqOut(frequency[count]);
irDetectRight = P3_7;
irDetectLeft = P1_7;
//printf("irDetectLeft = %d ",irDetectLeft);
//printf(" irDetectRight = %d\n ",irDetectRight);
if (irDetectLeft == 1)
leftdistance++;
if (irDetectRight == 1)
rightdistance++;
}
}
void Send_Pulse(unsigned int pulseLeft,unsigned int pulseRight)
{
P1_1=1;
delay_nus(pulseLeft);
P1_1=0;
P1_0=1;
delay_nus(pulseRight);
P1_0=0;
delay_nms(18);
}
int main(void)
{
unsigned int pulseLeft,pulseRight;
timer_init();
while(1)
{
Get_lr_Distances();
pulseLeft=(SetPoint-leftdistance)*Kpl+CenterPulse;
pulseRight=(SetPoint-rightdistance)*Kpr+CenterPulse;
Send_Pulse(pulseLeft,pulseRight);
}
} |