高级会员
- 积分
- 516
- 威望
- 361
- 贡献
- 83
- 兑换币
- 0
- 注册时间
- 2012-2-13
- 在线时间
- 36 小时
- 毕业学校
- 广西大学
|
附个代码出来,哪位高手可以指点下,我实在搞不懂了~~·
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define Tg 0.004 //同样的算法,但是跟官网的差别很大(时间补偿常数)??
#define jsdq_ratio 0.57
#define jiaodu_speed_ratio 1.1
#define jadq_offset 290
#define jiaodu_speed_offset 340
float jsdq=0.0,jadq_a=0.0,jiaosu_speed=0.0,jiaodu0=0.0,jiaodu=0.0,jiaosu_speed_a=0.0;
//bus clock
void SetBusCLK_nM(byte nM)
{
MMCCTL1=0X00;
PKGCR =0X06;
DIRECT =0x00;
IVBR =0xFF;
ECLKCTL=0xC0;
CPMUPROT =0x26; //停止保护时钟配置寄存器
CPMUCLKS_PSTP=0; //
CPMUCLKS_PLLSEL=1; //应用PLL
//CPMUSYNR =nM-1; //设置分频因子
CPMUSYNR =0x40|(nM-1); //设置分频因子
CPMUREFDIV =0x80|0x00; //pllclock=2*(1+SYNR)= MHz;
CPMUPOSTDIV=0x00; // Set the post divider register
CPMUPOSTDIV=0x00; // Set the post divider register
CPMUPLL =0x10; // Set the PLL frequency modulation
while(CPMUFLG_LOCK == 0); /* Wait until the PLL is within the desired tolerance of the target frequency */
CPMUPROT=0x00; /* Enable protection of clock configuration registers */
}
void Dly_ms(int ms)
{
int ii,jj;
if (ms<1) ms=1;
for(ii=0;ii<ms;ii++)
for(jj=0;jj<2670;jj++); //busclk:16MHz--1ms
}
static void Port_Init(void) //灯低电平亮
{
DDRD = 0xff; //LED PTB0--7,
// PORTD= 0xff; //LEDs off
PORTD= 0xf0; //LEDs on
}
void AD_Init()//4路,陀螺仪、加速度各一路,两路备用
{ ATDCTL1=0x20;//10位
//ATDCTL1=0x40; //12位
//ATDCTL1=0x00; //7:1-外部触发,65:00-8位精度,4:放电,3210:ch
ATDCTL2=0x40; //禁止外部触发, 中断禁止
ATDCTL3=0x80; //右对齐无符号,每次转换8个序列, No FIFO, Freeze模式下继续转
ATDCTL4=0x01; //765:采样时间为4个AD时钟周期,ATDClock=[BusClock*0.5]/[PRS+1]
ATDCTL5=0x30; //6:0特殊通道禁止,5:1连续转换 ,4:1多通道轮流采样
ATDDIEN=0x00; //禁止数字输入
//Led2=0;//led2
}
void RTI_Init(void)//中断初始化,1ms定时中断
{
//CPMURTI=0Xff; //3.2M/16M=0.5s
CPMURTI=0X8F;//7:1,654:000 16k ,16k/16M=0.001s=1ms
CPMUINT=0X80;//中断允许
}
void Get_AD_average() {
float acceleration[20];
float gyroscope[20];
int m;
float acceleration_sum=0,gyroscope_sum=0;
for(m=0;m<20;m++) {
acceleration[m]= ATDDR1;
acceleration_sum+=acceleration[m];
gyroscope[m]=ATDDR0;
gyroscope_sum+=gyroscope[m] ;
}
jadq_a= acceleration_sum/20;
jiaosu_speed_a=gyroscope_sum/20;
//Led3=0;
}
void Calculate_angle(){
float xiuzheng=0.0f;
jsdq=(jadq_a-jadq_offset)*jsdq_ratio; //jsdq=加速度器 jadq_a为20次采样平均值 _offset是初始值
jiaosu_speed=(jiaosu_speed_a-jiaodu_speed_offset)*jiaodu_speed_ratio;//jiaodu为陀螺仪的信号
//jiaosu_speed_a:陀螺仪平均值
//jiaodu_speed_offset 初始值
jiaodu0=jiaodu;
xiuzheng=(jsdq-jiaodu0)/Tg;
jiaodu+=(jiaodu0+xiuzheng)/200;
}
void main(void) {
/* put your own code here */
SetBusCLK_nM(48) ;
Port_Init();
AD_Init();
RTI_Init();
EnableInterrupts;
PORTD= 0x0f;
for(;;) {
_FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 7 RTI(void){
static int i=0;
i++;
CPMUFLG_RTIF=1;//RTI time-out has occurred清中断标志位
if(i==1){
Get_AD_average();//AD采集、滤波
Calculate_angle();
//Angle_Control();
}
else if(i==5){
//MotorOutput();// Motor out
i=0ORTD= 0x00;
}
//Led6=0;
}
|
|