#include <hidef.h>
#include <MC9S12XS128.h>
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
#define uchar unsigned char
#define uint unsigned int
uchar flag1,flag2,flag3; //flag bit
uint result,temp,num,left,middle,right,tt;
uchar speed[]={100,90,80,70,60,50,40,30,20,10}; //speed degression
uint direction[]={ 933,932,931,930,929,928,927,925,923,922,921,920,919,918,917};//angle eudipleural
void delay(uint k) //delay k ms
{
uint i,j;
for(i=k;i>0;i--)
for(j=9650;j>0;j--);
}
void SetBusCLK_32M(void)
{
CLKSEL=0x00; //disengage PLL to system
PLLCTL_PLLON=1; //turn on PLL(打开锁相环电路)
SYNR =0xc0 | 0x03;
REFDV=0xc0 | 0x01;
POSTDIV=0x00; //pllclock=2*osc*(1+SYNR)/(1+REFDV)=80MHz;
_asm(nop); //BUS CLOCK=40M
_asm(nop);
while(!(CRGFLG_LOCK==1)); //when pll is steady ,then use it;
CLKSEL_PLLSEL =1; //engage PLL to system;
}
void IO_init()
{
DDRT=0x50;
DDRB=0xff;
DDRP=0xff;
DDRA=0xff; //PORTA set as ouput
DDRJ=0x00; // PORTJ7 set as input
PPSJ=0xff; // set PORTJ7 as rising edge
PIEJ=0xff; //enable PORTJ7 interrupt
PORTA=0xff;
PTT=0x40;
}
void PIT_init() //周期中断定时器
{
PITCFLMT=0x80; //PIT module is enabled
PITFLT=0x00;
PITCE=0x01; //The corresponding PIT channel is enabled.
PITMUX=0x00; //The corresponding 16-bit timer counts with micro time base 0.
PITINTE=0x01; //Interrupt of the corresponding PIT channel is enabled.
PITMTLD0=199;
PITLD0=3199; // 20 ms
/*Whenever a 16-bit timer counter and the connected 8-bit micro timer counter have counted to zero, the
PITLD register is reloaded and the corresponding time-out flag PTF in the PIT time-out flag (PITTF)
register is set, as shown in Figure 12-20. The time-out period is a function of the timer load (PITLD) and
micro timer load (PITMTLD) registers and the bus clock fBUS:
time-out period = (PITMTLD + 1) * (PITLD + 1) / fBUS.*/
}
void ETC_init()
{
TIOS=0x0d; //channel 1 set as inputCapture
TIE=0x02; //PT1 ouput interrupt enable
TSCR1=0x80; // timer function normally,all mode continue work
TCTL4=0x08; //Capture on falling edges only
}
void AD_init()
{
ATD0CTL1=0x40; // 12-bit resolution conversion(12位精度选择)
ATD0CTL2=0x40; //prohibit the external triggerclear the flag naturally(ATD转换CCF快速清零位)
ATD0CTL3=0xc0; //right justified result data.,conversion 8 channes(结果寄存器数据右对齐,一个转换序列由8个ATD转换次数)
ATD0CTL4=0x01; //4 ATD conversion clock cycles,conversion cycle is 1/2us ATDClock=8MHz
ATD0CTL5=0x30; //多通道转换
ATD0DIEN=0x00; //缓冲区无效,禁止输入
}
void PWM_init()
{
PWME=0x88; //6、7 通道级联,3、2通道级联
PWMPOL=0x88; //先输出高电平后输出低电平
PWMCLK=0x80; //6、7选择SB作为时钟源,2、3选择B作为时钟源
PWMPRCLK=0x40; //B clock=1/16 BUS Clock ,2、3 chanel's frequency is 2 MHz
PWMCAE=0x00; //二者都选择左对齐显示波形
PWMCTL=0xa0; // 级联方式使能
PWMSCLB=20; //Clock SB = Clock B / (2 * PWMSCLB)设置为 50K HZ ,6、7 chanel --50K Hz
PWMPER67=1000; //PWMxPeriod=(channelclockperiod)*(PWMPERx) 通道周期=(1/50k)*1000)=20ms
PWMPER23=200; // Period=(1/2M)*200= 100us ,PWM frequency is 10k
//PWMDTY67=925; //占空比7.5%
// PWMDTY23=10; //占空比 10%
}
uint Mult_covert(uchar num)
{
uint result;
while(!ATD0STAT0_SCF); //等待当前队列转换完成
ATD0STAT0_SCF=1; //清除队列完成标志
switch(num)
{
case 0:
{
result=ATD0DR0;
} break;
case 1:
{
result=ATD0DR1;
} break;
case 2:
{
result=ATD0DR2;
} break;
case 3:
{
result=ATD0DR3;
} break;
case 4:
{
result=ATD0DR4;
} break;
case 5:
{
result=ATD0DR5;
} break;
case 6:
{
result=ATD0DR6;
} break;
case 7:
{
result=ATD0DR7;
} break;
default: break;
}
return result;
}
void Ctrl_pwm(uchar num1,uchar num2) //6、7 chanel is 0.1*num% ,2、3 chanel is num2%
{
PWMDTY67=direction[num1];
PWMDTY23=speed[num2];
}
void detect_speed()
{
if(tt>=50) //time is 1s
{
tt=0;
temp=num;
num=0;
}
}
void signal_Ctrl() //signal capture and dispose
{
middle=Mult_covert(5);
right=Mult_covert(1);
left=Mult_covert(7);
if((2000<middle)||(middle+left+right>5600))//Only middle can be detected
{
flag1=1;
Ctrl_pwm(7,7);
}
else if((1500<right)) //just right sensor capture the signal
{
flag2=1;
Ctrl_pwm(4,7); //turn right 21 degree
}
else if(1500<left)
{
flag2=1;
Ctrl_pwm(10,7); //turn left 21 degree
}
else if((600<left)&&((middle+right)<300)) //just left sensor capture the signal
{
flag3=1;
Ctrl_pwm(14,7); //turn left 35 degree
}
else if((600<right)&&((middle+left)<300))
{
flag3=1;
Ctrl_pwm(0,7); //turn right 35 degree
}
}
void main(void)
{
EnableInterrupts;
SetBusCLK_32M();
AD_init();
IO_init();
PWM_init();
PIT_init();
ETC_init();
delay(1500);
PWMDTY67=direction[7]; //center location
for(;;)
{
signal_Ctrl();
detect_speed();
}
}
#pragma CODE_SEG NON_BANKED //输入捕捉用作测速
void interrupt 9 IT1 (void)
{
TFLG1=0x02;
num++;
PORTA=0xaa;
}
#pragma CODE_SEG NON_BANKED //用作定时
void interrupt 66 PIT0(void)
{
PITTF=0x01;
tt++;
}
dianci.rar_电磁组算法_飞思卡尔 DG128
版权申诉
56 浏览量
2022-09-23
16:52:53
上传
评论
收藏 3KB RAR 举报
JaniceLu
- 粉丝: 79
- 资源: 1万+