//测试代码
#include "typedef.h"
#include "nuc970_system.h"
#include "stdlib.h"
#include "main.h"
#include "test.h"
#include "QMC5883L.h"
#include "SoftwareIIC.h"
#include "iic.h"
SIIC_HANDLE g_IIC_Handle;
QMC5883L_HANDLE g_QMC5883L_Handle;
#define QMC5883L_IIC_CH IIC_CH0
#define IS_TEST_HARDWAVE_IIC 1 //是否测试硬件IIC
//IIC读取寄存器接口
bool QMC5883L_IIC_ReadReg(u8 SlaveAddr, u8 RegAddr, u8 *pDataBuff, u16 ByteNum)
{
#if(IS_TEST_HARDWAVE_IIC) //测试硬件IIC
IIC_ERROR Error;
Error = IIC_MasterReadReg(QMC5883L_IIC_CH, SlaveAddr, RegAddr, TRUE, pDataBuff, ByteNum); //IIC读取寄存器(可以读取1个或者多个寄存器)
if(Error != IIC_OK)
{
uart_printf("[硬件IIC]R错误:%d\r\n", Error);
return FALSE;
}
else return TRUE;
#else //软件IIC
return SIIC_ReadReg(&g_IIC_Handle, SlaveAddr, RegAddr, TRUE, pDataBuff, ByteNum); //软件IIC读取寄存器(可以读取1个或者多个寄存器)
#endif //IS_TEST_HARDWAVE_IIC
}
//IIC写入寄存器接口
bool QMC5883L_IIC_WriteReg(u16 SlaveAddr, u8 RegAddr, u8 *pDataBuff, u16 ByteNum)
{
#if(IS_TEST_HARDWAVE_IIC) //测试硬件IIC
IIC_ERROR Error;
Error = IIC_MasterWriteReg(QMC5883L_IIC_CH, SlaveAddr, RegAddr, TRUE, pDataBuff, ByteNum); //IIC写寄存器(可以写1个或者多个寄存器)
if(Error != IIC_OK)
{
uart_printf("[硬件IIC]W错误:%d\r\n", Error);
return FALSE;
}
else return TRUE;
#else //软件IIC
return SIIC_WriteReg(&g_IIC_Handle, SlaveAddr, RegAddr, TRUE, pDataBuff, ByteNum); //软件IIC写寄存器(可以写1个或者多个寄存器)
#endif //IS_TEST_HARDWAVE_IIC
}
//QMC5883L0测试
void qmc5883l_test(void)
{
s16 Temp;
s16 MagneticData[3];
#if(IS_TEST_HARDWAVE_IIC) //测试硬件IIC
IIC_Init(QMC5883L_IIC_CH, 100, 0); //硬件IIC初始化
#else //软件IIC
SIIC_Init(&g_IIC_Handle, GPIOG, GPIOG, 1, 0, 10); //软件IIC初始化
#endif //IS_TEST_HARDWAVE_IIC
while(QMC5883L_Init(&g_QMC5883L_Handle, 0x1A, QMC5883L_IIC_ReadReg, QMC5883L_IIC_WriteReg) == FALSE) //QMC5883L初始化
{
Sleep(1000);
}
while(1)
{
if(QMC5883L_GetTemper(&g_QMC5883L_Handle, &Temp) == TRUE) //MPU6050读取温度
{
uart_printf("[QMC5883L]温度:%s%d.%02d℃\r\n",(Temp<0)?"-":"", abs(Temp)/100, abs(Temp)%100);
}
else
{
uart_printf("[QMC5883L]温度:ERROR\r\n");
}
if(QMC5883L_GetMagneticData(&g_QMC5883L_Handle, &MagneticData[0], &MagneticData[1] , &MagneticData[2]) == TRUE) //QMC5883L读取磁力计原始数据
{
uart_printf("[QMC5883L]磁力计:%d %d %d\r\n", MagneticData[0],MagneticData[1],MagneticData[2]);
}
else
{
uart_printf("[QMC5883L]磁力计:ERROR\r\n");
}
Sleep(500);
}
}
|