FXOS8700CQ - Magnetic threshold detection function example code

Document created by Tomas Vaverka Employee on Sep 20, 2017Last modified by ebiz_ws_prod on Dec 13, 2017
Version 3Show Document
  • View in full screen mode

Hi Everyone,


If you are interested in a simple bare metal example code illustrating the use of the magnetic threshold detection function, please find below one of my examples I created for the FXOS8700CQ while working with the NXP FRDM-KL25Z platform and FRDM-FXS-MULT2-B sensor expansion board.


The FXOS8700CQ is set to detect magnetic field exceeding 12.8uT (128 counts) for a minimum period of 100ms on the X-axis. Once an event is triggered, an active low interrupt will be generated on the INT1 pin:


void FXOS8700CQ_Init (void)
   I2C_WriteRegister(FXOS8700CQ_I2C_ADDRESS, M_THS_X_MSB_REG, 0x00); // Threshold value MSB 
   I2C_WriteRegister(FXOS8700CQ_I2C_ADDRESS, M_THS_X_LSB_REG, 0x80); // Threshold value LSB 
   I2C_WriteRegister(FXOS8700CQ_I2C_ADDRESS, M_THS_CFG_REG, 0xCB);   // Event flag latch enabled, logic OR of enabled axes, only X-axis enabled, threshold interrupt enabled and routed to INT1
   I2C_WriteRegister(FXOS8700CQ_I2C_ADDRESS, M_THS_COUNT_REG, 0x0A); // 100ms at 100Hz ODR and magnetometer mode only
   I2C_WriteRegister(FXOS8700CQ_I2C_ADDRESS, M_CTRL_REG1, 0x1D);     // Max OSR, only magnetometer is active
   I2C_WriteRegister(FXOS8700CQ_I2C_ADDRESS, CTRL_REG3, 0x00);       // Push-pull, active low interrupt
   I2C_WriteRegister(FXOS8700CQ_I2C_ADDRESS, CTRL_REG1, 0x19);       // ODR = 100Hz, Active mode      



In the ISR, only the interrupt flag is cleared and the M_THS_SRC (0x53) register is read in order to clear the SRC_M_THS flag in the M_INT_SRC register and deassert the INT1 pin, as shown on the screenshot below.


void PORTD_IRQHandler()
   PORTD_PCR4 |= PORT_PCR_ISF_MASK;                                       // Clear the interrupt flag
   M_Ths_Src = I2C_ReadRegister(FXOS8700CQ_I2C_ADDRESS, M_THS_SRC_REG);   // Read the M_THS_SRC register to clear the SRC_M_THS flag in the M_INT_SRC register and deassert the INT1 pin




Attached you can find the complete source code. If there are any questions regarding this simple example code, please feel free to ask below. 




Original Attachment has been moved to: FRDM-KL25Z-FXOS8700CQ-Magnetic-threshold-detection-using-interrupts.rar

1 person found this helpful