|
DATA_FORMAT寄存器我设置的是0x2B,即满分辨率,三轴的偏置都都是0,范围正负16g,右对齐,X轴和Y轴静止时几乎是-50左右,可是Z轴怎么读到的都是正的,1090左右,翻转180后读到的是512,按理应该是250左右才对吧?请问这是什么情况? 右对齐的话如果是负数,由补码求原码是-(8192-x)吗?
改来改去已经该晕了,大家还是直接看代码吧,有什么地方不对的请指教,非常感谢!我用的是IIC读的数据,定时器2ms读取一次
void Accelerate_Init() //加速度初始化
{
u8 Device_Check_ID;
IIC_WriteReg(ACCELERATE_ADDR,DATA_FORMAT,0x2B); //满分辨率,范围正负16g,右对齐
IIC_WriteReg(ACCELERATE_ADDR,BW_RATE,0x0C); //输出速率400HZ,带宽200HZ
IIC_WriteReg(ACCELERATE_ADDR,OFSX,0x00); //偏置为0
IIC_WriteReg(ACCELERATE_ADDR,OFSY,0x00); //偏置为0
IIC_WriteReg(ACCELERATE_ADDR,OFSZ,0x00); //偏置为0
IIC_WriteReg(ACCELERATE_ADDR,ACCE_FIFO_CTL,0x9F);
IIC_WriteReg(ACCELERATE_ADDR,POWER_CTL,0x08);
IIC_WriteReg(ACCELERATE_ADDR,INT_ENABLE,0x80);
Device_Check_ID = CheckAcce(); //检查加速度传感器,是否正确
if(Device_Check_ID==0xE5)
{
printf("ACC_OK ");
}
else printf("ACC_ERROR ");
Acc_Correct(); //得到初始值
}
u8 Read_Accelerate() //读取加速度传感器
{
int AcceData[6];
u8 status = 0;
GetAcceFIFOStatus(&status);
if(status&0x3f)
{
IIC_ReadAccData(ACCELERATE_ADDR,0x32,AcceData,6);
AccRawData.x = (int) ( AcceData[1]<<8|AcceData[0]);
AccRawData.y = (int) ( AcceData[3]<<8|AcceData[2]);
AccRawData.z = (int) ( AcceData[5]<<8|AcceData[4]);
// printf("B %d, %d ",AcceData[5],AcceData[4]);
if(AccRawData.x&0x1000) //补码变原码
{
AccRawData.x = -(8192-AccRawData.x);
}
if(AccRawData.y&0x1000) //补码变原码
{
AccRawData.y = -(8192-AccRawData.y);
}
if(AccRawData.z&0x1000) //补码变原码
{
AccRawData.z = -(8192-AccRawData.z);
}
return 1;
}
return 0;
}
void CalAccAngle(void) //由加速度计算角度
{
if(Read_Accelerate())
{
// AccRawData.x-=AccBase.x;
// AccRawData.y-=AccBase.y;
// AccRawData.z-=AccBase.z;
/*
AngleAcc.x=-asin(AccRawData.x/310.0)*90/1.14;
AngleAcc.y=asin(AccRawData.y/310.0)*90/1.14;
AngleAcc.z=-AccRawData.z*90.0/320;
*/
AngleAcc_tmpx[m]=-asin(AccRawData.x/310.0)*90/1.14;
AngleAcc_tmpy[m]=asin(AccRawData.y/310.0)*90/1.14;
m++;
if(m==NumAcc)
{
m=0;
Acc_Filter(AngleAcc_tmpx,AngleAcc_tmpy);
AngleAccLast.x=AngleAcc.x;
AngleAccLast.y=AngleAcc.y;
}
else
{
AngleAcc.x=AngleAccLast.x;
AngleAcc.y=AngleAccLast.y;
}
printf("B %d, %d, %d ",AccRawData.x,AccRawData.y,AccRawData.z);
// printf("B %.3f, %.3f, ",AngleAcc.x,AngleAcc.y);
}
}
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|