FXLS8471Q - Accelerometer orientation detection function example code

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

FXLS8471Q - Accelerometer orientation detection function example code

FXLS8471Q - Accelerometer orientation detection function example code

Hi Everyone,

 

If you are interested in a simple bare metal example code illustrating the use of the FXLS8471Q orientation detection function, please find below one of my examples I created for the FXLS8471Q accelerometer while working with the NXP FRDM-KL25Z platform and FRDMSTBC-A8471 board.

 

This example code complements the code snippet from the  AN4068.

 

void FXLS8471Q_Init (void)
{
  FXLS8471Q_WriteRegister(CTRL_REG1, 0x00);  // Standby mode
      
  FXLS8471Q_WriteRegister(PL_CFG_REG, 0x40); // Enable orientation detection
  FXLS8471Q_WriteRegister(PL_BF_ZCOMP_REG, 0x43);  // Back/Front trip point set to 75°, Z-lockout angle set to 25°
  FXLS8471Q_WriteRegister(P_L_THS_REG, 0x14);  // Threshold angle = 45°, hysteresis = 14°
  FXLS8471Q_WriteRegister(PL_COUNT_REG, 0x05); // Debounce counter set to 100ms at 50Hz 
      
  FXLS8471Q_WriteRegister(CTRL_REG3, 0x00);  // Push-pull, active low interrupt
  FXLS8471Q_WriteRegister(CTRL_REG4, 0x10); // Orientation interrupt enabled
  FXLS8471Q_WriteRegister(CTRL_REG5, 0x10); // Route orientation interrupt to INT1 - PTD4
            
  FXLS8471Q_WriteRegister(CTRL_REG1, 0x21); // ODR = 50Hz, Active mode
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

 

In the ISR, only the interrupt flag is cleared and the PL_STATUS (0x10) register is read in order to:

 

- Clear the SRC_LNDPRT flag in the INT_SOURCE register and deassert the INT1 pin, as shown on the screenshot below.

- Get orientation information. 0x82 in this example corresponds to "Portrait down" orientation.

 

void PORTD_IRQHandler()
{
  PORTD_PCR4 |= PORT_PCR_ISF_MASK; // Clear the interrupt flag 
  PL_Status = FXLS8471Q_ReadRegister(PL_STATUS_REG); // Read the PL_STATUS register to clear the SRC_LNDPRT flag in the INT_SOURCE register  
}‍‍‍‍‍‍‍‍‍‍

 

pastedImage_1.png

 

 

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

 

Regards,

Tomas

Labels (1)
Attachments
%3CLINGO-SUB%20id%3D%22lingo-sub-1116045%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EFXLS8471Q%20-%20Accelerometer%20orientation%20detection%20function%20example%20code%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1116045%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3EHi%20Everyone%2C%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3EIf%20you%20are%20interested%20in%20a%20simple%20bare%20metal%20example%20code%20illustrating%20the%20use%20of%20the%26nbsp%3BFXLS8471Q%20orientation%20detection%20function%2C%20please%20find%20below%20one%20of%20my%20examples%20I%20created%20for%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20style%3D%22color%3A%20%233d9ce7%3B%20border%3A%200px%3B%20font-weight%3A%20inherit%3B%20text-decoration%3A%20none%3B%22%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Fwww.nxp.com%2Fproducts%2Fsensors%2Faccelerometers%2F3-axis-accelerometers%2F2g-4g-8g-low-g-14-bit-digital-accelerometer%3AFXLS8471Q%22%20target%3D%22_blank%22%20data-content-finding%3D%22Community%22%3EFXLS8471Q%20accelerometer%3C%2FA%3E%26nbsp%3Bwhile%20working%20with%20the%20NXP%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20style%3D%22color%3A%20%233d9ce7%3B%20border%3A%200px%3B%20font-weight%3A%20inherit%3B%20text-decoration%3A%20none%3B%22%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Fwww.nxp.com%2Fproducts%2Fsoftware-and-tools%2Fhardware-development-tools%2Ffreedom-development-boards%2Ffreedom-development-platform-for-kinetis-kl14-kl15-kl24-kl25-mcus%3AFRDM-KL25Z%22%20target%3D%22_blank%22%20data-content-finding%3D%22Community%22%3EFRDM-KL25Z%3C%2FA%3E%26nbsp%3Bplatform%20and%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20style%3D%22color%3A%20%233d9ce7%3B%20border%3A%200px%3B%20font-weight%3A%20inherit%3B%20text-decoration%3A%20none%3B%22%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Fwww.nxp.com%2Fproducts%2Fsoftware-and-tools%2Fhardware-development-tools%2Ffreedom-development-boards%2Fsensor-toolbox-development-platform-for-fxls8471q-3-axis-linear-accelerometer%3AFRDMSTBC-A8471%22%20target%3D%22_blank%22%20data-content-finding%3D%22Community%22%3EFRDMSTBC-A8471%3C%2FA%3E%3CSPAN%20style%3D%22color%3A%20%234a4a4d%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%20font-weight%3A%20inherit%3B%20font-size%3A%2016px%3B%22%3E%26nbsp%3B%3C%2FSPAN%3Eboard%3CSPAN%20style%3D%22color%3A%20%234a4a4d%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%20font-weight%3A%20inherit%3B%20font-size%3A%2016px%3B%22%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E%3CSPAN%3EThis%20example%20code%20complements%20the%20code%20snippet%20from%20the%26nbsp%3B%20%3CA%20href%3D%22https%3A%2F%2Fwww.nxp.com%2Fdocs%2Fen%2Fapplication-note%2FAN4068.pdf%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EAN4068%3C%2FA%3E%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-cpp%22%3E%3CCODE%3Evoid%20FXLS8471Q_Init%20(void)%0A%7B%0A%20%20FXLS8471Q_WriteRegister(CTRL_REG1%2C%200x00)%3B%20%20%2F%2F%20Standby%20mode%0A%20%20%20%20%20%20%0A%20%20FXLS8471Q_WriteRegister(PL_CFG_REG%2C%200x40)%3B%20%2F%2F%20Enable%20orientation%20detection%0A%20%20FXLS8471Q_WriteRegister(PL_BF_ZCOMP_REG%2C%200x43)%3B%20%20%2F%2F%20Back%2FFront%20trip%20point%20set%20to%2075%C2%B0%2C%20Z-lockout%20angle%20set%20to%2025%C2%B0%0A%20%20FXLS8471Q_WriteRegister(P_L_THS_REG%2C%200x14)%3B%20%20%2F%2F%20Threshold%20angle%20%3D%2045%C2%B0%2C%20hysteresis%20%3D%2014%C2%B0%0A%20%20FXLS8471Q_WriteRegister(PL_COUNT_REG%2C%200x05)%3B%20%2F%2F%20Debounce%20counter%20set%20to%20100ms%20at%2050Hz%20%0A%20%20%20%20%20%20%0A%20%20FXLS8471Q_WriteRegister(CTRL_REG3%2C%200x00)%3B%20%20%2F%2F%20Push-pull%2C%20active%20low%20interrupt%0A%20%20FXLS8471Q_WriteRegister(CTRL_REG4%2C%200x10)%3B%20%2F%2F%20Orientation%20interrupt%20enabled%0A%20%20FXLS8471Q_WriteRegister(CTRL_REG5%2C%200x10)%3B%20%2F%2F%20Route%20orientation%20interrupt%20to%20INT1%20-%20PTD4%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20FXLS8471Q_WriteRegister(CTRL_REG1%2C%200x21)%3B%20%2F%2F%20ODR%20%3D%2050Hz%2C%20Active%20mode%0A%7D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E%3CSPAN%3EIn%20the%20ISR%2C%20only%20the%20interrupt%20flag%20is%20cleared%20and%20the%20PL_STATUS%20(0x10)%20register%20is%20read%20in%20order%26nbsp%3B%3C%2FSPAN%3Eto%3A%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E-%20Clear%20the%20SRC_LNDPRT%20flag%20in%20the%20INT_SOURCE%20register%20and%20deassert%20the%20INT1%20pin%2C%20as%20shown%20on%20the%20screenshot%20below.%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E-%20Get%20orientation%20information.%200x82%20in%20this%20example%20corresponds%20to%20%22Portrait%20down%22%20orientation.%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-c%22%3E%3CCODE%3Evoid%20PORTD_IRQHandler()%0A%7B%0A%20%20PORTD_PCR4%20%7C%3D%20PORT_PCR_ISF_MASK%3B%20%2F%2F%20Clear%20the%20interrupt%20flag%20%0A%20%20PL_Status%20%3D%20FXLS8471Q_ReadRegister(PL_STATUS_REG)%3B%20%2F%2F%20Read%20the%20PL_STATUS%20register%20to%20clear%20the%20SRC_LNDPRT%20flag%20in%20the%20INT_SOURCE%20register%20%20%0A%7D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%E2%80%8D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CBR%20%2F%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pastedImage_1.png%22%20style%3D%22width%3A%20593px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22pastedImage_1.png%22%20style%3D%22width%3A%20593px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F78164i3E275F7321E7C9AC%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22pastedImage_1.png%22%20alt%3D%22pastedImage_1.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CBR%20%2F%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3EAttached%20you%20can%20find%20the%20complete%20source%20code.%20If%20there%20are%20any%20questions%20regarding%20this%20simple%20example%20code%2C%20please%20feel%20free%20to%20ask%20below.%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3ERegards%2C%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3ETomas%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1116045%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CLINGO-LABEL%3EAccelerometers%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
No ratings
Version history
Last update:
‎09-11-2020 05:48 AM
Updated by: