Hi Everyone, As I am often asked for a simple bare metal example code illustrating the use of the accelerometer vector-magnitude function, I have decided to share here one of my examples I created for the FXLS8471Q accelerometer while working with the Freescale FRDM-KL25Z platform and FRDM-FXS-MULT2-B sensor expansion board. This example code complements the Python code snippet from the AN4692. The FXLS8471Q is set for detection of a change in tilt angle exceeding 17.25° from the horizontal plane. Once an event is triggered, an interrupt will be generated on the INT1 pin: void FXLS8471Q_Init (void) { FXLS8471Q_WriteRegister(A_VECM_THS_MSB_REG, 0x84); // Threshold value set to 300mg or ~17.25° FXLS8471Q_WriteRegister(A_VECM_THS_LSB_REG, 0xCC); FXLS8471Q_WriteRegister(A_VECM_CNT_REG, 0x01); // Debounce timer period set to 80ms FXLS8471Q_WriteRegister(A_VECM_INITX_MSB_REG, 0x00); FXLS8471Q_WriteRegister(A_VECM_INITX_LSB_REG, 0x00); FXLS8471Q_WriteRegister(A_VECM_INITY_MSB_REG, 0x00); FXLS8471Q_WriteRegister(A_VECM_INITY_LSB_REG, 0x00); FXLS8471Q_WriteRegister(A_VECM_INITZ_MSB_REG, 0x10); // Set Z-axis to 1g as a reference value FXLS8471Q_WriteRegister(A_VECM_INITZ_LSB_REG, 0x00); FXLS8471Q_WriteRegister(A_VECM_CFG_REG, 0x78); // Event latch enabled, A_VECM_INITX/Y/Z used as initial reference, acceleration vector-magnitude detection feature enabled FXLS8471Q_WriteRegister(CTRL_REG4, 0x02); // Acceleration vector-magnitude interrupt enabled FXLS8471Q_WriteRegister(CTRL_REG5, 0x02); // Acceleration vector-magnitude interrupt routed to INT1 - PTA5 FXLS8471Q_WriteRegister(CTRL_REG1, 0x29); // ODR = 12.5Hz, Active mode } In the ISR, only the interrupt flag is cleared and the INT_SOURCE (0x0C) register is read in order to clear the SRC_A_VECM 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(INT_SOURCE_REG); // Read the INT_SOURCE register to clear the SRC_A_VECM bit } 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
View full article