中级会员
- 积分
- 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[5]={1598,1565,1465,1365,1332};
unsigned char AD_LEFT=0,AD_MIDDLE=1,AD_RIGHT=2;
unsigned int i=0;
#define PITTIME 5000;
unsigned int vTmpPIT=0;
//这个文件定义 主传感器操作函数,包括AD采集,分析等
//只用了3个电感
unsigned int AD_Value[16];//AD转换 结果缓存
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_PMUX0=0; //选择8位时钟
PITMTLD0=240-1;
PITLD0=PITTIME-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 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计数允许
}
void AD_GetValue() //获取AD值并存入缓存区
{
while(ATD0STAT2_CCF0==0); // 等待转换结束while(ATDOSTAT2_CCF0==0)
AD_Value[0]=ATD0DR0; //读取结果寄存器的值
AD_Value[1]=ATD0DR1;
AD_Value[2]=ATD0DR2;
/*AD_Value[3]=ATD0DR3;
AD_Value[4]=ATD0DR4;
AD_Value[5]=ATD0DR5;
AD_Value[6]=ATD0DR6;
AD_Value[7]=ATD0DR7;*/
}
uchar Cal_PostitionA()
{
unsigned char i;
unsigned int sum;
for(i=0;i<8;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];
if(sum!=0)
{
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;
return 0;
} else return 1;
}
void IO_init(void)
{
DDRB=0xff;
PORTB=0xff;
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 66 PIT0(void)
{
vTmpPIT++;
if(vTmpPIT==10)
{
PORTB=~PORTB;//输出取反
vTmpPIT=0;
}
PITTF_PTF0=1;//清中断标志位
}
/*{
PACTL_PAEN=0;
//while(!(SCI0SR1&0x40));
SCI0DRL=PACNT;
PACNT=0;
PACTL_PAEN=1;//16b计数允许
PITTF_PTF0=1;//清中断标志位
}*/
void boot(void)
{
sci_init ();
}
void main(void)
{
//boot();
PLL_INIT();
IO_init();
PWM_Init();
time1Init();
dianjiPWM_init();
PIT_INIT();
AD_init();
PWMDTY2 = 45;
PWMDTY3 = 0;
EnableInterrupts;
for (;;)
{
//AD_GetValue();
//Cal_PostitionA();
if (SenA<25) PWMDTY01=table[0];
else if(SenA<45) PWMDTY01=table[1];
else if (SenA<55) PWMDTY01=table[2];
else if (SenA<75) PWMDTY01=table[3];
else PWMDTY01=table[4];
ATD0STAT0_SCF=1;
}
_FEED_COP();
}
|
|