|
自己写的HMC5983&HMC5883L驱动函数,仿官方库函数写法
[复制链接]
本帖最后由 dacapoday 于 2014-9-11 22:45 编辑
整理了一下暑期的代码,按照官方的格式改写了一下,用法和官方sensehub库里的函数一模一样
HMC5983&HMC5883L这两个三轴电子罗盘比较常见,而官方库中却没有,且库中的地磁仪都偏弱。
蓝本是库中的mpu6050,结合本函数,与mpu6050函数,可以调用库中comp函数得出欧拉角与四元数以下是使用示例 "tm4c123gh6pm.h"
#include "i2c.h"
#include "i2cm_drv.h"
#include "hw_hmc5983.h"
#include "hmc5983.h"
#define HMC5983_I2C_ADDRESS 0x1E
tI2CMInstance g_sI2CInst;//I2C实体
tHMC5983 g_sHMC5983Inst; //HMC5983实体
volatile uint_fast8_t g_vui8I2CDoneFlag;//I2C 操作完成标志
volatile uint_fast8_t g_vui8ErrorFlag;//I2C 错误标志
float X,Y,Z;
void
HMC5983AppCallback(void *pvCallbackData, uint_fast8_t ui8Status){
if(ui8Status == I2CM_STATUS_SUCCESS){
g_vui8I2CDoneFlag = 1;
}
g_vui8ErrorFlag = ui8Status;
}
void
HMC5983I2CIntHandler(void){
I2CMIntHandler(&g_sI2CInst);
}
void
HMC5983AppErrorHandler(void){
while(1){
//Do Nothing
}
}
void
HMC5983AppI2CWait(){
while((g_vui8I2CDoneFlag == 0) && (g_vui8ErrorFlag == 0)){
// Do Nothing
}
if(g_vui8ErrorFlag){ //错误处理
HMC5983AppErrorHandler();
}
g_vui8I2CDoneFlag = 0;
}
void i2c_Init(){
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
ROM_GPIOPinConfigure(GPIO_PB2_I2C0SCL);
ROM_GPIOPinConfigure(GPIO_PB3_I2C0SDA);
GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
ROM_GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
I2CMInit(&g_sI2CInst, I2C0_BASE, INT_I2C0, 0xff, 0xff,ROM_SysCtlClockGet());
}
void HMC5983_Init(){
i2c_Init();
HMC5983Init(&g_sHMC5983Inst, &g_sI2CInst,HMC5983_I2C_ADDRESS,HMC5983AppCallback, &g_sHMC5983Inst);
HMC5983AppI2CWait();
}
uint8_t HMC5983_WhoAmI(){
uint8_t id;
HMC5983Read(&g_sHMC5983Inst, HMC5983_O_ID_A ,&id, 1, HMC5983AppCallback, &g_sHMC5983Inst);
HMC5983AppI2CWait();
return id;
}
void HMC5983_DataGet(float *X,float *Y,float *Z){
HMC5983DataRead(&g_sHMC5983Inst, HMC5983AppCallback, &g_sHMC5983Inst);
HMC5983AppI2CWait();
HMC5983DataMagGetFloat(&g_sHMC5983Inst,X,Y,Z);
}
int main(void){
ROM_FPULazyStackingEnable();
ROM_SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ); //16M
SysCtlDelay(ROM_SysCtlClockGet()/300);
ConfigureUART();
float X,Y,Z;
UARTprintf("\033[2J\033[H");
UARTprintf("\033[37;46mTest HMC5983\033[0m\n");
UARTprintf("X: Y: Z: \033[0G");
ROM_IntMasterEnable();
HMC5983_Init();
for (; ; ){
HMC5983_DataGet(&X,&Y,&Z);
UARTprintf("\033[3G%8d\033[14G%8d\033[25G%8d\033[0G",(int16_t)X,(int16_t)Y,(int16_t)Z);
sysCtlDelay(ROM_SysCtlClockGet()/300);
}
}
|
|