BMI160

1.硬件spi的初始化
需要注意的是BMI160支持 2种模式,即:CPOL=0 and CPHA = 0;或者CPOL=1 and CPHA = 1。
2. 第一步读取chipid,看spi通讯是否正常。
第二步设置加速度和陀螺仪的量程;
第三步读取加速度和陀螺仪的数据。
此处没用中断。

void bmi160_init(void)
{   
    uint8_t ret = 0,i,imu_data[12]; 
    uint16_t acc_x,acc_y,acc_z;
    uint16_t gyr_x,gyr_y,gyr_z;
    //Read chip_id
    ret =  BMI160_Read_Reg(0x00);//0xD1
    if(ret != 0xD1){
        ret = 0;
    }
    //Config
    BMI160_Write_Reg( 0x41, 0x05);//acc_range
    ret =  BMI160_Read_Reg(0x41);
    if(ret != 0x05){
        ret = 0;
    }
    BMI160_Write_Reg( 0x43, 0x01);//gyr_range
    ret =  BMI160_Read_Reg(0x43);
    if(ret != 0x01){
        ret = 0;
    }
    while(1){
    //Read acc and gyr data
    for(i=0;i<12;i++){
        imu_data[i] = BMI160_Read_Reg(0x0C+i);//gyr acc
    }
    acc_x = imu_data[1]<<8 | imu_data[0];
    acc_y = imu_data[3]<<8 | imu_data[2];
    acc_z = imu_data[5]<<8 | imu_data[4];
    gyr_x = imu_data[7]<<8 | imu_data[6];
    gyr_y = imu_data[9]<<8 | imu_data[8];
    gyr_z = imu_data[11]<<8 | imu_data[10];
    ret  = 1;
}
uint8_t BMI160_Read_Reg(uint8_t reg)
{   
    uint8_t reg_val;     
    BMI160_CS = 0;        
    delay_ms(1);    
    SPI1_ReadWriteByte(reg | 0x80);  
    reg_val=SPI1_ReadWriteByte(0xFF);
    delay_ms(1);  
    BMI160_CS = 1;        
    return(reg_val);       
}

uint8_t BMI160_Write_Reg(uint8_t reg,uint8_t value)
{
    uint8_t status;  
    BMI160_CS=0;                 
    status =SPI1_ReadWriteByte(reg & 0x7f);
    SPI1_ReadWriteByte(value);     
    BMI160_CS=1;                
    return(status);               
}

你可能感兴趣的:(VR/AR)