MMA8452Q的倾斜中断使用

由于项目低功耗需要,使用了MMA8452Q来检测物体倾斜,当倾斜达到一定角度时输出一个脉冲信号以此来唤醒MSP430。按照AN4068手册上的例程配置,结果一直出不来,后来仔细检测发现是由于自己粗心没有仔细看全资料,特此记录此坑。

实质上需要MMA8452Q的倾斜中断持续输出,除了正确配置相关寄存器外,还需要在每产生一次中断脉冲后读取一个特定寄存器以此来清除中断标志。这个特定寄存器就是PL_STATUS(0x10),如果没有读这个寄存器,往往产生一次中断脉冲后就不会继续产生了,经常让人产生错觉得是寄存器哪里没配置对~~。

如下是MMA8452数据手册部分截图

 

寄存器配置参考《AN4068》:

4.1Example Steps for Implementing the Embedded Orientation Detection
Step 1: Put the part into Standby Mode
CTRL_REG1_Data = IIC_RegRead(0x2A); //read contents of register
CTRL_REG1_Data& = 0xFE; //Set last bit to 0.
IIC_RegWrite(0x2A, CTRL_REG1_Data); //write the updated value in CTRL_REG1
Step 2: Set the data rate to 50 Hz (for example, but can choose any sample rate).
CTRL_REG1_Data = IIC_RegRead(0x2A); //Note: Can combine this step with above
CTRL_REG1_Data& = 0xC7; //Clear the sample rate bits
CTRL_REG1_Data | = 0x20; //Set the sample rate bits to 50 Hz
IC_RegWrite(0x2A, CTRL_REG1_Data); //Write updated value into the register.
Step 3: Set the PL_EN bit in Register 0x11 PL_CFG. This will enable the orientation detection.
PLCFG_Data = IIC_RegRead (0x11);
PLCFG_Data | = 0x40;
IIC_RegWrite(0x11, PLCFG_Data);
Step 4: Set the Back/Front Angle trip points in register 0x13 following the table in the data sheet.
NOTE: This register is readable in all versions of MMA845xQ but it is only modifiable in the
MMA8451Q.
PL_BF_ZCOMP_Data = IIC_RegRead(0x13);
PL_BF_ZCOMP_Data& = 0x3F; //Clear bit 7 and 6
Select one of the following to set the B/F angle value:
PL_BF_ZCOMP_Data | = 0x00; //This does nothing additional and keeps bits [7:6] = 00
PL_BF_ZCOMP_Data | = 0x40; //Sets bits[7:6] = 01
PL_BF_ZCOMP_Data | = 0x80; //Sets bits[7:6] = 02
PL_BF_ZCOMP_Data | = 0xC0; //Sets bits[7:6] = 03
IIC_RegWrite(0x13, PL_BF_ZCOMP_Data); //Write in the updated Back/Front Angle
Step 5: Set the Z-Lockout angle trip point in register 0x13 following the table in the data sheet.
NOTE: This register is readable in all versions of MMA845xQ but it is only modifiable in the
MMA8451Q.
PL_BF_ZCOMP_Data = IIC_RegRead(0x1C); //Read out contents of the register (can be read by all
versions of MMA845xQ)
The remaining parts of this step only apply to MMA8451Q
PL_BF_ZCOMP_Data& = 0xF8; //Clear the last three bits of the register
Select one of the following to set the Z-lockout value
PL_BF_ZCOMP_Data | = 0x00; //This does nothing additional but the Z-lockout selection will remain at
14°
PL_BF_ZCOMP_Data | = 0x01; //Set the Z-lockout angle to 18°
PL_BF_ZCOMP_Data | = 0x02; //Set the Z-lockout angle to 21°
PL_BF_ZCOMP_Data | = 0x03; //Set the Z-lockout angle to 25°
PL_BF_ZCOMP_Data | = 0x04; //Set the Z-lockout angle to 29°
PL_BF_ZCOMP_Data | = 0x05; //Set the Z-lockout angle to 33°
PL_BF_ZCOMP_Data | = 0x06; //Set the Z-lockout angle to 37°
PL_BF_ZCOMP_Data | = 0x07; //Set the Z-lockout angle to 42°
IIC_RegWrite(0x13, PL_BF_ZCOMP_Data); //Write in the updated Z-lockout angle
Step 6: Set the Trip Threshold Angle
NOTE: This register is readable in all versions of MMA845xQ but it is only modifiable in the
MMA8451Q.
Select the angle desired in the table, and,
Enter in the values given in the table for the corresponding angle.
Refer to Figure 7 for the reference frame of the trip angles.
P_L_THS_Data = IIC_RegRead(0x14); (can be read by all versions of MMA845xQ)
The remaining parts of this step only apply to MMA8451Q
P_L_THS_Data& = 0x07; //Clear the Threshold values
Choose one of the following options
P_L_THS_Data | = (0x07)<<3; //Set Threshold to 15°
P_L_THS_Data | = (0x09)<<3; //Set Threshold to 20°
P_L_THS_Data | = (0x0C)<<3; //Set Threshold to 30°
P_L_THS_Data | = (0x0D)<<3; //Set Threshold to 35°
P_L_THS_Data | = (0x0F)<<3; //Set Threshold to 40°
P_L_THS_Data | = (0x10)<<3; //Set Threshold to 45°
P_L_THS_Data | = (0x13)<<3; //Set Threshold to 55°
P_L_THS_Data | = (0x14)<<3; //Set Threshold to 60°
P_L_THS_Data | = (0x17)<<3; //Set Threshold to 70°
P_L_THS_Data | = (0x19)<<3; //Set Threshold to 75°
IIC_RegWrite(0x14, P_L_THS_Data);
Step 7: Set the Hysteresis Angle
NOTE: This register is readable in all versions of MMA845xQ but it is only modifiable in the
MMA8451Q.
Select the hysteresis value based on the desired final trip points (threshold + hysteresis)
Enter in the values given in the table for that corresponding angle.
Note: Care must be taken. Review the final resulting angles. Make sure there isn’t a resulting trip value
greater than 90 or less than 0.
The following are the options for setting the hysteresis.
P_L_THS_Data = IIC_RegRead(0x14);
NOTE: The remaining parts of this step only apply to the MMA8451Q.
P_L_THS_Data& = 0xF8; //Clear the Hysteresis values
P_L_THS_Data | = 0x01; //Set Hysteresis to ±4°
P_L_THS_Data | = 0x02; //Set Threshold to ±7°
P_L_THS_Data | = 0x03; //Set Threshold to ±11°
P_L_THS_Data | = 0x04; //Set Threshold to ±14°
P_L_THS_Data | = 0x05; //Set Threshold to ±17°
P_L_THS_Data | = 0x06; //Set Threshold to ±21°
P_L_THS_Data | = 0x07; //Set Threshold to ±24°
IIC_RegWrite(0x14, P_L_THS_Data);
Step 8: Register 0x2D, Control Register 4 configures all embedded features for interrupt
detection.
To set this device up to run an interrupt service routine:
Program the Orientation Detection bit in Control Register 4.
Set bit 4 to enable the orientation detection “INT_EN_LNDPRT”.
CTRL_REG4_Data = IIC_RegRead(0x2D); //Read out the contents of the register
CTRL_REG4_Data | = 0x10; //Set bit 4
IIC_RegWrite(0x2D, CTRL_REG4_Data); //Set the bit and write into CTRL_REG4
Step 9: Register 0x2E is Control Register 5 which gives the option of routing the interrupt to
either INT1 or INT2
Depending on which interrupt pin is enabled and configured to the processor:
Set bit 4 “INT_CFG_LNDPRT” to configure INT1, or,
Leave the bit clear to configure INT2.
CTRL_REG5_Data = IIC_RegRead(0x2E);
In the next two lines choose to clear bit 4 to route to INT2 or set bit 4 to route to INT1
CTRL_REG5_Data& = 0xEF; //Clear bit 4 to choose the interrupt to route to INT2
CTRL_REG5_Data | = 0x10; //Set bit 4 to choose the interrupt to route to INT1
IIC_RegWrite(0x2E, CTRL_REG5_Data); //Write in the interrupt routing selection
Step 10: Set the debounce counter in register 0x12
This value will scale depending on the application-specific required ODR.
If the device is set to go to sleep, reset the debounce counter before the device goes to sleep. This setting
helps avoid long delays since the debounce will always scale with the current sample rate. The debounce
can be set between 50 ms - 100 ms to avoid long delays.
IIC_RegWrite(0x12, 0x05); //This sets the debounce counter to 100 ms at 50 Hz
Step 11: Put the device in Active Mode
CTRL_REG1_Data = IIC_RegRead(0x2A); //Read out the contents of the register
CTRL_REG1_Data | = 0x01; //Change the value in the register to Active Mode.
IIC_RegWrite(0x2A, CTRL_REG1_Data); //Write in the updated value to put the device in Active Mode
Step 12: Write a Service Routine to Service the Interrupt
Register 0x0C gives the status of any of the interrupts that are enabled in the entire device.
•An interrupt service routine must be set to handle enabling and then clearing of the interrupts. Register 0x0C will be read to determine which interrupt caused the event.
•When bit 4 is set in Register 0x0C “SRC_LNDPRT” this is the indication that a new orientation has been detected.
•The interrupt source (0x0C) register and the PL_Status (0x10) register are cleared and the new portrait/landscape detection can occur. 
CTRL_REG1_Data = IIC_RegRead(0x2A); //read contents of register
CTRL_REG1_Data& = 0xFE; //Set last bit to 0.
IIC_RegWrite(0x2A, CTRL_REG1_Data); //write the updated value in CTRL_REG1
Step 2: Set the data rate to 50 Hz (for example, but can choose any sample rate).
CTRL_REG1_Data = IIC_RegRead(0x2A); //Note: Can combine this step with above
CTRL_REG1_Data& = 0xC7; //Clear the sample rate bits
CTRL_REG1_Data | = 0x20; //Set the sample rate bits to 50 Hz
IC_RegWrite(0x2A, CTRL_REG1_Data); //Write updated value into the register.
Step 3: Set the PL_EN bit in Register 0x11 PL_CFG. This will enable the orientation detection.
PLCFG_Data = IIC_RegRead (0x11);
PLCFG_Data | = 0x40;
IIC_RegWrite(0x11, PLCFG_Data);
Step 4: Set the Back/Front Angle trip points in register 0x13 following the table in the data sheet.
NOTE: This register is readable in all versions of MMA845xQ but it is only modifiable in the
MMA8451Q.
PL_BF_ZCOMP_Data = IIC_RegRead(0x13);
PL_BF_ZCOMP_Data& = 0x3F; //Clear bit 7 and 6
Select one of the following to set the B/F angle value:
PL_BF_ZCOMP_Data | = 0x00; //This does nothing additional and keeps bits [7:6] = 00
PL_BF_ZCOMP_Data | = 0x40; //Sets bits[7:6] = 01
PL_BF_ZCOMP_Data | = 0x80; //Sets bits[7:6] = 02
PL_BF_ZCOMP_Data | = 0xC0; //Sets bits[7:6] = 03
IIC_RegWrite(0x13, PL_BF_ZCOMP_Data); //Write in the updated Back/Front Angle
Step 5: Set the Z-Lockout angle trip point in register 0x13 following the table in the data sheet.
NOTE: This register is readable in all versions of MMA845xQ but it is only modifiable in the
MMA8451Q.
PL_BF_ZCOMP_Data = IIC_RegRead(0x1C); //Read out contents of the register (can be read by all
versions of MMA845xQ)
The remaining parts of this step only apply to MMA8451Q
PL_BF_ZCOMP_Data& = 0xF8; //Clear the last three bits of the register
Select one of the following to set the Z-lockout value
PL_BF_ZCOMP_Data | = 0x00; //This does nothing additional but the Z-lockout selection will remain at
14°
PL_BF_ZCOMP_Data | = 0x01; //Set the Z-lockout angle to 18°
PL_BF_ZCOMP_Data | = 0x02; //Set the Z-lockout angle to 21°
PL_BF_ZCOMP_Data | = 0x03; //Set the Z-lockout angle to 25°
PL_BF_ZCOMP_Data | = 0x04; //Set the Z-lockout angle to 29°
PL_BF_ZCOMP_Data | = 0x05; //Set the Z-lockout angle to 33°
PL_BF_ZCOMP_Data | = 0x06; //Set the Z-lockout angle to 37°
PL_BF_ZCOMP_Data | = 0x07; //Set the Z-lockout angle to 42°
IIC_RegWrite(0x13, PL_BF_ZCOMP_Data); //Write in the updated Z-lockout angle
Step 6: Set the Trip Threshold Angle
NOTE: This register is readable in all versions of MMA845xQ but it is only modifiable in the
MMA8451Q.
Select the angle desired in the table, and,
Enter in the values given in the table for the corresponding angle.
Refer to Figure 7 for the reference frame of the trip angles.
P_L_THS_Data = IIC_RegRead(0x14); (can be read by all versions of MMA845xQ)
The remaining parts of this step only apply to MMA8451Q
P_L_THS_Data& = 0x07; //Clear the Threshold values
Choose one of the following options
P_L_THS_Data | = (0x07)<<3; //Set Threshold to 15°
P_L_THS_Data | = (0x09)<<3; //Set Threshold to 20°
P_L_THS_Data | = (0x0C)<<3; //Set Threshold to 30°
P_L_THS_Data | = (0x0D)<<3; //Set Threshold to 35°
P_L_THS_Data | = (0x0F)<<3; //Set Threshold to 40°
P_L_THS_Data | = (0x10)<<3; //Set Threshold to 45°
P_L_THS_Data | = (0x13)<<3; //Set Threshold to 55°
P_L_THS_Data | = (0x14)<<3; //Set Threshold to 60°
P_L_THS_Data | = (0x17)<<3; //Set Threshold to 70°
P_L_THS_Data | = (0x19)<<3; //Set Threshold to 75°
IIC_RegWrite(0x14, P_L_THS_Data);
Step 7: Set the Hysteresis Angle
NOTE: This register is readable in all versions of MMA845xQ but it is only modifiable in the
MMA8451Q.
Select the hysteresis value based on the desired final trip points (threshold + hysteresis)
Enter in the values given in the table for that corresponding angle.
Note: Care must be taken. Review the final resulting angles. Make sure there isn’t a resulting trip value
greater than 90 or less than 0.
The following are the options for setting the hysteresis.
P_L_THS_Data = IIC_RegRead(0x14);
NOTE: The remaining parts of this step only apply to the MMA8451Q.
P_L_THS_Data& = 0xF8; //Clear the Hysteresis values
P_L_THS_Data | = 0x01; //Set Hysteresis to ±4°
P_L_THS_Data | = 0x02; //Set Threshold to ±7°
P_L_THS_Data | = 0x03; //Set Threshold to ±11°
P_L_THS_Data | = 0x04; //Set Threshold to ±14°
P_L_THS_Data | = 0x05; //Set Threshold to ±17°
P_L_THS_Data | = 0x06; //Set Threshold to ±21°
P_L_THS_Data | = 0x07; //Set Threshold to ±24°
IIC_RegWrite(0x14, P_L_THS_Data);
Step 8: Register 0x2D, Control Register 4 configures all embedded features for interrupt
detection.
To set this device up to run an interrupt service routine:
Program the Orientation Detection bit in Control Register 4.
Set bit 4 to enable the orientation detection “INT_EN_LNDPRT”.
CTRL_REG4_Data = IIC_RegRead(0x2D); //Read out the contents of the register
CTRL_REG4_Data | = 0x10; //Set bit 4
IIC_RegWrite(0x2D, CTRL_REG4_Data); //Set the bit and write into CTRL_REG4
Step 9: Register 0x2E is Control Register 5 which gives the option of routing the interrupt to
either INT1 or INT2
Depending on which interrupt pin is enabled and configured to the processor:
Set bit 4 “INT_CFG_LNDPRT” to configure INT1, or,
Leave the bit clear to configure INT2.
CTRL_REG5_Data = IIC_RegRead(0x2E);
In the next two lines choose to clear bit 4 to route to INT2 or set bit 4 to route to INT1
CTRL_REG5_Data& = 0xEF; //Clear bit 4 to choose the interrupt to route to INT2
CTRL_REG5_Data | = 0x10; //Set bit 4 to choose the interrupt to route to INT1
IIC_RegWrite(0x2E, CTRL_REG5_Data); //Write in the interrupt routing selection
Step 10: Set the debounce counter in register 0x12
This value will scale depending on the application-specific required ODR.
If the device is set to go to sleep, reset the debounce counter before the device goes to sleep. This setting
helps avoid long delays since the debounce will always scale with the current sample rate. The debounce
can be set between 50 ms - 100 ms to avoid long delays.
IIC_RegWrite(0x12, 0x05); //This sets the debounce counter to 100 ms at 50 Hz
Step 11: Put the device in Active Mode
CTRL_REG1_Data = IIC_RegRead(0x2A); //Read out the contents of the register
CTRL_REG1_Data | = 0x01; //Change the value in the register to Active Mode.
IIC_RegWrite(0x2A, CTRL_REG1_Data); //Write in the updated value to put the device in Active Mode
Step 12: Write a Service Routine to Service the Interrupt
Register 0x0C gives the status of any of the interrupts that are enabled in the entire device.
•An interrupt service routine must be set to handle enabling and then clearing of the interrupts. Register 0x0C will be read to determine which interrupt caused the event.
•When bit 4 is set in Register 0x0C “SRC_LNDPRT” this is the indication that a new orientation has been detected.
•The interrupt source (0x0C) register and the PL_Status (0x10) register are cleared and the new portrait/landscape detection can occur. 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值