在线时间906 小时
UID3067179
注册时间2014-10-23
NXP金币3157
TA的每日心情 | 开心 前天 14:03 |
---|
签到天数: 1536 天 连续签到: 14 天 [LV.Master]伴坛终老
版主
  
- 积分
- 8844
- 最后登录
- 2025-9-29
|
本帖最后由 TLLED 于 2024-11-14 10:26 编辑
测试下板卡上的FXLS8974传感器芯片。
一、硬件电路
1.1、FXLS8974部分电路图
1.2、配置引脚
FXLS8974传感器使用I2C0接口
二、程序部分
2.1、fxls8974.c
- #include "fsl_debug_console.h"
- #include "pin_mux.h"
- #include "fsl_fxls.h"
- #include "fsl_i2c.h"
- #include "fsl_common.h"
- #include "fsl_gpio.h"
- #include "fsl_port.h"
- #include "i2c/bsp_i2c.h"
- #include "math.h"
- /* I2C source clock */
- #define I2C_BAUDRATE 100000U
- #define I2C_RELEASE_SDA_PORT PORTE
- #define I2C_RELEASE_SCL_PORT PORTE
- #define I2C_RELEASE_SDA_GPIO GPIOE
- #define I2C_RELEASE_SDA_PIN 25U
- #define I2C_RELEASE_SCL_GPIO GPIOE
- #define I2C_RELEASE_SCL_PIN 24U
- #define I2C_RELEASE_BUS_COUNT 100U
- /* Upper bound and lower bound angle values */
- #define ANGLE_UPPER_BOUND 85U
- #define ANGLE_LOWER_BOUND 5U
- #define I2C_FXLS8974_BASEADDR I2C0
- #define I2C_FXLS8974_CLOCK_FREQ CLOCK_GetFreq(I2C0_CLK_SRC)
- const uint8_t fxls8974_address = 0x18U;
- static void i2c_release_bus_delay(void)
- {
- uint32_t i = 0;
- for (i = 0; i < I2C_RELEASE_BUS_COUNT; i++)
- {
- __NOP();
- }
- }
- void BOARD_I2C_ReleaseBus(void)
- {
- uint8_t i = 0;
- gpio_pin_config_t pin_config;
- port_pin_config_t i2c_pin_config = {0};
- /* Config pin mux as gpio */
- i2c_pin_config.pullSelect = kPORT_PullUp;
- i2c_pin_config.mux = kPORT_MuxAsGpio;
- pin_config.pinDirection = kGPIO_DigitalOutput;
- pin_config.outputLogic = 1U;
- CLOCK_EnableClock(kCLOCK_PortE);
- PORT_SetPinConfig(I2C_RELEASE_SCL_PORT, I2C_RELEASE_SCL_PIN, &i2c_pin_config);
- PORT_SetPinConfig(I2C_RELEASE_SDA_PORT, I2C_RELEASE_SDA_PIN, &i2c_pin_config);
- GPIO_PinInit(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, &pin_config);
- GPIO_PinInit(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, &pin_config);
- /* Drive SDA low first to simulate a start */
- GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 0U);
- i2c_release_bus_delay();
- /* Send 9 pulses on SCL and keep SDA high */
- for (i = 0; i < 9; i++)
- {
- GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 0U);
- i2c_release_bus_delay();
- GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 1U);
- i2c_release_bus_delay();
- GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 1U);
- i2c_release_bus_delay();
- i2c_release_bus_delay();
- }
- /* Send stop */
- GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 0U);
- i2c_release_bus_delay();
- GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 0U);
- i2c_release_bus_delay();
- GPIO_PinWrite(I2C_RELEASE_SCL_GPIO, I2C_RELEASE_SCL_PIN, 1U);
- i2c_release_bus_delay();
- GPIO_PinWrite(I2C_RELEASE_SDA_GPIO, I2C_RELEASE_SDA_PIN, 1U);
- i2c_release_bus_delay();
- }
- //初始化i2c
- void I2C_Init(I2C_Type *base, uint32_t clkSrc_Hz)
- {
- i2c_master_config_t i2cConfig = {0};
- I2C_MasterGetDefaultConfig(&i2cConfig);
- I2C_MasterInit(base, &i2cConfig, clkSrc_Hz);
- }
- status_t I2C_Send(I2C_Type *base,
- uint8_t deviceAddress,
- uint32_t subAddress,
- uint8_t subaddressSize,
- uint8_t *txBuff,
- uint8_t txBuffSize)
- {
- i2c_master_transfer_t masterXfer;
- /* Prepare transfer structure. */
- masterXfer.slaveAddress = deviceAddress;
- masterXfer.direction = kI2C_Write;
- masterXfer.subaddress = subAddress;
- masterXfer.subaddressSize = subaddressSize;
- masterXfer.data = txBuff;
- masterXfer.dataSize = txBuffSize;
- masterXfer.flags = kI2C_TransferDefaultFlag;
- return I2C_MasterTransferBlocking(base, &masterXfer);
- }
- status_t I2C_Receive(I2C_Type *base,
- uint8_t deviceAddress,
- uint32_t subAddress,
- uint8_t subaddressSize,
- uint8_t *rxBuff,
- uint8_t rxBuffSize)
- {
- i2c_master_transfer_t masterXfer;
- /* Prepare transfer structure. */
- masterXfer.slaveAddress = deviceAddress;
- masterXfer.subaddress = subAddress;
- masterXfer.subaddressSize = subaddressSize;
- masterXfer.data = rxBuff;
- masterXfer.dataSize = rxBuffSize;
- masterXfer.direction = kI2C_Read;
- masterXfer.flags = kI2C_TransferDefaultFlag;
- return I2C_MasterTransferBlocking(base, &masterXfer);
- }
- void I2C_FXLS8974_Init(I2C_Type *base, uint32_t clkSrc_Hz)
- {
- I2C_Init(base, clkSrc_Hz);
- }
- status_t I2C_FXLS8974_Send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff)
- {
- uint8_t data = (uint8_t)txBuff;
- return I2C_Send(I2C_FXLS8974_BASEADDR, deviceAddress, subAddress, subaddressSize, &data, 1);
- }
- status_t I2C_FXLS8974_Receive(
- uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
- {
- return I2C_Receive(I2C_FXLS8974_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
- }
- fxls_handle_t fxlsHandle = {0};
- fxls_accel_data_t accelData = {0};
- fxls_config_t config = {0};
- int16_t xAngle = 0;
- int16_t yAngle = 0;
- int16_t zAngle = 0;
- int16_t xDuty = 0;
- int16_t yDuty = 0;
- int16_t zDuty = 0;
- uint8_t sensorRange = 0;
- uint8_t dataScale = 0;
- void init_fxls8974(void)
- {
- status_t result;
- BOARD_I2C_ReleaseBus();
- init_i2c0_pins();
-
- //I2C0初始化
- I2C_FXLS8974_Init(I2C_FXLS8974_BASEADDR, I2C_FXLS8974_CLOCK_FREQ);
-
- /* Configure the I2C function */
- config.I2C_SendFunc = I2C_FXLS8974_Send;
- config.I2C_ReceiveFunc = I2C_FXLS8974_Receive;
- config.slaveAddress = fxls8974_address;
-
- /* Initialize sensor devices */
- result = FXLS_Init(&fxlsHandle, &config);
- if (result != kStatus_Success)
- {
- PRINTF("\r\nSensor device initialize failed!\r\n");
- //return -1;
- }
- /* Get sensor range */
- if (FXLS_ReadReg(&fxlsHandle, SENS_CONFIG1_REG, &sensorRange, 1) != kStatus_Success)
- {
- PRINTF("\r\nSensor Get sensor range failed!\r\n");
- }
- sensorRange = (sensorRange & 0x6) >> 1;
- //全量程测量范围选择
- if (sensorRange == 0x00)
- {
- dataScale = 2U;
- }
- else if (sensorRange == 0x01)
- {
- dataScale = 4U;
- }
- else if (sensorRange == 0x02)
- {
- dataScale = 8U;
- }
- else if (sensorRange == 0x03)
- {
- dataScale = 16U;
- }
- else
- {
- }
-
- }
- void test_fxls8974(void)
- {
- /* Get new accelerometer data. */
- FXLS_ReadAccelData(&fxlsHandle, &accelData);
-
- /* Convert raw data to angle (normalize to 0-90 degrees). No negative angles. */
- xAngle = (int16_t)floor((double)accelData.accelX * (double)dataScale * 90 / 2048);
- if (xAngle < 0)
- {
- xAngle *= -1;
- }
- yAngle = (int16_t)floor((double)accelData.accelY * (double)dataScale * 90 / 2048);
- if (yAngle < 0)
- {
- yAngle *= -1;
- }
-
- /* Update duty cycle to turn on LEDs when angles ~ 90 */
- if (xAngle > ANGLE_UPPER_BOUND)
- {
- xDuty = 100;
- }
- if (yAngle > ANGLE_UPPER_BOUND)
- {
- yDuty = 100;
- }
- /* Update duty cycle to turn off LEDs when angles ~ 0 */
- if (xAngle < ANGLE_LOWER_BOUND)
- {
- xDuty = 0;
- }
- if (yAngle < ANGLE_LOWER_BOUND)
- {
- yDuty = 0;
- }
- /* Print out the angle data. */
- PRINTF("x= %2d y = %2d\r\n", xAngle, yAngle);
- }
复制代码
2.2、fxls8974.h
- #ifndef __FXLS8974_H
- #define __FXLS8974_H
- void init_fxls8974(void);
- void test_fxls8974(void);
- #endif
复制代码
2.3、main.c
- #include "pin_mux.h"
- #include "clock_config.h"
- #include "board.h"
- #include "fsl_debug_console.h"
- #include "fsl_gpio.h"
- #include "slcd/slcd.h"
- #include "led/led.h"
- #include "systick/systick.h"
- #include "slcd_engine.h"
- //#include "key/key.h"
- #include "adc/adc.h"
- #include "i2c/eeprom.h"
- #include "i2c/fxls8974.h"
- #include "fsl_uart.h"
- #include "stdio.h"
- void init_board(void)
- {
-
- SysTick_Init();
- BOARD_InitPins();
- BOARD_BootClockRUN();
- BOARD_InitDebugConsole();
- //init_led();
- //init_key();
- //init_adc();
- //init_slcd();
- //init_eeprom();
- init_fxls8974();
- // PRINTF("\r\n MCXC444 Board Test\r\n");
- }
- int main(void)
- {
- uint32_t m_adv=0;
- float vl= 0.0f;
-
- init_board();
-
- while (1)
- {
- test_fxls8974();
- SysTick_Delay_ms(100);
- }
- }
复制代码
三、运行结果
下载程序后,串口输出X和Y方向的数据。
|
|