#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
//探测头用了12个的TCRT5000,光电管的序号1 2 3 4 5 6 7 8 9 10 11 12
// 对应的端口序号PORTA7 A6A5A4A3A2A1 A0 PORTB3 B2B1 B0
unsigned int light=0; //光电检测
unsigned short direction_turn[]={260,290,340,410,460,510,568,626,704,762,800}; //11个转向给定值初始化
unsigned short direction_run[]={ 32,35,40,50 }; //电机控制速度
unsigned short start_flag=0;//起跑圈数
///////////////////////////////////////////////////////////////////////
unsigned char flag=0;
unsigned char flag1=0; // 鸵机标志位
//////////////////////////////////////////////////////////////////////
//====================时钟初始化===================================
void PLL_Init()
{
REFDV=0x81; /* PLLCLK=2*OSCCLK*(SYNR+1)/(REFDV+1)*/
SYNR=2; /* 锁相环时钟=2*16*(2+1)/(1+1)=48MHZ */
while(!(CRGFLG&0x08)); /* 总线时钟=48/2=24MHZ */
CLKSEL=0x80;
}
/*********************PWN初始化*******************************/
void PWM_Init()
{
PWME=0x00; //关闭PWM使能
PWMPRCLK=0x66; //A,B时钟均为总线的64分频,375KHZ
//PWMSCLA=0x01; //clockSA=clockA/(2*PWMSCLA) = 1500KHZ
//PWMSCLB=0X01; //clockSB=clockB/(2*PWMSCLB) =1500KHZ
PWMCLK=0x00;
PWMPOL=0xFF; //PWM输出起始电平为高电平
PWMCAE=0x00; //输出左对齐
PWMCTL=0xf0; //通道01,23,45,67级联
PWMPER01=7500; //舵机频率为50Hz
PWMDTY01=500; //占空比a=(PWMDTY01+1)/(PWMPER01+1)
PWMPER23=100; //PWM通道3周期为3750HZ
PWMDTY23=40; //占空比a=(PWMDTY01+1)/(PWMPER01+1)
PWME=0xff; //使能pwm
}
/*******************定时与延时函数**************************/
/*void Pit0_Init() //PIT初始化
{
PITCFLMT_PITE=0; //关PIT使能
PITCE_PCE0=1; //通道0使能
PITMUX_PMUX0=0; //通道0接微时钟0
PITMTLD0=99; //微时钟0值设置为7f
PITLD0=3839; //time-out period = (PITMTLD + 1) * (PITLD + 1) / fBUS.
//时间计算 100*3840/24000000=0.016s
PITINTE_PINTE0=1; //通道0中断时能
PITCFLMT_PITE=1; //PIT使能
}
void dly_1ms()
{
int i,j;
for(i=0;i<200;i++)
{
for(j=0;j<1000;j++){;}
}
}
*/
void delay(int zz)
{
int i,j;
for(i=0;i<zz;i++)
for(j=0;j<4000;j++);
}
//=================鸵机转向函数===================//
void turning()
{
flag=PORTA;
flag1= PORTB;
flag1=flag1<<4;
light=((unsigned int)flag<<8)|((unsigned int)flag1);
//==============二分法鸵机中间值语句=====================//
if((light&0x0600)||(light==0xfff0))
PWMDTY01=direction_turn[5];
//===========二分法鸵机左边语句================//
if(light&0xf800)//0xf800
{
if(light&0x3800) //26,28,32,37,40
{
if(light&0x1800)
{
if(light==0x0800)
{
PWMDTY01=direction_turn[4]; //拐弯的角度
PWMDTY23=direction_run[3]; //对应的减速
}
if(light==0x1000)
{
PWMDTY01=direction_turn[3];
PWMDTY23=direction_run[2];
}
}
if(light==0x2000)
{
PWMDTY01=direction_turn[2];
PWMDTY23=direction_run[1];
}
}
if(light==0x4000)
{
PWMDTY01=direction_turn[1];
PWMDTY23=direction_run[0] ;
}
if(light==0x8000)
{
PWMDTY01=direction_turn[0];
PWMDTY23=direction_run[0] ;
}
}
//========二分法鸵机右边函数===============//
if(light&0x01f0)
{
if(light&0x01c0)
{
if(light&0x0180)
{
if(light==0x0100)
{
PWMDTY01=direction_turn[6];
PWMDTY23=direction_run[3];
}
if(light==0x0080)
{
PWMDTY01=direction_turn[7];
PWMDTY23=direction_run[2];
}
}
if(light==0x0040)
{
PWMDTY01=direction_turn[8];
PWMDTY23=direction_run[1] ;
}
}
if(light==0x0020)
{
PWMDTY01=direction_turn[9];
PWMDTY23=direction_run[0] ;
}
if(light==0x0010)
{
PWMDTY01=direction_turn[10];
PWMDTY23=direction_run[0] ;
}
}
}
/**************************鸵机转向函数*********************/
/******************************************************************************/
void main()
{
DisableInterrupts; /* 关中断 */
PORTA=0X00;
PORTB=0X00;
DDRA=0x00;
DDRB=0x00;
DDRE_DDRE3=0; /* PTE5 as INPUT */
DDRE_DDRE2=0; /* PTE6 as INPUT */
PORTE_PE3=1;
PORTE_PE2=1;
PLL_Init();
PWM_Init();
//Pit0_Init();
EnableInterrupts;
for(;;)
{
turning(); //舵机转向函数
}
}