【MEMS传感器】BMI160九轴传感器SPI驱动

BMI160为九轴传感器,这里只获取加速度与陀螺的原始数据。

初始化调用

        BMI160_Init();

获取数据调用

	BMI160_GetACC();
	BMI160_GetGRO();

头文件:

#ifndef BMI160_H_
#define BMI160_H_

#include "include.h"

#define BMI160 1

#if BMI160
struct sensor_map
{ 
uint8_t Acc_conf ;
uint8_t Acc_range ;
};
#endif


void BMI160_Read_ID(void);
void BMI160_Restart(void);
void BMI160_Init(void);
void BMI160_config(void);//配置 ODR resolution BW
void BMI160_GetGRO(void);
void BMI160_GetACC(void);


#endif /* BMI160_H_ */

源文件:

#include "BMI160.h"
uint8_t  read_BMI160_ID=0xff;

struct sensor_map BMI160_reg={
	.Acc_conf =0x40,
	.Acc_range =0x41,

};

void BMI160_Read_ID(void)
{
	uint8_t temp_who_am_i,num;
	for (num=0;num<4;num++)
	{
		SPI_master_read_register(0x00,1,&temp_who_am_i);
		if(temp_who_am_i == 0xD1)
		{
			printf("who-am-i = %d,  =0x%02x\r\n",temp_who_am_i,temp_who_am_i);break;
		}
		else if(num==3 && temp_who_am_i != 0xD1)
		{
			read_BMI160_ID = false;
			printf("who-am-i = %d,  =0x%02x\r\n",temp_who_am_i,temp_who_am_i);
			printf("NO BMI160\r\n");
		}
	}
}

void BMI160_Restart(void)
{
	
	
}


void BMI160_Init(void)
{
	BMI160_Read_ID();
	if (read_BMI160_ID!=0)
	{
		BMI160_Restart();
	}
	delay_ms(10);
	BMI160_config();
	delay_ms(20);
}


void BMI160_config(void)//配置 ODR resolution BW
{
	uint8_t temp,ch ;
	temp=0x26;
	SPI_master_write_register(0x40, 1, &temp);		//ACC	ODR:25Hz		acc_bwp=3db(defult:acc_us=0b0)	
	temp=0x0C;
	SPI_master_write_register(0x41, 1, &temp);		//Acc_range:16g
	temp=0x26;
	SPI_master_write_register(0x42, 1, &temp);		//Gro	ODR:25Hz		gro_bwp=3db	
	temp=0x03;
	SPI_master_write_register(0x43, 1, &temp);		//Gro_range:250dps
	//FIFO  Config
	temp=0xfe;
	SPI_master_write_register(0x47, 1, &temp);		//enable
	//Set PMU mode	Register(0x7E) CMD		attention the command
	temp=0x11;
	SPI_master_write_register(0x7E, 1, &temp);		//Acc normal mode
	temp=0x15;
	SPI_master_write_register(0x7E, 1, &temp);		//Gro normal mode
	//check the PMU_status	Register(0x03) 
	SPI_master_read_register(0x03,1,&ch);
	if (ch == 0x14)
	{
		printf("sensor is Normal \r\n");
	}
}


void BMI160_GetGRO(void)
{
	uint8_t buf[6]={0};
	int16_t gry[3]={0};
	SPI_master_read_register(0x0c,6,&buf);
	gry[0] = (int16_t)((buf[0])  | (int16_t)buf[1]	<< 8);
	gry[1] = (int16_t)((buf[2])  | (int16_t)buf[3]	<< 8);
	gry[2] = (int16_t)((buf[4])  | (int16_t)buf[5]	<< 8);
	printf("Gro,%d,%d,%d\r\n",gry[0],gry[1],gry[2]);
}
void BMI160_GetACC(void)
{
	uint8_t buf[6]={0};
	int16_t Acc[3]={0};
	SPI_master_read_register(0x12,6,&buf);
	Acc[0] = (int16_t)((buf[0])  | (int16_t)buf[1]	<< 8);
	Acc[1] = (int16_t)((buf[2])  | (int16_t)buf[3]	<< 8);
	Acc[2] = (int16_t)((buf[4])  | (int16_t)buf[5]	<< 8);
	printf("Acc,%d,%d,%d\r\n",Acc[0],Acc[1],Acc[2]);
}

猜你喜欢

转载自blog.csdn.net/putiputiti/article/details/80828865