Hi Everyone,
As I am often asked for a simple bare metal example code illustrating the use of the accelerometer transient detection function, I have decided to share here one of my examples I created for the FXLS8471Q accelerometer while working with the NXP FRDM-KL25Z platform and FRDM-FXS-MULT2-B sensor expansion board.
This example code complements the Python code snippet from the AN4693. The FXLS8471Q is set for detection of an “instantaneous” acceleration change exceeding 315mg for a minimum period of 40 ms on either the X or Y axes. Once an event is triggered, an interrupt will be generated on the INT1 pin:
void FXLS8471Q_Init (void)
{
FXLS8471Q_WriteRegister(TRANSIENT_THS_REG, 0x85); // Set threshold to 312.5mg (5 x 62.5mg )
FXLS8471Q_WriteRegister(TRANSIENT_COUNT_REG, 0x02); // Set debounce timer period to 40ms
FXLS8471Q_WriteRegister(TRANSIENT_CFG_REG, 0x16); // Enable transient detection for X and Y axis, latch enabled
FXLS8471Q_WriteRegister(CTRL_REG4, 0x20); // Acceleration transient interrupt enabled
FXLS8471Q_WriteRegister(CTRL_REG5, 0x20); // Route acceleration transient interrupt to INT1 - PTA5
FXLS8471Q_WriteRegister(CTRL_REG1, 0x29); // ODR = 12.5Hz, Active mode
}
In the ISR, only the interrupt flag is cleared and the TRANSIENT_SRC (0x1E) register is read in order to clear the SRC_TRANS status bit and deassert the INT1 pin, as shown on the screenshot below.
void PORTA_IRQHandler()
{
PORTA_PCR5 |= PORT_PCR_ISF_MASK; // Clear the interrupt flag
IntSource = FXLS8471Q_ReadRegister(TRANSIENT_SRC_REG); // Read the TRANSIENT_SRC register to clear the SRC_TRANS flag in the INT_SOURCE register
EventCounter++;
}
Attached you can find the complete source code. If there are any questions regarding this simple example code, please feel free to ask below. Your feedback or suggestions are also welcome.
Regards,
Tomas