ICM-42688-P 库例程
时间: 2023-07-07 07:40:40 浏览: 1189
ICM-42688-P-6轴姿态传感器 STM32F103VET6 示例代码
5星 · 资源好评率100%
以下是使用 STM32 HAL 库编写的 ICM-42688-P 库例程,包括初始化、读取传感器数据等功能。
库文件:icm42688p.h 和 icm42688p.c
icm42688p.h:
```c
#ifndef __ICM42688P_H
#define __ICM42688P_H
#include "stm32f1xx_hal.h"
#include <stdint.h>
#define ICM42688_SPI_HANDLE hspi1
#define ICM42688_CS_GPIO GPIOA
#define ICM42688_CS_PIN GPIO_PIN_4
typedef struct
{
float x;
float y;
float z;
} ICM42688_AccData_t;
typedef struct
{
float x;
float y;
float z;
} ICM42688_GyroData_t;
void ICM42688_Init(void);
void ICM42688_ReadAccData(ICM42688_AccData_t *accData);
void ICM42688_ReadGyroData(ICM42688_GyroData_t *gyroData);
#endif /* __ICM42688P_H */
```
icm42688p.c:
```c
#include "icm42688p.h"
#define ICM42688_STATUS 0x2A
#define ICM42688_ACCEL_DATA_X1 0x2D
#define ICM42688_GYRO_DATA_X1 0x33
#define ICM42688_WHO_AM_I 0x75
#define ICM42688_WHO_AM_I_RESULT 0x49
#define SPI_TIMEOUT 1000
SPI_HandleTypeDef ICM42688_SPI_HANDLE;
void ICM42688_SPI_CS_LOW(void)
{
HAL_GPIO_WritePin(ICM42688_CS_GPIO, ICM42688_CS_PIN, GPIO_PIN_RESET);
}
void ICM42688_SPI_CS_HIGH(void)
{
HAL_GPIO_WritePin(ICM42688_CS_GPIO, ICM42688_CS_PIN, GPIO_PIN_SET);
}
uint8_t ICM42688_SPI_Transfer(uint8_t data)
{
uint8_t rx_data;
HAL_SPI_TransmitReceive(&ICM42688_SPI_HANDLE, &data, &rx_data, 1, SPI_TIMEOUT);
return rx_data;
}
void ICM42688_SPI_Read(uint8_t reg_addr, uint8_t *data, uint16_t len)
{
ICM42688_SPI_CS_LOW();
ICM42688_SPI_Transfer(reg_addr | 0x80);
for (uint16_t i = 0; i < len; i++)
{
data[i] = ICM42688_SPI_Transfer(0);
}
ICM42688_SPI_CS_HIGH();
}
void ICM42688_SPI_Write(uint8_t reg_addr, uint8_t data)
{
ICM42688_SPI_CS_LOW();
ICM42688_SPI_Transfer(reg_addr);
ICM42688_SPI_Transfer(data);
ICM42688_SPI_CS_HIGH();
}
uint8_t ICM42688_WHO_AM_I_Test(void)
{
uint8_t who_am_i;
ICM42688_SPI_Read(ICM42688_WHO_AM_I, &who_am_i, 1);
return who_am_i == ICM42688_WHO_AM_I_RESULT;
}
void ICM42688_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
ICM42688_SPI_HANDLE.Instance = SPI1;
ICM42688_SPI_HANDLE.Init.Mode = SPI_MODE_MASTER;
ICM42688_SPI_HANDLE.Init.Direction = SPI_DIRECTION_2LINES;
ICM42688_SPI_HANDLE.Init.DataSize = SPI_DATASIZE_8BIT;
ICM42688_SPI_HANDLE.Init.CLKPolarity = SPI_POLARITY_LOW;
ICM42688_SPI_HANDLE.Init.CLKPhase = SPI_PHASE_1EDGE;
ICM42688_SPI_HANDLE.Init.NSS = SPI_NSS_SOFT;
ICM42688_SPI_HANDLE.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
ICM42688_SPI_HANDLE.Init.FirstBit = SPI_FIRSTBIT_MSB;
ICM42688_SPI_HANDLE.Init.TIMode = SPI_TIMODE_DISABLE;
ICM42688_SPI_HANDLE.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
ICM42688_SPI_HANDLE.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&ICM42688_SPI_HANDLE) != HAL_OK)
{
Error_Handler();
}
GPIO_InitStruct.Pin = ICM42688_CS_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(ICM42688_CS_GPIO, &GPIO_InitStruct);
ICM42688_SPI_CS_HIGH();
}
void ICM42688_ReadAccData(ICM42688_AccData_t *accData)
{
uint8_t data[6];
ICM42688_SPI_Read(ICM42688_ACCEL_DATA_X1, data, 6);
int16_t x = (int16_t)((data[1] << 8) | data[0]);
int16_t y = (int16_t)((data[3] << 8) | data[2]);
int16_t z = (int16_t)((data[5] << 8) | data[4]);
accData->x = (float)x / 16384.0;
accData->y = (float)y / 16384.0;
accData->z = (float)z / 16384.0;
}
void ICM42688_ReadGyroData(ICM42688_GyroData_t *gyroData)
{
uint8_t data[6];
ICM42688_SPI_Read(ICM42688_GYRO_DATA_X1, data, 6);
int16_t x = (int16_t)((data[1] << 8) | data[0]);
int16_t y = (int16_t)((data[3] << 8) | data[2]);
int16_t z = (int16_t)((data[5] << 8) | data[4]);
gyroData->x = (float)x / 131.0;
gyroData->y = (float)y / 131.0;
gyroData->z = (float)z / 131.0;
}
```
在您的主函数中,您可以按照以下方式使用该库:
```c
#include "icm42688p.h"
#include <stdio.h>
int main(void)
{
HAL_Init();
ICM42688_Init();
if (ICM42688_WHO_AM_I_Test())
{
printf("ICM-42688-P is detected.\n");
}
else
{
printf("ICM-42688-P is NOT detected.\n");
}
ICM42688_AccData_t accData;
ICM42688_GyroData_t gyroData;
while (1)
{
ICM42688_ReadAccData(&accData);
printf("Acc Data: x = %.6f, y = %.6f, z = %.6f\n", accData.x, accData.y, accData.z);
ICM42688_ReadGyroData(&gyroData);
printf("Gyro Data: x = %.6f, y = %.6f, z = %.6f\n", gyroData.x, gyroData.y, gyroData.z);
}
}
```
这样,您就可以读取 ICM-42688-P 的加速度和陀螺仪数据,并将其输出到终端。
阅读全文