这个芯片纯国产挺好用的,电路很好设计,我这垃圾焊功,纯手焊,,居然能用。
第一部分 硬件连接
画的很简陋,看看就可以了。
第二部分 软件驱动
I2C的具体时序实现需要自己搞定!!
2.1 I2C读写寄存器功能
u8 QMC5883_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();
}
//4.寄存器地址
IIC_Send_Byte(Reg);
//5.wait
if(IIC_Wait_Ack())
{
IIC_Stop();
}
//6.启动IIC
IIC_Start();
//7.设备地址
IIC_Send_Byte((dAddr << 1)|1);
//8.wait read ack
if(IIC_Wait_Ack())
{
IIC_Stop();
}
Value = IIC_Read_Byte(0);
//9.stop
IIC_Stop();
return Value;
}
void QMC5883_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 驱动部分
void QMC5883_Init()
{
u8 uChipID = 0;
// 1.Device Reset & check Device
//QMC5883_WriteReg(QMC5883_DEV,0x0A,0x41); //PowerReset
QMC5883_WriteReg(QMC5883_DEV,0x0B,0x01); //PowerReset
QMC5883_WriteReg(QMC5883_DEV,0x20,0x40); //PowerReset
QMC5883_WriteReg(QMC5883_DEV,0x21,0x01); //PowerReset
QMC5883_WriteReg(QMC5883_DEV,0x09,0x0d); //PowerReset
delay_ms(5000); // wait stable
while( uChipID!= 0xFF)
{
uChipID = QMC5883_ReadReg(QMC5883_DEV,0x0D); // Read ID
delay_ms(2000); // device check
}
printf("QMC5883 check Pass!:deviceID = 0x%x \r\n",uChipID);
}
void QMC5883_ReadMuti(u8 dAddr, u8 Len, u8 Reg, u8* Buff)
{
u8 i = 0;
for(i = 0;i<Len; i++)
{
*(Buff+i) = QMC5883_ReadReg(dAddr,Reg+i);
}
}
void QMC5883_dataGet(void)
{
QMC5883_ReadMuti(QMC5883_DEV,6,0x0,uQMC5883_Data.buffer);// 读取数据部分是前六个寄存器0x0~0x5
printf("%d %d %d ", \
(s16)(uQMC5883_Data.stData.AX), (s16)(uQMC5883_Data.stData.AY), (s16)(uQMC5883_Data.stData.AZ));
/*printf("AX=%d AY=%d AZ=%d ", \
(s16)(uQMC5883_Data.stData.AX), (s16)(uQMC5883_Data.stData.AY), (s16)(uQMC5883_Data.stData.AZ));*/
}