中级会员
- 积分
- 289
- 威望
- 130
- 贡献
- 49
- 兑换币
- 43
- 注册时间
- 2012-10-31
- 在线时间
- 55 小时
- 毕业学校
- 常熟理工
|
#include <hidef.h> /* common defines and macros */
#include <MC9S12XS128.h> /* derivative-specific definitions */
#pragma LINK_INFO DERIVATIVE"mc9s12xs128"
unsigned int SenA;
unsigned int table[7]={1610,1598,1545,1470,1385,1327,1315};
unsigned char AD_LEFT=4,AD_MIDDLE=5,AD_RIGHT=6;
unsigned int i=0,j=0,k=0,p=2.5;
int a=-1,b,c;
unsigned int steer;
unsigned int AD_datamax,AD_datamin,AD_datasum;
unsigned int AD_data[8][10]=0,AD_Value[8]=0;
float speed;
//这个文件定义 主传感器操作函数,包括AD采集,分析等
//只用了3个电感
void deley(unsigned int loop_times)
{
unsigned int loop_i,loop_j;
for (loop_i=0; loop_i< loop_times; loop_i++)
{
for (loop_j=0; loop_j<500;loop_j++)
{
;
}
}
}
void AD_init(void)
{
ATD0CTL1=0x00; //7:1 -外部触发 ,65:10 -12 位精度 ,4:1 采样前放 电,3210:ch
ATD0CTL2=0x40; // 禁止外部触发 , 中断禁止
ATD0CTL3=0x80; //7:1 右对齐无符号 ,6543:0100 每次转换 4个序列 , 210:000,No FIFO, Freeze 模式下继续转
ATD0CTL4=0x03; //765: 采样时间为 4个 AD 时钟周 期,ATDClock=[BusClock*0.5]/[PRS+1]
ATD0CTL5=0x30; //6:0 特殊通道禁止 ,5:1 连续转换 ,4:1 多通道轮流采样
ATD0DIEN=0x00; // 禁止数字输入
}
void PIT_INIT(void)
{
PITCFLMT_PITE=0;
PITCE_PCE0=1; //通道使能
PITMUX=0; //选择8位时钟
PITMTLD0=240-1;
PITLD0=2000-1;
PITINTE_PINTE0=1;//中断使能
PITCFLMT_PITE=1; //PIT使能
}
void sci_init (void)
{
SCI0CR2=0xb8;
SCI0BDH=0x00;
SCI0BDL=0x9c;
}
/*void sci_putchar(unsigned int ch)
{
while(!(SCI0SR1&0x40))
{ }
SCI0DRL=1;
}*/
void PLL_INIT(void)
{
SYNR=2;
REFDV=1;
asm(nop);
asm(nop);
while(!(CRGFLG&0x08));
CLKSEL=0x80;
}
/*void PLL_INIT(void)
{
CLKSEL=0X00; //disengage PLL to system
PLLCTL_PLLON=1; //turn on PLL
SYNR =0xc0 | 0x05;
REFDV=0x80 | 0x01;
POSTDIV=0x00; //pllclock=2*osc*(1+SYNR)/(1+REFDV)=96MHz;
_asm(nop); //BUS CLOCK=48M
_asm(nop);
while(!(CRGFLG_LOCK==1)); //when pll is steady ,then use it;
CLKSEL_PLLSEL =1; //engage PLL to system;
}*/
void PWM_Init(void)
{
PWMCTL_CON01=1;
PWMCAE_CAE1=0;
PWMPOL_PPOL1=1;
PWMCNT01=0;
PWMCLK_PCLK1=1;
PWMSCLA=12;
PWMCLK_PCLK1=1;
PWMPER01=20000;
PWME_PWME1=1;
}
void dianjiPWM_init()//电机PWM初始化
{
PWME_PWME2 = 0;
PWME_PWME3 = 0;
PWMPRCLK = 0;
PWMSCLA = 12;
PWMSCLB = 12;
PWMCLK_PCLK2 = 1;
PWMCLK_PCLK3 = 1;
PWMPOL_PPOL2 = 1;
PWMPOL_PPOL3 = 1;
PWMCAE_CAE2 = 0;
PWMCAE_CAE3 = 0;
PWMPER2 = 100;
PWMPER3 = 100;
PWME_PWME2 = 1;
PWME_PWME3 = 1;
}
void time1Init(void){
PACTL_PAEN=0;//0-16b计数禁止,1允许,先禁止
PACTL_PAMOD=0;//0-计数模式,1-门时计数模式,设0
PACTL_PEDGE=0;//0下降沿,1-上升沿,指定有IOC7-PT7口输入,此位受PACTL_PAMOD影响,此设0
PACTL_CLK1=0;//当计数位外部脉冲,此设置无意义,都设0
PACTL_CLK0=0;
PACTL_PAOVI=0;//0-溢出中断禁止,1-允许,本例计数个数有time0时钟中断读取,不需要溢出中断,设0
PACTL_PAI=0;//0禁止中断,1允许,设0
PACNT=0;//计数器寄存器的值,可以使0000~FFFF,先清0
//PAFLG_PAOVF=1;//计数溢出FFFF>0000
PAFLG_PAIF=1;//脉冲数入1=IOC7-PT7,最大65536个脉冲
PACTL_PAEN=1;//16b计数允许
}
unsigned char ReadATD(byte ch)
{
byte ad=0;
DisableInterrupts;
while(!ATD0STAT0_SCF);
switch(ch)
{
case 0:
ad= ATD0DR0L;
break;
case 1:
ad= ATD0DR1L;
break;
case 2:
ad= ATD0DR2L;
break;
case 3:
ad= ATD0DR3L;
break;
case 4:
ad= ATD0DR4L;
break;
case 5:
ad= ATD0DR5L;
break;
case 6:
ad= ATD0DR6L;
break;
case 7:
ad= ATD0DR7L;
break;
}
EnableInterrupts;
return ad;
}
void control(void)
{
if(PORTB_PB0==1)
{
//Cal_PostitionA();
unsigned int sum;
for(i=0;i<3;i++)
{
if(AD_Value[i]<=20)AD_Value[i]=0; //数字滤掉零飘
if(AD_Value[i]!=0) AD_Value[i]=AD_Value[i]-19; //新加的 11.7.13
}
sum=AD_Value[AD_LEFT]+AD_Value[AD_MIDDLE]+AD_Value[AD_RIGHT];
sum=AD_Value[AD_LEFT]*5+AD_Value[AD_MIDDLE]*50+AD_Value[AD_RIGHT]*95;//最核心的2句哦
sum=sum/(AD_Value[AD_LEFT]+AD_Value[AD_MIDDLE]+AD_Value[AD_RIGHT]);
SenA=sum;
if (SenA<10) PWMDTY01=table[0];
else if (SenA<25) PWMDTY01=table[1];
else if (SenA<43) PWMDTY01=table[2];
else if (SenA<58) PWMDTY01=table[3];
else if (SenA<75) PWMDTY01=table[4];
else if (SenA<90) PWMDTY01=table[5];
else PWMDTY01=table[6];
}
else
{
if (PORTA_PA0==1) {PWMDTY2 = 45;}
if (PORTA_PA1==1) {PWMDTY2 = 50;}
if (PORTA_PA2==1) {PWMDTY2 = 55;}
if (PORTA_PA3==1) {PWMDTY2 = 60;}
/*if (PORTA_PA0==1) {p=3ORTB=(char)ATD0DR0;}
if (PORTA_PA1==1) {p=4;PORTB=(char)ATD0DR1;}
if (PORTA_PA2==1) {p=5;PORTB=(char)ATD0DR2;}
if (PORTA_PA3==1) {p=6;PORTB=(char)ATD0DR3;}*/
a=AD_Value[1]-AD_Value[2];
b=AD_Value[0]-AD_Value[3];
c=AD_Value[4]-AD_Value[6];
if (!((AD_Value[1]==0)&(AD_Value[2]==0)))
{
if ((a<=5)&(a>=-5)) steer=table[3];
else if (a<-5) steer=table[3]+a*p;
else if (a>5) steer=table[3]+a*p;
}
else
{
if (!((AD_Value[0]==0)&(AD_Value[3]==0)))
{
if(b>0) steer=table[0];
else steer=table[6];
}
else
{
//Cal_PostitionA();
unsigned int sum;
for(i=0;i<3;i++)
{
if(AD_Value[i]<=20)AD_Value[i]=0; //数字滤掉零飘
if(AD_Value[i]!=0) AD_Value[i]=AD_Value[i]-19; //新加的 11.7.13
}
sum=AD_Value[AD_LEFT]+AD_Value[AD_MIDDLE]+AD_Value[AD_RIGHT];
sum=AD_Value[AD_LEFT]*5+AD_Value[AD_MIDDLE]*50+AD_Value[AD_RIGHT]*95;//最核心的2句哦
sum=sum/(AD_Value[AD_LEFT]+AD_Value[AD_MIDDLE]+AD_Value[AD_RIGHT]);
SenA=sum;
if (SenA<10) PWMDTY01=table[0];
else if (SenA<25) PWMDTY01=table[1];
else if (SenA<43) PWMDTY01=table[2];
else if (SenA<58) PWMDTY01=table[3];
else if (SenA<75) PWMDTY01=table[4];
else if (SenA<90) PWMDTY01=table[5];
else PWMDTY01=table[6];
}
}
if ((steer>=1290)&(steer<=1650)==1) PWMDTY01=steer;
else if (steer<1290) PWMDTY01=table[6];
else PWMDTY01=table[0];
}
}
void IO_init(void)
{
PORTB=0x00;
PORTA=0x00;
DDRB=0x00;
DDRA=0x00;
PORTE=0xff;
DDRE=0xff;
}
void boot(void)
{
PLL_INIT();
dianjiPWM_init();
PWM_Init();
PIT_INIT();
//sci_init ();
AD_init();
time1Init();
IO_init();
}
void main(void)
{
boot();
for(j=0;j<8;j++)
{
for(k=0;k<10;k++)
{AD_data[j][k]=ReadATD(j);}
}
PWMDTY2 = 50;
PWMDTY3 = 0;
EnableInterrupts;
for (;;)
{
if (PORTA_PA0==1) {PWMDTY2=45;}
if (PORTA_PA1==1) {PWMDTY2=50;}
if (PORTA_PA2==1) {PWMDTY2=55;}
if (PORTA_PA3==1) {PWMDTY2=60;}
for(j=0;j<8;j++)
{
for(k=0;k<9;k++)
{
AD_data[j][k]=AD_data[j][k+1];
}
}
for(j=0;j<8;j++)
{
AD_data[j][9]=ReadATD(j);
}
for(j=0;j<8;j++)
{
AD_datamax=0;
AD_datamin=255;
AD_datasum=0;
for(k=0;k<10;k++)
{
AD_datasum=AD_datasum+AD_data[j][k];
if(AD_data[j][k]>AD_datamax) AD_datamax=AD_data[j][k];
if(AD_data[j][k]<AD_datamin) AD_datamin=AD_data[j][k];
}
AD_datasum=AD_datasum-AD_datamax-AD_datamin;
AD_Value[j]=AD_datasum/8;
}
control();
/*while(!(SCI0SR1&0x40));
if (PORTB_PB0==1) SCI0DRL=AD_Value[AD_LEFT];
else if (PORTB_PB1==1) SCI0DRL=AD_Value[1];
else if (PORTB_PB2==1) SCI0DRL=AD_Value[2];
PACNT=0;
ATD0STAT0_SCF=1;*/
_FEED_COP();
}
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 66 PIT0(void)
{
PACTL_PAEN=0;//16b计数禁止
PORTB=PACNT*50/80;
speed=(float)PACNT*50/40;
PACNT=0;
PACTL_PAEN=1;//16b计数允许
PITTF_PTF0=1;//清除PIT0的标记
}
我明明把PWM01设成50hz,但用示波器测得频率为9khz-10khz。可是我用我的另一个程序就可以,两个程序对PWM的设置是完全一样
|
|