Hi Everyone,
As I am often asked for a simple bare metal example code illustrating the use of the embedded rate threshold detection function, I have decided to share here one of my examples I created for the FXAS21002C gyroscope while working with the NXP FRDM-KL25Z platform and FRDM-FXS-MULT2-B sensor expansion board.
The FXAS21002C is set for detection of an angular rate exceeding 96 dps for a minimum period of 20 ms on either the X or Y axes. Once an event is triggered, an interrupt will be generated on the INT1 pin:
void FXAS21002_Init (void) { unsigned char reg_val = 0; I2C_WriteRegister(FXAS21002_I2C_ADDRESS, CTRL_REG1, 0x40); // Reset all registers to POR values Pause(0x631); // ~1ms delay do // Wait for the RST bit to clear { reg_val = I2C_ReadRegister(FXAS21002_I2C_ADDRESS, CTRL_REG1) & 0x40; } while (reg_val); I2C_WriteRegister(FXAS21002_I2C_ADDRESS, RT_THS_REG, 0x05); // Set threshold to 96 dps I2C_WriteRegister(FXAS21002_I2C_ADDRESS, RT_COUNT_REG, 0x02); // Set debounce timer period to 20 ms I2C_WriteRegister(FXAS21002_I2C_ADDRESS, RT_CFG_REG, 0x0B); // Enable rate threshold detection for X and Y axis, latch enabled I2C_WriteRegister(FXAS21002_I2C_ADDRESS, CTRL_REG2, 0x30); // Rate threshold interrupt enabled and routed to INT1 I2C_WriteRegister(FXAS21002_I2C_ADDRESS, CTRL_REG1, 0x0E); // ODR = 100 Hz, Active mode }
In the ISR, only the interrupt flag is cleared and the RT_SRC register (0x0F) is read in order to clear the EA status bit and deassert the INT1 pin, as shown on the screenshot below. 0x4C in the RT_SRC register indicates that the rate threshold event has been detected on the Y-axis and was negative.
void PORTA_IRQHandler() { PORTA_PCR5 |= PORT_PCR_ISF_MASK; // Clear the interrupt flag IntSource = I2C_ReadRegister(FXAS21002_I2C_ADDRESS, RT_SRC_REG); // Read the RT_SRC register to clear the EA flag and deassert the INT1 pin 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
Original Attachment has been moved to: FRDM-KL25Z-FXAS21002-Angular-rate-detection-using-interrupts.rar