FXLS8471Q - Accelerometer orientation detection function example code

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

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

标签 (1)
附件
%3CLINGO-SUB%20id%3D%22lingo-sub-1116045%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EFXLS8471Q%20-%20%E5%8A%A0%E9%80%9F%E5%BA%A6%E8%AE%A1%E6%96%B9%E5%90%91%E6%A3%80%E6%B5%8B%E5%8A%9F%E8%83%BD%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81%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%3E%E5%A4%A7%E5%AE%B6%E5%A5%BD%EF%BC%8C%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%E5%A6%82%E6%9E%9C%E6%82%A8%E5%AF%B9%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E8%A3%B8%E6%9C%BA%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81%E6%84%9F%E5%85%B4%E8%B6%A3%EF%BC%8C%E8%AF%A5%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81%E8%AF%B4%E6%98%8E%E4%BA%86%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20FXLS8471Q%20%E6%96%B9%E5%90%91%E6%A3%80%E6%B5%8B%E5%8A%9F%E8%83%BD%EF%BC%8C%E8%AF%B7%E5%8F%82%E9%98%85%E4%B8%8B%E9%9D%A2%E6%88%91%E4%B8%BA%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%20%E5%8A%A0%E9%80%9F%E5%BA%A6%E8%AE%A1%3C%2FA%3E%E4%B8%8E%20NXP%20%E9%85%8D%E5%90%88%E4%BD%BF%E7%94%A8%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%E5%B9%B3%E5%8F%B0%E5%92%8C%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%3E%E6%9C%A8%E6%9D%BF%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%E3%80%82%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%3E%E6%AD%A4%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81%E8%A1%A5%E5%85%85%E4%BA%86%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%E4%B8%AD%E7%9A%84%E4%BB%A3%E7%A0%81%E7%89%87%E6%AE%B5%3C%2FSPAN%3E%3CSPAN%3E%E3%80%82%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%20translate%3D%22no%22%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%3E%E5%9C%A8%20ISR%20%E4%B8%AD%EF%BC%8C%E4%BB%85%E6%B8%85%E9%99%A4%E4%B8%AD%E6%96%AD%E6%A0%87%E5%BF%97%E5%B9%B6%E8%AF%BB%E5%8F%96%20PL_STATUS%20(0x10)%20%E5%AF%84%E5%AD%98%E5%99%A8%E4%BB%A5%E4%BE%BF%3C%2FSPAN%3E%EF%BC%9A%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-%20%E6%B8%85%E9%99%A4%20INT_SOURCE%20%E5%AF%84%E5%AD%98%E5%99%A8%E4%B8%AD%E7%9A%84%20SRC_LNDPRT%20%E6%A0%87%E5%BF%97%E5%B9%B6%E5%8F%96%E6%B6%88%20INT1%20%E5%BC%95%E8%84%9A%EF%BC%8C%E5%A6%82%E4%B8%8B%E9%9D%A2%E7%9A%84%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%E6%89%80%E7%A4%BA%E3%80%82%3C%2FP%3E%0A%3CP%20style%3D%22color%3A%20%2351626f%3B%20background-color%3A%20%23ffffff%3B%20border%3A%200px%3B%22%3E-%20%E8%8E%B7%E5%8F%96%E6%96%B9%E5%90%91%E4%BF%A1%E6%81%AF%E3%80%82%E6%9C%AC%E4%BE%8B%E4%B8%AD%E7%9A%84%200x82%20%E5%AF%B9%E5%BA%94%E4%BA%8E%E2%80%9C%E7%BA%B5%E5%90%91%E5%90%91%E4%B8%8B%E2%80%9D%E6%96%B9%E5%90%91%E3%80%82%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%20translate%3D%22no%22%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%3E%E6%82%A8%E5%8F%AF%E4%BB%A5%E5%9C%A8%E9%99%84%E4%BB%B6%E4%B8%AD%E6%89%BE%E5%88%B0%E5%AE%8C%E6%95%B4%E7%9A%84%E6%BA%90%E4%BB%A3%E7%A0%81%E3%80%82%E5%A6%82%E6%9E%9C%E5%AF%B9%E8%BF%99%E4%B8%AA%E7%AE%80%E5%8D%95%E7%9A%84%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81%E6%9C%89%E4%BB%BB%E4%BD%95%E7%96%91%E9%97%AE%EF%BC%8C%E8%AF%B7%E9%9A%8F%E6%97%B6%E5%9C%A8%E4%B8%8B%E9%9D%A2%E6%8F%90%E9%97%AE%E3%80%82%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%E6%AD%A4%E8%87%B4%EF%BC%8C%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%3E%E5%8A%A0%E9%80%9F%E5%BA%A6%E4%BC%A0%E6%84%9F%E5%99%A8%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
无评分
版本历史
最后更新:
‎02-03-2026 05:12 PM
更新人: