本文聊聊上海矽睿产的加速度传感器QMA6100P的使用。
1、特性
高集成,小尺寸封装:2 x 2 x 0.95 mm LGA
14位数模转化,低噪声 ·
具有标准模式和快速模式,支持I2C和SPI接口
内置self-test
大范围操作电压1.7V-3.6V,与低功耗2~50μA
集成64位FIFO存贮器,符合RoHS标准,无卤素添加
内置运动算法 ,尤其是硬件计步算法
可在-40°C~85°C的温度中工作
2、引脚说明
可以看出提供了两个外部中断引脚,支持SPI或I2C通信。
3、I2C读写地址
4、部分寄存器
4.1、设备ID寄存器
可以通过读这个寄存器判断设备是否存在,默认是0x90。
4.2、软复位寄存器
4.3、数据寄存器
通过这6个寄存器可以读出三轴数据。
4.4、量程寄存器
通过这个寄存器设置测量范围。
4.5、带宽寄存器
4.6、电源管理寄存器
5、原理图设计
6、代码
本文使用软硬件I2C与QMA6100P通讯,通过宏切换。
void I2cInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF , ENABLE);
/* Connect PXx to I2C_SCL*/
GPIO_PinAFConfig(GPIOF, GPIO_PinSource6, GPIO_AF_1);
/* Connect PXx to I2C_SDA*/
GPIO_PinAFConfig(GPIOF, GPIO_PinSource7, GPIO_AF_1);
/* GPIO configuration */
/* Configure sEE_I2C pins: SCL */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* Configure sEE_I2C pins: SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOF, &GPIO_InitStructure);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStructure.I2C_DigitalFilter = 0x00;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_Timing = 0x40B22536;//0x20D22E37;// 0x02105070; //0x40B22536//100k, 0x10950C27; //400kHz
I2C_Init(I2C2, &I2C_InitStructure);
/* Apply sEE_I2C configuration after enabling it */
I2C_Init(I2C2, &I2C_InitStructure);
/* sEE_I2C Peripheral Enable */
I2C_Cmd(I2C2, ENABLE);
}
void I2cWrite(uint8_t reg,uint8_t var)
{
/* While the bus is busy */
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY) != RESET){};
/* Send Touch address for write */
I2C_TransferHandling(I2C2, (SLAVE_ADDR<<1), 1, I2C_Reload_Mode, I2C_Generate_Start_Write);
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TXIS )==RESET){}; // I2C_FLAG_TXE
I2C_SendData(I2C2, reg);
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TCR) == RESET){};
I2C_TransferHandling(I2C2, (SLAVE_ADDR<<1), 1, I2C_AutoEnd_Mode, I2C_No_StartStop);
/* Test on EV8 and clear it */
while (I2C_GetFlagStatus(I2C2, I2C_FLAG_TXIS) == RESET){}; //I2C_FLAG_TXIS
/* Send the current byte */
I2C_SendData(I2C2, var);
/* Send STOP condition */
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF) == RESET){};
}
uint8_t I2cRead(uint8_t reg)
{
uint8_t ret=0;
/* While the bus is busy */
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY) != RESET){};
/* Generate start & wait event detection */
I2C_TransferHandling(I2C2, (SLAVE_ADDR<<1), 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TXIS ) == RESET){}; //I2C_FLAG_TXIS
I2C_SendData(I2C2, reg);
while (I2C_GetFlagStatus(I2C2, I2C_FLAG_TC) == RESET){};
/* Send STRAT condition a second time */
I2C_TransferHandling(I2C2, (SLAVE_ADDR<<1), 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_RXNE) == RESET){};
/* Read a byte from the EEPROM */
ret = I2C_ReceiveData(I2C2);
/* Enable Acknowledgement to be ready for another reception */
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF) == RESET){};
return ret;
}
#define _SCL_PORT GPIOF
#define _SCL_PIN GPIO_Pin_6
#define _SDA_PORT GPIOF
#define _SDA_PIN GPIO_Pin_7
void _I2cInit(void)
{
/* sEE_I2C_SCL_GPIO_CLK and sEE_I2C_SDA_GPIO_CLK Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF , ENABLE);
/* sEE_I2C Periph clock enable */
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIO configuration */
/* Configure sEE_I2C pins: SCL */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* Configure sEE_I2C pins: SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOF, &GPIO_InitStructure);
}
void _SDA_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIO configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
}
void _SDA_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIO configuration */
/* Configure sEE_I2C pins: SCL */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOF, &GPIO_InitStructure);
}
void _I2C_Start(void)
{
_SDA_OUT();
GPIO_SetBits(_SDA_PORT,_SDA_PIN); //SDA=1
DelayUs(10);
GPIO_SetBits(_SCL_PORT,_SCL_PIN); //SCL=1
DelayUs(10);
GPIO_ResetBits(_SDA_PORT,_SDA_PIN);//SDA=0
DelayUs(10);
GPIO_ResetBits(_SCL_PORT,_SCL_PIN);//SCL=0
DelayUs(10);
}
void _I2C_Stop(void)
{
_SDA_OUT();
GPIO_ResetBits(_SDA_PORT,_SDA_PIN);//SDA=0
DelayUs(10);
GPIO_SetBits(_SCL_PORT,_SCL_PIN); //SCL=1
DelayUs(10);
GPIO_SetBits(_SDA_PORT,_SDA_PIN); //SDA=1
DelayUs(10);
}
void _I2C_Ack(void)
{
_SDA_OUT();
GPIO_ResetBits(_SDA_PORT,_SDA_PIN);//SDA=0
DelayUs(5);
GPIO_SetBits(_SCL_PORT,_SCL_PIN); //SCL=1
DelayUs(5);
GPIO_ResetBits(_SCL_PORT,_SCL_PIN); //SCL=0
}
void _I2C_NAck(void)
{
GPIO_SetBits(_SDA_PORT,_SDA_PIN); //SDA=1
DelayUs(10);
GPIO_SetBits(_SCL_PORT,_SCL_PIN); //SCL=1
DelayUs(10);
GPIO_ResetBits(_SCL_PORT,_SCL_PIN); //SCL=0
DelayUs(10);
}
uint8_t _I2C_Wait_Ack(void)
{
uint8_t ucErrTime=0;
#if 0
SDA_IN();
GPIO_SetBits(_SDA_PORT,_SDA_PIN); //释放总线
#else
GPIO_SetBits(_SDA_PORT,_SDA_PIN); //释放总线
_SDA_IN();
#endif
DelayUs(5);
GPIO_SetBits(_SCL_PORT,_SCL_PIN); //SCL=1
DelayUs(5);
while(GPIO_ReadInputDataBit(_SDA_PORT,_SDA_PIN))
{
ucErrTime++;
if(ucErrTime>250)
{
_I2C_Stop();
return 1;
}
}
GPIO_ResetBits(_SCL_PORT,_SCL_PIN); //SCL=0
DelayUs(5);
return 0;
}
uint8_t _I2C_Read_Byte(uint8_t ack)
{
uint8_t i,rxdata=0;
#if 0
_SDA_IN();
GPIO_SetBits(_SDA_PORT,_SDA_PIN); //释放总线
#else
GPIO_SetBits(_SDA_PORT,_SDA_PIN); //释放总线
_SDA_IN();
#endif
for(i=0;i<8;i++ )
{
GPIO_ResetBits(_SCL_PORT,_SCL_PIN); //SCL=0
DelayUs(5);
GPIO_SetBits(_SCL_PORT,_SCL_PIN); //SCL=1
DelayUs(5);
rxdata<<=1;
if(GPIO_ReadInputDataBit(_SDA_PORT,_SDA_PIN))
{
rxdata|=0x01;
}
DelayUs(5);
}
if (!ack)
_I2C_NAck();//nACK
else
_I2C_Ack(); //ACK
return rxdata;
}
void _I2C_Send_Byte(uint8_t txd)
{
uint8_t i;
_SDA_OUT();
GPIO_ResetBits(_SCL_PORT,_SCL_PIN); //SCL=0
for(i=0;i<8;i++)
{
if((txd&0x80)==0x80)
GPIO_SetBits(_SDA_PORT,_SDA_PIN);
else
GPIO_ResetBits(_SDA_PORT,_SDA_PIN);
txd<<=1;
DelayUs(5);
GPIO_SetBits(_SCL_PORT,_SCL_PIN); //SCL=1
DelayUs(5);
GPIO_ResetBits(_SCL_PORT,_SCL_PIN); //SCL=0
DelayUs(5);
}
}
void _WriteByteReg(uint8_t addr,uint8_t reg,uint8_t data)
{
_I2C_Start();
_I2C_Send_Byte(addr); //write addr
_I2C_Wait_Ack();
_I2C_Send_Byte(reg);
_I2C_Wait_Ack();
_I2C_Send_Byte(data);
_I2C_Wait_Ack();
_I2C_Stop();
}
uint8_t _ReadByteReg(uint8_t addr,uint8_t reg)
{
uint8_t data;
_I2C_Start();
_I2C_Send_Byte(addr); //write addr
_I2C_Wait_Ack();
_I2C_Send_Byte(reg);
_I2C_Wait_Ack();
_I2C_Start();
_I2C_Send_Byte(addr+1); //read addr
_I2C_Wait_Ack();
data=_I2C_Read_Byte(0); //no ack
_I2C_Stop();
return data;
}
void _WriteRegU8(uint8_t reg,uint8_t data)
{
_I2C_Start();
_I2C_Send_Byte(SLAVE_ADDR<<1|0x00); //write addr
_I2C_Wait_Ack();
_I2C_Send_Byte(reg);
_I2C_Wait_Ack();
_I2C_Send_Byte(data);
_I2C_Wait_Ack();
_I2C_Stop();
}
uint8_t _ReadRegU8(uint8_t reg)
{
uint8_t data;
_I2C_Start();
_I2C_Send_Byte(SLAVE_ADDR<<1|0x00); //write addr
_I2C_Wait_Ack();
_I2C_Send_Byte(reg);
_I2C_Wait_Ack();
_I2C_Start();
_I2C_Send_Byte(SLAVE_ADDR<<1|0x01); //read addr
_I2C_Wait_Ack();
data=_I2C_Read_Byte(0); //no ack
_I2C_Stop();
return data;
}
#if 1
#define GsensorRead I2cRead
#define GsensorWrite I2cWrite
#define GsensorIoInit I2cInit
#else
#define GsensorRead _ReadRegU8
#define GsensorWrite _WriteRegU8
#define GsensorIoInit _I2cInit
#endif
void QMA6100PCheck(void)
{
uint8_t chip_id=0;
chip_id=GsensorRead(QMA6100P_REG_CHIP_ID);
log_debug ("chip id=%d\r\n",chip_id);
if(chip_id==0x90)
{
log_debug("device exist\r\n");
}
else
{
log_debug("device not exist\r\n");
}
}
void QMA6100PInit(void)
{
GsensorIoInit();
QMA6100PCheck();
GsensorWrite(QMA6100P_REG_RESET, 0xb6);
DelayMs(5);
GsensorWrite(QMA6100P_REG_RESET, 0x00);
DelayMs(10);
GsensorWrite(0x11, 0x80);
GsensorWrite(0x11, 0x84);
GsensorWrite(0x4a, 0x20);
GsensorWrite(0x56, 0x01);
GsensorWrite(0x5f, 0x80);
DelayMs(2);
GsensorWrite(0x5f, 0x00);
DelayMs(10);
GsensorWrite(QMA6100P_REG_RANGE,QMA6100P_RANGE_8G);
GsensorWrite(QMA6100P_REG_BW_ODR,QMA6100P_BW_100);
GsensorWrite(QMA6100P_REG_POWER_MANAGE,QMA6100P_MCLK_51_2K|0x80);
qma6100p_hand_raise_down(3, QMA6100P_MAP_INT1, 1);
GsensorWrite(0x21, 0x03);// default 0x1c, step latch mode
}
void QMA6100PReadRawData(QMA6100PRawData_t *rawdata)
{
int16_t temp=0;
temp=GsensorRead(QMA6100P_REG_XOUTL)+(GsensorRead(QMA6100P_REG_XOUTH)<<8);
rawdata->acc_x=temp>>2;
temp=GsensorRead(QMA6100P_REG_YOUTL)+(GsensorRead(QMA6100P_REG_YOUTH)<<8);
rawdata->acc_y=temp>>2;
temp=GsensorRead(QMA6100P_REG_ZOUTL)+(GsensorRead(QMA6100P_REG_ZOUTH)<<8);
rawdata->acc_z=temp>>2;
}
void QMA6100PPolling(void)
{
static uint32_t curtick=0;
float angle_x,angle_y,angle_z;
if(SystemGetTick()-curtick>=200)
{
QMA6100PRawData_t rawdata;
float x ,y,z,g;
QMA6100PReadRawData(&rawdata);
x=rawdata.acc_x*QMA6100P_SENSITITY_8G/1000.0;
y=rawdata.acc_y*QMA6100P_SENSITITY_8G/1000.0;
z=rawdata.acc_z*QMA6100P_SENSITITY_8G/1000.0;
g=sqrt(x*x+y*y+z*z);
log_debug("%f, %f, %f, %f\r\n",x,y,z,g);
curtick=SystemGetTick();
}
}
#define SLAVE_ADDR QMA6100P_ADDRESS
#define QMA6100P_ADDRESS 0x12
#define QMA6100P_DEVICE_ID0x90
#define QMA6100P_REG_CHIP_ID 0x00
#define QMA6100P_REG_XOUTL0x01
#define QMA6100P_REG_XOUTH0x02
#define QMA6100P_REG_YOUTL0x03
#define QMA6100P_REG_YOUTH0x04
#define QMA6100P_REG_ZOUTL0x05
#define QMA6100P_REG_ZOUTH0x06
#define QMA6100P_REG_STEP_CNT_L0x07
#define QMA6100P_REG_STEP_CNT_M0x08
#define QMA6100P_REG_STEP_CNT_H0x0d
#define QMA6100P_REG_INT_STATUS_00x09
#define QMA6100P_REG_INT_STATUS_10x0a
#define QMA6100P_REG_INT_STATUS_20x0b
#define QMA6100P_REG_INT_STATUS_30x0c
#define QMA6100P_REG_FIFO_STATE0x0e
#define QMA6100P_REG_RANGE0x0f
#define QMA6100P_REG_BW_ODR0x10
#define QMA6100P_REG_POWER_MANAGE0x11
#define QMA6100P_REG_STEP_SAMPLE_CNT0x12
#define QMA6100P_REG_STEP_PRECISION0x13
#define QMA6100P_REG_STEP_TIME_LOW0x14
#define QMA6100P_REG_STEP_TIME_UP0x15
#define QMA6100P_REG_INT_EN_00x16
#define QMA6100P_REG_INT_EN_10x17
#define QMA6100P_REG_INT_EN_20x18
#define QMA6100P_REG_INT1_MAP_00x19
#define QMA6100P_REG_INT1_MAP_10x1a
#define QMA6100P_REG_INT2_MAP_00x1b
#define QMA6100P_REG_INT2_MAP_10x1c
#define QMA6100P_REG_INTPIN_CFG0x20
#define QMA6100P_REG_INT_CFG0x21
#define QMA6100P_REG_OS_CUST_X 0x27
#define QMA6100P_REG_OS_CUST_Y0x28
#define QMA6100P_REG_OS_CUST_Z0x29
#define QMA6100P_REG_NVM0x33
#define QMA6100P_REG_RESET0x36
#define QMA6100P_REG_DRDY_BIT0x10// enable 1
#define QMA6100P_REG_AMD_X_BIT0x01
#define QMA6100P_REG_AMD_Y_BIT0x02
#define QMA6100P_REG_AMD_Z_BIT0x04
typedef enum
{
QMA6100P_MAP_INT1,
QMA6100P_MAP_INT2,
QMA6100P_MAP_INT_NONE
}qma6100p_int_map;
typedef enum
{
QMA6100P_BW_100 = 0,
QMA6100P_BW_200 = 1,
QMA6100P_BW_400 = 2,
QMA6100P_BW_800 = 3,
QMA6100P_BW_1600 = 4,
QMA6100P_BW_50 = 5,
QMA6100P_BW_25 = 6,
QMA6100P_BW_12_5 = 7,
QMA6100P_BW_OTHER = 8
}qma6100p_bw;
typedef enum
{
QMA6100P_RANGE_2G = 0x01,
QMA6100P_RANGE_4G = 0x02,
QMA6100P_RANGE_8G = 0x04,
QMA6100P_RANGE_16G = 0x08,
QMA6100P_RANGE_32G = 0x0f
}qma6100p_range;
typedef enum
{
QMA6100P_LPF_OFF = (0x00<<5),
QMA6100P_LPF_1 = (0x04<<5),
QMA6100P_LPF_2 = (0x01<<5),
QMA6100P_LPF_4 = (0x02<<5),
QMA6100P_LPF_8 = (0x03<<5),
QMA6100P_LPF_RESERVED = 0xff
}qma6100p_nlpf;
typedef enum
{
QMA6100P_HPF_DIV_OFF = (0x00<<5),
QMA6100P_HPF_DIV_10 = (0x01<<5),
QMA6100P_HPF_DIV_25 = (0x02<<5),
QMA6100P_HPF_DIV_50 = (0x03<<5),
QMA6100P_HPF_DIV_100 = (0x04<<5),
QMA6100P_HPF_DIV_200 = (0x05<<5),
QMA6100P_HPF_DIV_400 = (0x06<<5),
QMA6100P_HPF_DIV_800 = (0x07<<5),
QMA6100P_HPF_RESERVED = 0xff
}qma6100p_nhpf;
typedef enum
{
QMA6100P_MODE_STANDBY = 0,
QMA6100P_MODE_ACTIVE = 1,
QMA6100P_MODE_MAX
}qma6100p_mode;
typedef enum
{
QMA6100P_MCLK_102_4K = 0x03,
QMA6100P_MCLK_51_2K = 0x04,
QMA6100P_MCLK_25_6K = 0x05,
QMA6100P_MCLK_12_8K = 0x06,
QMA6100P_MCLK_6_4K = 0x07,
QMA6100P_MCLK_RESERVED = 0xff
}qma6100p_mclk;
typedef enum
{
QMA6100P_STEP_LPF_0 = (0x00<<6),
QMA6100P_STEP_LPF_2 = (0x01<<6),
QMA6100P_STEP_LPF_4 = (0x02<<6),
QMA6100P_STEP_LPF_8 = (0x03<<6),
QMA6100P_STEP_LPF_RESERVED = 0xff
}qma6100p_step_lpf;
typedef enum
{
QMA6100P_STEP_AXIS_ALL = 0x00,
QMA6100P_STEP_AXIS_YZ = 0x01,
QMA6100P_STEP_AXIS_XZ = 0x02,
QMA6100P_STEP_AXIS_XY = 0x03,
QMA6100P_STEP_AXIS_RESERVED = 0xff
}qma6100p_step_axis;
typedef enum
{
QMA6100P_STEP_START_0 = 0x00,
QMA6100P_STEP_START_4 = 0x20,
QMA6100P_STEP_START_8 = 0x40,
QMA6100P_STEP_START_12 = 0x60,
QMA6100P_STEP_START_16 = 0x80,
QMA6100P_STEP_START_24 = 0xa0,
QMA6100P_STEP_START_32 = 0xc0,
QMA6100P_STEP_START_40 = 0xe0,
QMA6100P_STEP_START_RESERVED = 0xff
}qma6100p_step_start_cnt;
typedef enum
{
QMA6100P_FIFO_MODE_NONE,
QMA6100P_FIFO_MODE_FIFO,
QMA6100P_FIFO_MODE_STREAM,
QMA6100P_FIFO_MODE_BYPASS,
QMA6100P_FIFO_MODE_MAX
}qma6100p_fifo_mode;
typedef enum
{
QMA6100P_TAP_SINGLE = 0x80,
QMA6100P_TAP_DOUBLE = 0x20,
QMA6100P_TAP_TRIPLE = 0x10,
QMA6100P_TAP_QUARTER = 0x01,
QMA6100P_TAP_MAX = 0xff
}qma6100p_tap;
typedef enum
{
QMA6100P_SENSITITY_2G = 244,
QMA6100P_SENSITITY_4G = 488,
QMA6100P_SENSITITY_8G = 977,
QMA6100P_SENSITITY_16G = 1950,
QMA6100P_SENSITITY_32G = 3910
}qma6100p_sensitity;
typedef struct {
int16_t temp;
int16_t acc_x;
int16_t acc_y;
int16_t acc_z;
}QMA6100PRawData_t;
7、读取数据
本测试中读取了x,y,z三轴的值,并计算出了g的值。
-
传感器
+关注
关注
2550文章
51035浏览量
753083 -
寄存器
+关注
关注
31文章
5336浏览量
120232 -
加速度传感器
+关注
关注
12文章
471浏览量
55140
发布评论请先 登录
相关推荐
评论