关于STM32F405单片机控制传感器BMI160程序及说明

#include "bmi160.h"
#include "stm32f4_spi.h"
#include "stm32f4_delay.h"
#include "string.h"


#define MAX_ARG_NUM 32 //max of arg num
char bmi160_reg_arg_table[MAX_ARG_NUM][2]={
{0x7e,0x11},
{0x7e,0x15},
{0x7e,0x19},
{0x77,0xc0},//offset enable
{0x69,0x7d},//foc enable
{0x7e,0x03},//trigger foc
//{0x47,0xc0},//fifo enable
{0,0}
};


unsigned char bmi160_read_reg(unsigned char addr,unsigned char *p_read_buff,unsigned int len)
{
unsigned char p_send_buff[64];
unsigned int r = 0;
unsigned char s_addr = addr|0x80;//读需要 |0x80
//unsigned char r_addr = 0;
//p_send_buff = (unsigned char *)malloc(len);
memset(p_send_buff,0,len);
SPI1_Read_Write(s_addr,p_send_buff,p_read_buff,len);
//printf("bmi160_read_reg :0x%x,0x%x\r\n",read_buff[0],read_buff[1]);
return r;
}


unsigned char bmi160_write_reg(unsigned char addr,unsigned char wdata)
{
unsigned char cmd[2] = {0,0};
unsigned char recv[2];
cmd[0] = wdata;
SPI1_Read_Write(addr,cmd,recv,1);
return 0;
}
int bmi160_init(void)
{
unsigned char cmd[2] = {0xff,0xff};
unsigned char recv[2] = {0,0};
unsigned char id = 0,stat = 0;
unsigned int i = 0,r = 0;
SPI1_Read_Write(0xff,cmd,recv,1);


//while(1)
bmi160_read_reg(0x00,&id,1);
//printf("bmi160_init chip id:%x\n", id);


for(i = 0;i < MAX_ARG_NUM;i++)
{
if(0 == bmi160_reg_arg_table[i][0])
break;
bmi160_write_reg(bmi160_reg_arg_table[i][0],bmi160_reg_arg_table[i][1]);
r = bmi160_read_reg(bmi160_reg_arg_table[i][0],&stat,1);
Delay_1ms(10);
//printf("slv:0x%x,reg:0x%0x data:0x%x\n",BMX055_ACC_SLV_ADDR,acc_reg_arg_table[i][0], read_data); //0xfa
}
r = bmi160_read_reg(0x03,&stat,1);
//printf("bmi160_init read stat:%x\n", stat);
r = bmi160_read_reg(0x1b,&stat,1);
//printf("bmi160_init read foc_stat:%x\n", stat);
r = bmi160_read_reg(0x02,&stat,1);
//printf("bmi160_init read err_stat:%x\n", stat);
r = bmi160_read_reg(0x47,&stat,1);
//printf("bmi160_init read err_stat:%x\n", stat);
    return r;
}

你可能感兴趣的:(嵌入式MCU)