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%A8%88%E3%81%AE%E5%90%91%E3%81%8D%E6%A4%9C%E5%87%BA%E9%96%A2%E6%95%B0%E3%81%AE%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E3%82%B3%E3%83%BC%E3%83%89%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%E3%81%AD%E3%81%88%E7%9A%86%E3%81%95%E3%82%93%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%3EFXLS8471Q%E6%96%B9%E5%90%91%E6%A4%9C%E5%87%BA%E6%A9%9F%E8%83%BD%E3%81%AE%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95%E3%82%92%E7%A4%BA%E3%81%99%E5%8D%98%E7%B4%94%E3%81%AA%E3%83%99%E3%82%A2%E3%83%A1%E3%82%BF%E3%83%AB%E3%81%AE%E4%BE%8B%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AB%E8%88%88%E5%91%B3%E3%81%8C%E3%81%82%E3%82%8B%E5%A0%B4%E5%90%88%E3%81%AF%E3%80%81NXP%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%3E%20FRDM-KL25Z%3C%2FA%3E%3CSPAN%3E%3C%2FSPAN%3E%20%E3%83%97%E3%83%A9%E3%83%83%E3%83%88%E3%83%95%E3%82%A9%E3%83%BC%E3%83%A0%E3%81%A8%3CSPAN%3E%3C%2FSPAN%3E%20%3CSPAN%3E%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%3C%2FSPAN%3E%20%E3%83%9C%E3%83%BC%E3%83%89%E3%81%A7%E4%BD%9C%E6%A5%AD%E3%81%97%E3%81%AA%E3%81%8C%E3%82%89%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%3E%20FXLS8471Q%E5%8A%A0%E9%80%9F%E5%BA%A6%E8%A8%88%20%E7%94%A8%E3%81%AB%E4%BD%9C%E6%88%90%E3%81%97%E3%81%9F%E4%BE%8B%E3%81%AE1%E3%81%A4%E3%82%92%E4%BB%A5%E4%B8%8B%E3%81%AB%E7%A4%BA%E3%81%97%E3%81%BE%E3%81%99%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%20%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%E3%81%93%E3%81%AE%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AF%E3%80%81%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%20%E3%81%AE%E3%82%B3%E3%83%BC%E3%83%89%E3%82%B9%E3%83%8B%E3%83%9A%E3%83%83%E3%83%88%E3%82%92%E8%A3%9C%E5%AE%8C%E3%81%99%E3%82%8B%E3%82%82%E3%81%AE%E3%81%A7%E3%81%99%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%3EISR%20%E3%81%A7%E3%81%AF%E3%80%81%E5%89%B2%E3%82%8A%E8%BE%BC%E3%81%BF%E3%83%95%E3%83%A9%E3%82%B0%E3%81%AE%E3%81%BF%E3%81%8C%E3%82%AF%E3%83%AA%E3%82%A2%E3%81%95%E3%82%8C%E3%80%81PL_STATUS%20(0x10)%20%E3%83%AC%E3%82%B8%E3%82%B9%E3%82%BF%E3%81%8C%E8%AA%AD%E3%81%BF%E5%8F%96%E3%82%89%E3%82%8C%3C%2FSPAN%3E%E3%81%BE%E3%81%99%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-%20INT_SOURCE%E3%83%AC%E3%82%B8%E3%82%B9%E3%82%BF%E3%81%AESRC_LNDPRT%E3%83%95%E3%83%A9%E3%82%B0%E3%82%92%E3%82%AF%E3%83%AA%E3%82%A2%E3%81%97%E3%80%81%E6%AC%A1%E3%81%AE%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%E3%81%AB%E7%A4%BA%E3%81%99%E3%82%88%E3%81%86%E3%81%ABINT1%E3%83%94%E3%83%B3%E3%82%92%E3%83%87%E3%82%A3%E3%82%A2%E3%82%B5%E3%83%BC%E3%83%88%E3%81%97%E3%81%BE%E3%81%99%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%E3%82%AA%E3%83%AA%E3%82%A8%E3%83%B3%E3%83%86%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E6%83%85%E5%A0%B1%E3%82%92%E5%8F%96%E5%BE%97%E3%81%97%E3%81%BE%E3%81%99%E3%80%82%E3%81%93%E3%81%AE%E4%BE%8B%E3%81%A7%E3%81%AF%E3%80%810x82%20%22Portrait%20down%22%20%E3%81%AE%E5%90%91%E3%81%8D%E3%81%AB%E5%AF%BE%E5%BF%9C%E3%81%97%E3%81%A6%E3%81%84%E3%81%BE%E3%81%99%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%B7%BB%E4%BB%98%E3%81%95%E3%82%8C%E3%81%A6%E3%81%84%E3%82%8B%E5%AE%8C%E5%85%A8%E3%81%AA%E3%82%BD%E3%83%BC%E3%82%B9%E3%82%B3%E3%83%BC%E3%83%89%E3%82%92%E8%A6%8B%E3%81%A4%E3%81%91%E3%82%8B%E3%81%93%E3%81%A8%E3%81%8C%E3%81%A7%E3%81%8D%E3%81%BE%E3%81%99%E3%80%82%E3%81%93%E3%81%AE%E7%B0%A1%E5%8D%98%E3%81%AA%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E3%82%B3%E3%83%BC%E3%83%89%E3%81%AB%E9%96%A2%E3%81%97%E3%81%A6%E8%B3%AA%E5%95%8F%E3%81%8C%E3%81%82%E3%82%8B%E5%A0%B4%E5%90%88%E3%81%AF%E3%80%81%E4%BB%A5%E4%B8%8B%E3%81%A7%E3%81%8A%E6%B0%97%E8%BB%BD%E3%81%AB%E3%81%8A%E5%95%8F%E3%81%84%E5%90%88%E3%82%8F%E3%81%9B%E3%81%8F%E3%81%A0%E3%81%95%E3%81%84%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%E3%82%88%E3%82%8D%E3%81%97%E3%81%8F%E3%81%8A%E9%A1%98%E3%81%84%E3%81%97%E3%81%BE%E3%81%99%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%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%E3%82%BB%E3%83%B3%E3%82%B5%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
評価なし
バージョン履歴
最終更新日:
‎09-11-2020 05:48 AM
更新者: