MM32F3273G8P火龙果开发板MindSDK开发教程15 - 获取msa311加速器的方向改变事件
1、功能描述
类似手机里横屏竖屏检测,当方向发生变化时,横屏竖屏自动切换。
当msa311方向改变时,会产生中断,然后从寄存器Reg 0x0C(Orientation _Status) 中读取现在设备的方位。
有这几种可能,
1、 orientation value of z axis, 0:upward looking
orientation value of x/y axes
00: portrait upright
01: portrait upside down
10: landscape left,
11: landscape right
2、orientation value of z axis, 1:downward looking
orientation value of x/y axes
00: portrait upright
01: portrait upside down
10: landscape left,
11: landscape right
2、寄存器设置
这个寄存器可以设置orient功能的一些特性,但是我没有设置,保持默认值。
当中断产生后,读取09寄存器,bit6 一直都是0,所以程序里没有判断bit6的状态。
中断产生后,读取0x0C寄存器的值,从来判断模块的具体方位。
3、中断处理函数
void handle_int_message(void)
{
uint8_t state;
uint8_t orient_state;
bool bRet;
bRet = Msa311_ReadReg(MSA311_REG_MOTIONINT,&state);
bRet = Msa311_ReadReg(MSA311_REG_ORIENTATION_STATUS,&orient_state);
if (bRet == false)
{
perror("Msa311_GetInterrupt1Enable read error\n");
return;
}
if (state & (1 << MSA311_S_TAP_INT_STATE))
{
printf("this is s_tap \r\n");
}
else if (state & (1 << MSA311_D_TAP_INT_STATE))
{
printf("this is d_tap \r\n");
}
else if (state & (1<< MSA311_ACTIVE_INT_STATE))
{
printf("this is active\r\n");
}
if (orient_state & 1<< 6)
{
printf("downward looking ");
if ((orient_state & 0x30)>>4 == 0)
{
printf(" portrait upright \r\n");
}
else if ((orient_state & 0x30)>>4 == 1)
{
printf(" portrait upside down \r\n");
}
else if ((orient_state & 0x30 ) >> 4 == 2)
{
printf(" landscape left \r\n") ;
}
else if ((orient_state & 0x30 ) >> 4 == 3)
{
printf(" landscape right \r\n");
}
}
else
{
printf(" upward looking ");
if ((orient_state & 0x30 ) >> 4 == 0)
{
printf(" portrait upright \r\n");
}
else if ((orient_state & 0x30)>>4 == 1)
{
printf(" portrait upside down \r\n");
}
else if ((orient_state & 0x30 ) >> 4 == 2)
{
printf(" landscape left \r\n") ;
}
else if ((orient_state & 0x30 ) >> 4 == 3)
{
printf(" landscape right \r\n");
}
}
}
4、现象
当改变msa311方位时,会打印出msa311相对于人眼视角的方位角度打印。
5、代码
代码下载