飞思卡尔智能汽车比赛程序
0赞
#include <hidef.h> /* common defines and macros */
#include "derivative.h"
#define Jx 15000
//-----时钟初始化程序--------//
void PLL_Init(void)
{
REFDV=3; //PLLCLK=2*OSCCLK*(SYNR+1)/(REFDV+1)
SYNR=7; //锁相环时钟=2*16*(7+1)/(3+1)=64MHz
while(!(CRGFLG&0x08));//busclock=64/2=32MHz
CLKSEL=0x80; //选定锁相环时钟
}
void IO_Init(void) //IO
{
DDRB =0xff;
PORTB=0xff; //B口置高电平,给D1或者D2输出高电平,用以控制33886状态。
}
//-----PWM初始化程序------//
void PWM_Init(void)
{
PWME = 0x00;
PWMCTL_CON01=1;
PWMPRCLK = 0x22; // clockA=clockB=busclock/4=32/4=8MHz
PWMSCLA = 0x04; //clockSA=clockA/(2*PWMSCLA)=8/8=1MHz
PWMSCLB = 0x04; //clockSA=clockA/(2*PWMSCLA)=8/8=1MHz
PWMCLK = 0xff; //时钟源选择SA,SB
PWMPOL = 0xff; //正极性脉冲
PWMCAE = 0x00; // 左对齐模式
PWMPER01 = 20000;//舵机计数周期寄存器(20ms)
PWMDTY01 = 1550; //输出高电平时间1.4ms
PWMDTY2 = 38; //
PWME= 0x07; //输出PWM
}
{
PLL_Init();
IO_Init();
PWM_Init();
EnableInterrupts;
for(;;)
{
AD_GetValue();
//在中间
if( (A4>Jx) && (A5>Jx) ) //如果中间的两个传感器同时检测到黑线,认为是中间。给舵机一个PWM,
{
PWMDTY01=1550; //轮子正直,PWM为1550,(因车而异)
}
if( (A4>Jx) && (A5<Jx) ) //中间稍左边的检测到,而它的两边都没检测到,状态为稍左,
{
if(A3<Jx) PWMDTY01=1500; //同上,不做解释。。。
}
if( A3>Jx)
{
PWMDTY01=1450;
}
if(A2>Jx )
{
PWMDTY01=1400;
}
if( A1>Jx )
{
PWMDTY01=1300;
}
if( (A4<Jx) && (A5>Jx) )
{
if(A6<Jx) PWMDTY01=1600; ;
}
if( A6>Jx )
{
PWMDTY01=1650;
}
if( A7>Jx )
{
PWMDTY01=1700;
}
if( A8>Jx )
{
PWMDTY01=1800;
}
_FEED_COP();
}
}
