嗨,我正在将 LSM6DS 与 nrf52 接口以读取加速器和陀螺仪。我得到的 accl 值是正确的,但不是陀螺仪的值,因为当传感器稳定时,陀螺仪的值为 0 或 255,而它在我移动时显示值。
这里是陀螺仪的初始化代码
////init lsm
$queue
这会读取 accl 和陀螺仪的值,这是我卡住的地方,因为 accl 显示正确的值,陀螺仪也显示正确,但仅在我移动它时。 是否有任何缓冲区或我想念的东西
uint8_t LSM_init() {
uint8_t data = 0;
data = 0x01;
LSM_REGISTER_RW(0x12, &data, 0);
nrf_delay_ms(1);
m_tx_buf[0] = 0x8F; //who am i
m_tx_buf[1] = 0x40;
memset(m_rx_buf, 0, 3);
nrf_gpio_pin_clear(LSM_CS);
nrf_drv_spi_transfer(&spi, m_tx_buf, 2, m_rx_buf, 2);
nrf_delay_ms(5);
nrf_gpio_pin_set(LSM_CS);
nrf_delay_ms(5);
if (m_rx_buf[1] == 0x6C) {
m_tx_buf[0] = 0x12; //auto increment 1 and enable BDU
m_tx_buf[1] = 0x64;
memset(m_rx_buf, 0, 3);
nrf_gpio_pin_clear(LSM_CS);
nrf_drv_spi_transfer(&spi, m_tx_buf, 2, m_rx_buf, 2);
nrf_delay_ms(5);
nrf_gpio_pin_set(LSM_CS);
nrf_delay_ms(5);
m_tx_buf[0] = 0x0A; // fifo cntrlby pass
m_tx_buf[1] = 0x00;
memset(m_rx_buf, 0, 3);
nrf_gpio_pin_clear(LSM_CS);
nrf_drv_spi_transfer(&spi, m_tx_buf, 2, m_rx_buf, 2);
nrf_delay_ms(5);
nrf_gpio_pin_set(LSM_CS);
nrf_delay_ms(5);
m_tx_buf[0] = 0x10; // Xl odr 416 htz
m_tx_buf[1] = 0x62; //4A //62
memset(m_rx_buf, 0, 3);
nrf_gpio_pin_clear(LSM_CS);
nrf_drv_spi_transfer(&spi, m_tx_buf, 2, m_rx_buf, 2);
nrf_delay_ms(5);
nrf_gpio_pin_set(LSM_CS);
nrf_delay_ms(5);
data = 0x00;
LSM_REGISTER_RW(0x5C, &data, 0); //wakeup duration
data = 0x01;
LSM_REGISTER_RW(0x5D, &data, 0); //threshhold
// m_tx_buf[0] = 0x11; // gyroscope
// m_tx_buf[1] = 0x00;
// memset(m_rx_buf, 0, 3);
// nrf_gpio_pin_clear(LSM_CS);
// nrf_drv_spi_transfer(&spi, m_tx_buf, 2, m_rx_buf, 2);
// nrf_delay_ms(5);
// nrf_gpio_pin_set(LSM_CS);
// nrf_delay_ms(5);
m_tx_buf[0] = 0x11; // gyroscope
m_tx_buf[1] = 0x6E;
memset(m_rx_buf, 0, 3);
nrf_gpio_pin_clear(LSM_CS);
nrf_drv_spi_transfer(&spi, m_tx_buf, 2, m_rx_buf, 2);
nrf_delay_ms(5);
nrf_gpio_pin_set(LSM_CS);
nrf_delay_ms(5);
data = 0xF0;
LSM_REGISTER_RW(0x14, &data, 0); //enable lsm.
// data = 0x20;
// LSM_REGISTER_RW(0x66, &data, 0); //enable lsm.
//interrupt route
data = 0x80;
LSM_REGISTER_RW(0x01, &data, 0); //embedded func set -
data = 0x01;
LSM_REGISTER_RW(0x0D, &data, 0); //LSM6DSOX_MLC_INT1
data = 0x80;
LSM_REGISTER_RW(0x0A, &data, 0); //LSM6DSOX_EMB_FUNC_INT1
data = 0x01;
LSM_REGISTER_RW(0x0B, &data, 0); //LSM6DSOX_FSM_INT1_A
data = 0x00;
LSM_REGISTER_RW(0x0C, &data, 0); //LSM6DSOX_FSM_INT1_B
data = 0x01;
LSM_REGISTER_RW(0x01, &data, 0); //user func
//pull down disable int1
data = 0x01;
LSM_REGISTER_RW(0x62, &data, 0); //I3C_BUS_AVB
data = 0x41; //01 41
LSM_REGISTER_RW(0x56, &data, 0); //TAP_CFG0
data = 0x12;
LSM_REGISTER_RW(0x5E, &data, 0); //md1fg = int1_ff set :LSM6DSOX_MD1_CFG or md1_cfg.int1_emb_func = PROPERTY_ENABLE
data = 0x41;
LSM_REGISTER_RW(0x56, &data, 0); //tap config0
data = 0x20;
LSM_REGISTER_RW(0x0D, &data, 0); //LSM6DSOX_INT1_CTRL write
data = 0x80;
LSM_REGISTER_RW(0x58, &data, 0); //TAP_CFG2 write 0x80
///////////////////////////////////////////////////////
return 0;
} else {
return 1;
}
}
///reading code in main func