stm32f103 spi 口读取 ICM42600 C 语言的代码
时间: 2023-08-22 13:06:42 浏览: 161
STM32F103的程序代码
以下是基于STM32F103的SPI口读取ICM42600的C语言代码示例:
```c
#include "stm32f10x.h"
#include "spi.h"
#define ICM42600_CS_HIGH() GPIO_SetBits(GPIOA, GPIO_Pin_4)
#define ICM42600_CS_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_4)
/* ICM42600 定义 */
#define ICM42600_REG_WHOAMI 0x00
#define ICM42600_REG_ACCEL_X 0x02
#define ICM42600_REG_GYRO_X 0x0C
/* SPI口定义 */
#define SPIx SPI1
#define SPIx_CLK RCC_APB2Periph_SPI1
#define SPIx_GPIO GPIOA
#define SPIx_GPIO_CLK RCC_APB2Periph_GPIOA
#define SPIx_PIN_SCK GPIO_Pin_5
#define SPIx_PIN_MISO GPIO_Pin_6
#define SPIx_PIN_MOSI GPIO_Pin_7
#define SPIx_CS_PIN GPIO_Pin_4
void ICM42600_init(void)
{
// 初始化SPI口
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(SPIx_GPIO_CLK | SPIx_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = SPIx_PIN_SCK | SPIx_PIN_MOSI;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(SPIx_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPIx_PIN_MISO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(SPIx_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPIx_CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(SPIx_GPIO, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPIx, &SPI_InitStructure);
SPI_Cmd(SPIx, ENABLE);
ICM42600_CS_HIGH();
}
uint8_t ICM42600_read_reg(uint8_t reg_addr)
{
uint8_t data = 0;
ICM42600_CS_LOW();
SPIx->DR = reg_addr | 0x80;
while((SPIx->SR & SPI_I2S_FLAG_TXE) == RESET);
while((SPIx->SR & SPI_I2S_FLAG_RXNE) == RESET);
data = SPIx->DR;
SPIx->DR = 0x00;
while((SPIx->SR & SPI_I2S_FLAG_TXE) == RESET);
while((SPIx->SR & SPI_I2S_FLAG_RXNE) == RESET);
data = SPIx->DR;
ICM42600_CS_HIGH();
return data;
}
void ICM42600_read_accel(int16_t *accel)
{
uint8_t data[6];
ICM42600_CS_LOW();
SPIx->DR = ICM42600_REG_ACCEL_X | 0x80;
while((SPIx->SR & SPI_I2S_FLAG_TXE) == RESET);
while((SPIx->SR & SPI_I2S_FLAG_RXNE) == RESET);
SPIx->DR;
for(int i=0; i<6; i++)
{
SPIx->DR = 0x00;
while((SPIx->SR & SPI_I2S_FLAG_TXE) == RESET);
while((SPIx->SR & SPI_I2S_FLAG_RXNE) == RESET);
data[i] = SPIx->DR;
}
ICM42600_CS_HIGH();
accel[0] = (int16_t)(data[1] | (data[0] << 8));
accel[1] = (int16_t)(data[3] | (data[2] << 8));
accel[2] = (int16_t)(data[5] | (data[4] << 8));
}
void ICM42600_read_gyro(int16_t *gyro)
{
uint8_t data[6];
ICM42600_CS_LOW();
SPIx->DR = ICM42600_REG_GYRO_X | 0x80;
while((SPIx->SR & SPI_I2S_FLAG_TXE) == RESET);
while((SPIx->SR & SPI_I2S_FLAG_RXNE) == RESET);
SPIx->DR;
for(int i=0; i<6; i++)
{
SPIx->DR = 0x00;
while((SPIx->SR & SPI_I2S_FLAG_TXE) == RESET);
while((SPIx->SR & SPI_I2S_FLAG_RXNE) == RESET);
data[i] = SPIx->DR;
}
ICM42600_CS_HIGH();
gyro[0] = (int16_t)(data[1] | (data[0] << 8));
gyro[1] = (int16_t)(data[3] | (data[2] << 8));
gyro[2] = (int16_t)(data[5] | (data[4] << 8));
}
```
此代码初始化了SPI口和ICM42600设备,并提供了读取陀螺仪和加速度计数据的函数。在函数中,通过SPI口写入命令和地址来读取设备寄存器中的数据,并根据ICM42600的数据格式解析数据。
阅读全文