MM32F3273G8P火龙果开发板MindSDK开发教程13 - 获取msa311加速器的运动事件

MM32F3273G8P火龙果开发板MindSDK开发教程13 - 获取msa311加速器的运动事件

1、功能描述

msa311在运动时候,会产生一个中断,此时我们读一下09寄存器的02位,如果为1,那么我们可以断定msa311处在运动状态,但是当msa311处于静止的时候,却没有中断产生。而adxl345在静止的时候,也会在设定时间之内,产生中断,从来更好的判断运动与静止状态。

2、设置活动检测参数

相关寄存器
MM32F3273G8P火龙果开发板MindSDK开发教程13 - 获取msa311加速器的运动事件_第1张图片
active_th 活动检测的阈值,超过这个阈值就会产生中断。
注意这里比例因子随着range的范围变化而变化。
假如2G Range ,1g对应FF,即1/0.00391 = 0xFF。
active_dur 超过阈值持续时间,必须超过active+1ms才会产生中断。

设置寄存器代码:

static bool Msa311_SetActiveDurationParam(msa311_active_duration_t activeDuration)
{
    uint8_t r_data;
	uint8_t w_data;
	bool bRet = 0;

	bRet = Msa311_ReadReg(MSA311_REG_ACTIVE_DURATION,&r_data);
	if (bRet == false)
	{
		perror("Msa311_SetActiveDurationParam read error\n");
		return false;
	}
 	w_data = ((r_data & MSA311_ACTIVE_DURATION_MASK) | activeDuration);
	bRet = Msa311_WirteReg(MSA311_REG_ACTIVE_DURATION,w_data);
	if (bRet == false)
	{
		perror("Msa311_SetActiveDurationParam write error\n");
		return false;
	}

    return bRet;
}

static bool Msa311_GetActiveDurationParam(msa311_active_duration_t *activeDuration)
{    
    uint8_t r_data;
	bool bRet = 0;

    bRet = Msa311_ReadReg(MSA311_REG_ACTIVE_DURATION,&r_data);
	if (bRet == false)
	{
		perror("Msa311_GetActiveDurationParam read error\n");
		return false;
	}
    *activeDuration = (r_data & ~(MSA311_ACTIVE_DURATION_MASK));
    
    return bRet;
}


static uint8_t Msa311_ActiveThresholdConvertToRegister(float g, uint8_t *reg)
{
    msa311_range_t range;
    Msa311_GetRange(&range);
    switch (range)
    {
    case MSA311_RANGE_2_G/* constant-expression */:
        /* code */
        *reg = (int8_t)(g / 0.00391f);    
        break;
    case MSA311_RANGE_4_G/* constant-expression */:
        /* code */
        *reg = (int8_t)(g / 0.00781f);    
        break;
    case MSA311_RANGE_8_G/* constant-expression */:
        /* code */
        *reg = (int8_t)(g / 0.015625f);    
        break;
    case MSA311_RANGE_16_G/* constant-expression */:
        /* code */
        *reg = (int8_t)(g / 0.03125f);    
        break;        
    default:
        printf("Msa311_ActiveThresholdConvertToRegister error\r\n");
        break;
    }
    return 0;                                                                             
}

static uint8_t Msa311_ActiveThresholdConvertToData(uint8_t reg, float *g)
{
    msa311_range_t range;
    Msa311_GetRange(&range);
    switch (range)
    {
    case MSA311_RANGE_2_G/* constant-expression */:
        /* code */
        *g = (float)(reg) * 0.00391f;   
        break;
    case MSA311_RANGE_4_G/* constant-expression */:
        /* code */
        *g = (float)(reg) * 0.00781f;     
        break;
    case MSA311_RANGE_8_G/* constant-expression */:
        /* code */
        *g = (float)(reg) * 0.015625f;     
        break;
    case MSA311_RANGE_16_G/* constant-expression */:
        /* code */
        *g = (float)(reg) * 0.03125f;    
        break;        
    default:
        printf("Msa311_ActiveThresholdConvertToData error\r\n");
        break;
    }
    
    *g = (float)(reg) * 0.0039f;    
    return 0;
}

static bool Msa311_SetActiveThresholdParam(uint8_t threshold)
{
    uint8_t r_data;
	uint8_t w_data;
	bool bRet = 0;

 	w_data = threshold;
	bRet = Msa311_WirteReg(MSA311_REG_ACTIVE_THRESHOLD,w_data);
	if (bRet == false)
	{
		perror("Msa311_SetActiveThresholdParam write error\n");
		return false;
	}

    return bRet;
}

static bool Msa311_GetActiveThresholdParam(uint8_t *threshold)
{    
    uint8_t r_data;
	bool bRet = 0;

    bRet = Msa311_ReadReg(MSA311_REG_ACTIVE_THRESHOLD,&r_data);
	if (bRet == false)
	{
		perror("Msa311_GetActiveThresholdParam read error\n");
		return false;
	}
    *threshold = r_data;
    
    return bRet;
}

3、映射与使能中断

MM32F3273G8P火龙果开发板MindSDK开发教程13 - 获取msa311加速器的运动事件_第2张图片
使能bit2,将中断映射到int管脚
MM32F3273G8P火龙果开发板MindSDK开发教程13 - 获取msa311加速器的运动事件_第3张图片
使能bit0,bit1,bit2,将输出x y z产生的活动中断。
相关代码:

// 映射活动中断
    Msa311_SetInterruptMap0(MSA311_INTERRUPT_ACTIVE,MSA311_BOOL_TRUE);
    Msa311_GetInterruptMap0(MSA311_INTERRUPT_ACTIVE,&enable);
    printf("Msa311_GetInterruptMap0 MSA311_INTERRUPT_ACTIVE %s\r\n",enable==MSA311_BOOL_TRUE ? "enable" : "disable");

    Msa311_SetInterrupt0Enable(MSA311_INTERRUPT_ACTIVE_EN_X,MSA311_BOOL_TRUE);
    Msa311_GetInterrupt0Enable(MSA311_INTERRUPT_ACTIVE_EN_X,&enable);
    printf("Msa311_GetInterrupt0Enable MSA311_INTERRUPT_ACTIVE_EN_X %s\r\n",enable==MSA311_BOOL_TRUE ? "enable" : "disable");

    Msa311_SetInterrupt0Enable(MSA311_INTERRUPT_ACTIVE_EN_Y,MSA311_BOOL_TRUE);
    Msa311_GetInterrupt0Enable(MSA311_INTERRUPT_ACTIVE_EN_Y,&enable);
    printf("Msa311_GetInterrupt0Enable MSA311_INTERRUPT_ACTIVE_EN_Y %s\r\n",enable==MSA311_BOOL_TRUE ? "enable" : "disable");

    Msa311_SetInterrupt0Enable(MSA311_INTERRUPT_ACTIVE_EN_Z,MSA311_BOOL_TRUE);
    Msa311_GetInterrupt0Enable(MSA311_INTERRUPT_ACTIVE_EN_Z,&enable);
    printf("Msa311_GetInterrupt0Enable MSA311_INTERRUPT_ACTIVE_EN_Z %s\r\n",enable==MSA311_BOOL_TRUE ? "enable" : "disable");

4、中断处理

MM32F3273G8P火龙果开发板MindSDK开发教程13 - 获取msa311加速器的运动事件_第4张图片
中断处理函数中读取bit2是否为1,为1,则活动,为0,则不活动。

void handle_int_message(void)
{
    uint8_t state;
    bool bRet;
    bRet = Msa311_ReadReg(MSA311_REG_MOTIONINT,&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");
    }
}

5、现象

摇晃msa311,可见如下打印:
MM32F3273G8P火龙果开发板MindSDK开发教程13 - 获取msa311加速器的运动事件_第5张图片

6、代码

代码下载

你可能感兴趣的:(火龙果MM32F3273G8P,stm32,mm32,嵌入式硬件,单片机)