c51吧 关注:1,268贴子:2,950
  • 0回复贴,共1

新人发帖,求助

只看楼主收藏回复

求大神给解释一下下面的程序,这是我们C51与微型机器人课的期末任务,在小车前放一张纸,让小车跟随纸的移动而移动。程序是我同学编的,求大神们给注释一下,谢谢。
#include <BoeBot.h>
#include <uart.h>
#define LeftIR P1_2 //左边红外接受连接到P1_2
#define LeftLaunch P1_3 //左边红外发射连接到P1_3
#define Kpl -70
#define Kpr 70
#define SetPoint 2
#define CenterPulse 1500
unsigned int time;
int leftdistance;//左边的距离
int delayCount,distanceLeft,irDetectLeft;
unsigned int frequency[5]={29370,31230,33050,35700,38460};
void timer_init(void)
{
IE=0x82; //开总中断EA,允许定时器0中断ET0
TMOD |= 0X01; //定时器0工作在模式1:16位定时器模式
}
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
{
LeftLaunch = ~LeftLaunch;
TH0 = 0XFF;
TL0 = time;
}
void Get_lr_Distances()
{
unsigned char count;
leftdistance = 0; //初始化左边的距离
for(count = 0;count<5;count++)
{
FreqOut(frequency[count]);
irDetectLeft = LeftIR;
if (irDetectLeft == 1)
leftdistance++;
}
}
void Send_Pulse(unsigned int pulseLeft)
{
P1_1=1;
delay_nus(CenterPulse+pulseLeft);
P1_1=0;
P1_0=1;
delay_nus(CenterPulse-pulseLeft);
P1_0=0;
delay_nms(18);
}
int main(void)
{
unsigned int pulseLeft;
uart_Init();
timer_init();
while(1)
{
Get_lr_Distances();
pulseLeft=(SetPoint-leftdistance)*Kpl;
Send_Pulse(pulseLeft);
}
}


IP属地:浙江1楼2013-06-08 16:03回复