这个芯片纯国产挺好用的,电路很好设计,我这垃圾焊功,纯手焊,,居然能用。
第一部分 硬件连接
画的很简陋,看看就可以了,这里I2C总线需要接10K上拉没有画出来,这个需要注意一下。
第二部分 软件驱动
I2C的具体时序实现需要自己搞定!!
2.1 I2C读写寄存器功能
c
u8 QMI8658_ReadReg(u8 dAddr, u8 Reg)
{
u8 Value = 0;
//1.启动IIC
IIC_Start();
//2.写设备地址
IIC_Send_Byte((dAddr << 1)|0);
//3.wait
if(IIC_Wait_Ack())
{
IIC_Stop();
return 1;
}
//4.寄存器地址
IIC_Send_Byte(Reg);
//5.wait
if(IIC_Wait_Ack())
{
IIC_Stop();
return 1;
}
//6.启动IIC
IIC_Start();
//7.设备地址
IIC_Send_Byte((dAddr << 1)|1);
//8.wait read ack
if(IIC_Wait_Ack())
{
IIC_Stop();
return 1;
}
Value = IIC_Read_Byte(1);
//9.stop
IIC_Stop();
return Value;
}
void QMI8658_WriteReg(u8 dAddr, u8 Reg, u8 Value)
{
//1.启动IIC
IIC_Start();
//2.写设备地址
IIC_Send_Byte((dAddr << 1)|0);
//3.wait
while(IIC_Wait_Ack());
//4.写寄存器地址
IIC_Send_Byte(Reg);
//5.wait
IIC_Wait_Ack();
//6.写value
IIC_Send_Byte(Value);
//7.wait
IIC_Wait_Ack();
//8.stop
IIC_Stop();
}
2.2 qmi8658驱动部分
c
void QMI8658_Init()
{
u8 uChipID = 0;
u16 AX=0,AY=0,AZ=0,GX=0,GY=0,GZ=0;
// 1.Device Reset & check Device
QMI8658_WriteReg(QMI8658_DEV,CTRL9,0xA2); //PowerReset
delay_ms(2000); // wait stable
uChipID = QMI8658_ReadReg(QMI8658_DEV,0x00); // Read ID
while(uChipID != 0x5) delay_ms(2000); // device check
printf("QMC5883 check Pass!:deviceID = 0x%x \r\n",uChipID);
//2.Device Settings
QMI8658_WriteReg(QMI8658_DEV,CTRL1,0x40); //地址自增,小端
QMI8658_WriteReg(QMI8658_DEV,CTRL2,0x33); //配置加速度计 量程+-16g,采样率ODR==1kHZ
QMI8658_WriteReg(QMI8658_DEV,CTRL3,0x73); //配置陀螺仪 量程±2048 dps ,采样率是940HZ
QMI8658_WriteReg(QMI8658_DEV,CTRL5,0x0); //不使用低通滤波器
QMI8658_WriteReg(QMI8658_DEV,CTRL6,0x0); //不使用Motion on Demand
QMI8658_WriteReg(QMI8658_DEV,CTRL7,0x03); // 使能加速度计和陀螺仪
delay_ms(2000);
for(;;){
AX = (u16)QMI8658_ReadReg(QMI8658_DEV,AX_H);
AX = AX<<8;
AX = AX | (u16)QMI8658_ReadReg(QMI8658_DEV,AX_L);
AY = (u16)QMI8658_ReadReg(QMI8658_DEV,AY_H);
AY = AY<<8;
AY = AY | (u16)QMI8658_ReadReg(QMI8658_DEV,AY_L);
AZ = (u16)QMI8658_ReadReg(QMI8658_DEV,AZ_H);
AZ = AZ<<8;
AZ = AZ | (u16)QMI8658_ReadReg(QMI8658_DEV,AZ_L);
GX = (u16)QMI8658_ReadReg(QMI8658_DEV,GX_H);
GX = GX<<8;
GX = GX | (u16)QMI8658_ReadReg(QMI8658_DEV,GX_L);
GY = (u16)QMI8658_ReadReg(QMI8658_DEV,GY_H);
GY = GY<<8;
GY = GY | (u16)QMI8658_ReadReg(QMI8658_DEV,GY_L);
GZ = (u16)QMI8658_ReadReg(QMI8658_DEV,GZ_H);
GZ = GZ<<8;
GZ = GZ | (u16)QMI8658_ReadReg(QMI8658_DEV,GZ_L);
printf("QMC5883 GX=%d\r\n", GX);
printf("QMC5883 GY=%d\r\n", GY);
printf("QMC5883 GZ=%d\r\n", GZ);
delay_ms(60000);
}
}
最后!!!注意,这里跑出来的数值是没有经过二阶低通滤波的,芯片是带这个功能的。可以配置