查看: 831|回复: 0

[分享] 【MCXC444分享】FXLS8974传感器测试

[复制链接]
  • TA的每日心情
    开心
    前天 14:03
  • 签到天数: 1536 天

    连续签到: 14 天

    [LV.Master]伴坛终老

    152

    主题

    3224

    帖子

    31

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    8844
    最后登录
    2025-9-29
    发表于 2024-11-14 14:15:04 | 显示全部楼层 |阅读模式
    本帖最后由 TLLED 于 2024-11-14 10:26 编辑


    测试下板卡上的FXLS8974传感器芯片。

    一、硬件电路

    1.1、FXLS8974部分电路图
    101.png

    1.2、配置引脚
    FXLS8974传感器使用I2C0接口
    001.png

    二、程序部分


    2.1、fxls8974.c
    1. #include "fsl_debug_console.h"
    2. #include "pin_mux.h"
    3. #include "fsl_fxls.h"
    4. #include "fsl_i2c.h"
    5. #include "fsl_common.h"
    6. #include "fsl_gpio.h"
    7. #include "fsl_port.h"
    8. #include "i2c/bsp_i2c.h"
    9. #include "math.h"


    10. /* I2C source clock */
    11. #define I2C_BAUDRATE 100000U

    12. #define I2C_RELEASE_SDA_PORT  PORTE
    13. #define I2C_RELEASE_SCL_PORT  PORTE
    14. #define I2C_RELEASE_SDA_GPIO  GPIOE
    15. #define I2C_RELEASE_SDA_PIN   25U
    16. #define I2C_RELEASE_SCL_GPIO  GPIOE
    17. #define I2C_RELEASE_SCL_PIN   24U
    18. #define I2C_RELEASE_BUS_COUNT 100U
    19. /* Upper bound and lower bound angle values */
    20. #define ANGLE_UPPER_BOUND 85U
    21. #define ANGLE_LOWER_BOUND 5U




    22. #define I2C_FXLS8974_BASEADDR   I2C0
    23. #define I2C_FXLS8974_CLOCK_FREQ CLOCK_GetFreq(I2C0_CLK_SRC)

    24. const uint8_t fxls8974_address = 0x18U;

    25. static void i2c_release_bus_delay(void)
    26. {
    27.     uint32_t i = 0;
    28.     for (i = 0; i < I2C_RELEASE_BUS_COUNT; i++)
    29.     {
    30.         __NOP();
    31.     }
    32. }

    33. void BOARD_I2C_ReleaseBus(void)
    34. {
    35.     uint8_t i = 0;
    36.     gpio_pin_config_t pin_config;
    37.     port_pin_config_t i2c_pin_config = {0};

    38.     /* Config pin mux as gpio */
    39.     i2c_pin_config.pullSelect = kPORT_PullUp;
    40.     i2c_pin_config.mux        = kPORT_MuxAsGpio;

    41.     pin_config.pinDirection = kGPIO_DigitalOutput;
    42.     pin_config.outputLogic  = 1U;
    43.     CLOCK_EnableClock(kCLOCK_PortE);
    44.     PORT_SetPinConfig(I2C_RELEASE_SCL_PORT, I2C_RELEASE_SCL_PIN, &i2c_pin_config);
    45.     PORT_SetPinConfig(I2C_RELEASE_SDA_PORT, I2C_RELEASE_SDA_PIN, &i2c_pin_config);

    46.     GPIO_PinInit(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, &pin_config);
    47.     GPIO_PinInit(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, &pin_config);

    48.     /* Drive SDA low first to simulate a start */
    49.     GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 0U);
    50.     i2c_release_bus_delay();

    51.     /* Send 9 pulses on SCL and keep SDA high */
    52.     for (i = 0; i < 9; i++)
    53.     {
    54.         GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 0U);
    55.         i2c_release_bus_delay();

    56.         GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 1U);
    57.         i2c_release_bus_delay();

    58.         GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 1U);
    59.         i2c_release_bus_delay();
    60.         i2c_release_bus_delay();
    61.     }

    62.     /* Send stop */
    63.     GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 0U);
    64.     i2c_release_bus_delay();

    65.     GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 0U);
    66.     i2c_release_bus_delay();

    67.     GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 1U);
    68.     i2c_release_bus_delay();

    69.     GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 1U);
    70.     i2c_release_bus_delay();
    71. }


    72. //初始化i2c
    73. void I2C_Init(I2C_Type *base, uint32_t clkSrc_Hz)
    74. {
    75.     i2c_master_config_t i2cConfig = {0};

    76.     I2C_MasterGetDefaultConfig(&i2cConfig);
    77.     I2C_MasterInit(base, &i2cConfig, clkSrc_Hz);
    78. }

    79. status_t I2C_Send(I2C_Type *base,
    80.                         uint8_t deviceAddress,
    81.                         uint32_t subAddress,
    82.                         uint8_t subaddressSize,
    83.                         uint8_t *txBuff,
    84.                         uint8_t txBuffSize)
    85. {
    86.     i2c_master_transfer_t masterXfer;

    87.     /* Prepare transfer structure. */
    88.     masterXfer.slaveAddress   = deviceAddress;
    89.     masterXfer.direction      = kI2C_Write;
    90.     masterXfer.subaddress     = subAddress;
    91.     masterXfer.subaddressSize = subaddressSize;
    92.     masterXfer.data           = txBuff;
    93.     masterXfer.dataSize       = txBuffSize;
    94.     masterXfer.flags          = kI2C_TransferDefaultFlag;

    95.     return I2C_MasterTransferBlocking(base, &masterXfer);
    96. }

    97. status_t I2C_Receive(I2C_Type *base,
    98.                            uint8_t deviceAddress,
    99.                            uint32_t subAddress,
    100.                            uint8_t subaddressSize,
    101.                            uint8_t *rxBuff,
    102.                            uint8_t rxBuffSize)
    103. {
    104.     i2c_master_transfer_t masterXfer;

    105.     /* Prepare transfer structure. */
    106.     masterXfer.slaveAddress   = deviceAddress;
    107.     masterXfer.subaddress     = subAddress;
    108.     masterXfer.subaddressSize = subaddressSize;
    109.     masterXfer.data           = rxBuff;
    110.     masterXfer.dataSize       = rxBuffSize;
    111.     masterXfer.direction      = kI2C_Read;
    112.     masterXfer.flags          = kI2C_TransferDefaultFlag;

    113.     return I2C_MasterTransferBlocking(base, &masterXfer);
    114. }


    115. void I2C_FXLS8974_Init(I2C_Type *base, uint32_t clkSrc_Hz)
    116. {
    117.     I2C_Init(base, clkSrc_Hz);
    118. }

    119. status_t I2C_FXLS8974_Send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff)
    120. {
    121.     uint8_t data = (uint8_t)txBuff;

    122.     return I2C_Send(I2C_FXLS8974_BASEADDR, deviceAddress, subAddress, subaddressSize, &data, 1);
    123. }


    124. status_t I2C_FXLS8974_Receive(
    125.     uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
    126. {
    127.     return I2C_Receive(I2C_FXLS8974_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
    128. }


    129. fxls_handle_t fxlsHandle    = {0};
    130. fxls_accel_data_t accelData = {0};
    131. fxls_config_t config        = {0};
    132. int16_t xAngle          = 0;
    133. int16_t yAngle          = 0;
    134. int16_t zAngle          = 0;
    135. int16_t xDuty           = 0;
    136. int16_t yDuty           = 0;
    137. int16_t zDuty           = 0;
    138. uint8_t sensorRange     = 0;
    139. uint8_t dataScale       = 0;

    140. void init_fxls8974(void)
    141. {

    142.         status_t result;
    143.         BOARD_I2C_ReleaseBus();
    144.         init_i2c0_pins();
    145.         
    146.         //I2C0初始化
    147.         I2C_FXLS8974_Init(I2C_FXLS8974_BASEADDR, I2C_FXLS8974_CLOCK_FREQ);
    148.         
    149.         /* Configure the I2C function */
    150.         config.I2C_SendFunc    = I2C_FXLS8974_Send;
    151.         config.I2C_ReceiveFunc = I2C_FXLS8974_Receive;
    152.         config.slaveAddress    = fxls8974_address;
    153.         
    154.         /* Initialize sensor devices */
    155.         result = FXLS_Init(&fxlsHandle, &config);

    156.         if (result != kStatus_Success)
    157.         {
    158.                         PRINTF("\r\nSensor device initialize failed!\r\n");
    159.                         //return -1;
    160.         }
    161.         /* Get sensor range */
    162.         if (FXLS_ReadReg(&fxlsHandle, SENS_CONFIG1_REG, &sensorRange, 1) != kStatus_Success)
    163.         {
    164.                         PRINTF("\r\nSensor Get sensor range failed!\r\n");
    165.         }

    166.         sensorRange = (sensorRange & 0x6) >> 1;
    167.         //全量程测量范围选择
    168.         if (sensorRange == 0x00)
    169.         {
    170.                         dataScale = 2U;
    171.         }
    172.         else if (sensorRange == 0x01)
    173.         {
    174.                         dataScale = 4U;
    175.         }
    176.         else if (sensorRange == 0x02)
    177.         {
    178.                         dataScale = 8U;
    179.         }
    180.         else if (sensorRange == 0x03)
    181.         {
    182.                         dataScale = 16U;
    183.         }
    184.         else
    185.         {
    186.         }
    187.         
    188. }
    189. void test_fxls8974(void)
    190. {
    191.         /* Get new accelerometer data. */
    192.         FXLS_ReadAccelData(&fxlsHandle, &accelData);
    193.         

    194.         /* Convert raw data to angle (normalize to 0-90 degrees). No negative angles. */
    195.         xAngle = (int16_t)floor((double)accelData.accelX * (double)dataScale * 90 / 2048);
    196.         if (xAngle < 0)
    197.         {
    198.                         xAngle *= -1;
    199.         }

    200.         yAngle = (int16_t)floor((double)accelData.accelY * (double)dataScale * 90 / 2048);
    201.         if (yAngle < 0)
    202.         {
    203.                         yAngle *= -1;
    204.         }

    205.         
    206.         /* Update duty cycle to turn on LEDs when angles ~ 90 */
    207.         if (xAngle > ANGLE_UPPER_BOUND)
    208.         {
    209.                         xDuty = 100;
    210.         }
    211.         if (yAngle > ANGLE_UPPER_BOUND)
    212.         {
    213.                         yDuty = 100;
    214.         }
    215.         /* Update duty cycle to turn off LEDs when angles ~ 0 */
    216.         if (xAngle < ANGLE_LOWER_BOUND)
    217.         {
    218.                         xDuty = 0;
    219.         }
    220.         if (yAngle < ANGLE_LOWER_BOUND)
    221.         {
    222.                         yDuty = 0;
    223.         }

    224.                 /* Print out the angle data. */
    225.                 PRINTF("x= %2d y = %2d\r\n", xAngle, yAngle);
    226. }
    复制代码

    2.2、fxls8974.h
    1. #ifndef __FXLS8974_H
    2. #define __FXLS8974_H

    3. void init_fxls8974(void);
    4. void test_fxls8974(void);

    5. #endif
    复制代码

    2.3、main.c

    1. #include "pin_mux.h"
    2. #include "clock_config.h"
    3. #include "board.h"
    4. #include "fsl_debug_console.h"
    5. #include "fsl_gpio.h"
    6. #include "slcd/slcd.h"
    7. #include "led/led.h"
    8. #include "systick/systick.h"
    9. #include "slcd_engine.h"
    10. //#include "key/key.h"
    11. #include "adc/adc.h"
    12. #include "i2c/eeprom.h"
    13. #include "i2c/fxls8974.h"
    14. #include "fsl_uart.h"
    15. #include "stdio.h"


    16. void init_board(void)
    17. {
    18.         
    19.         SysTick_Init();
    20.         BOARD_InitPins();
    21.         BOARD_BootClockRUN();
    22.         BOARD_InitDebugConsole();
    23.         //init_led();
    24.         //init_key();
    25.         //init_adc();
    26.         //init_slcd();
    27.         //init_eeprom();
    28.         init_fxls8974();
    29.         //        PRINTF("\r\n MCXC444 Board Test\r\n");
    30. }

    31. int main(void)
    32. {
    33.         uint32_t m_adv=0;
    34.         float vl= 0.0f;
    35.         
    36.         init_board();
    37.         

    38.         while (1)
    39.         {
    40.                 test_fxls8974();
    41.                 SysTick_Delay_ms(100);
    42.         }
    43. }
    复制代码

    三、运行结果

    下载程序后,串口输出X和Y方向的数据。
    100.png
    哎...今天够累的,签到来了~
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-10-1 09:39 , Processed in 0.081927 second(s), 20 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表