本帖最后由 1348853047 于 2022-6-24 14:04 编辑
这是读寄存器的逻辑,目前是能读到数据,但是会有数据跳动,所以有调试过的大神帮忙指点一下。
#define IMU_CURRENT_SPI SPI2
#define READ_MODE spi_disable(IMU_CURRENT_SPI); \
spi_bidirectional_transfer_config(IMU_CURRENT_SPI, SPI_BIDIRECTIONAL_RECEIVE); \
spi_enable(IMU_CURRENT_SPI);
#define WRITE_MODE spi_disable(IMU_CURRENT_SPI); \
spi_bidirectional_transfer_config(IMU_CURRENT_SPI, SPI_BIDIRECTIONAL_TRANSMIT); \
spi_enable(IMU_CURRENT_SPI);
uint8_t BMI160_Read_Reg(uint8_t reg)
{
uint8_t data = 0;
uint8_t Retry = 0;
WRITE_MODE;
/* select the flash: chip select low */
SPI2_CS_LOW();
IMU_SPI2_WriteByte(reg|0x80); // 读寄存器时序
READ_MODE;
while(RESET == spi_i2s_flag_get(IMU_CURRENT_SPI, SPI_FLAG_RBNE))
{
if(++Retry > 200) return 0;
}
data = spi_i2s_data_receive(IMU_CURRENT_SPI);
SPI2_CS_HIGH();
WRITE_MODE;
//data = spi_i2s_data_receive(IMU_CURRENT_SPI);
while(spi_i2s_flag_get(IMU_CURRENT_SPI, SPI_FLAG_TRANS))
{
if(++Retry > 200) return 0;
}
return data;
}
|