#include "common.h"
#include "include.h"
typedef unsigned char uchar;
typedef unsigned int uint;
#define HMC5883_SlaveID 0x1E //定义器件在IIC总线中的从地址
uchar Rec_Data[6];
void Multiple_Read_HMC5883(void)
{
Rec_Data[0]=I2C_ReadAddr(I2C0,HMC5883_SlaveID, 0x03);
Rec_Data[1]=I2C_ReadAddr(I2C0,HMC5883_SlaveID, 0x04);
Rec_Data[2]=I2C_ReadAddr(I2C0,HMC5883_SlaveID, 0x05);
Rec_Data[3]=I2C_ReadAddr(I2C0,HMC5883_SlaveID, 0x06);
Rec_Data[4]=I2C_ReadAddr(I2C0,HMC5883_SlaveID, 0x07);
Rec_Data[5]=I2C_ReadAddr(I2C0,HMC5883_SlaveID, 0x08);
time_delay_ms(100);
}
void main(void)
{
int X,Y,Z;
double Angle;
uint Acr;
I2C_init(I2C0);
while(1)
{
Multiple_Read_HMC5883();//连续读出数据,存储在Rec_Data[]中
X=Rec_Data[0]<<8 | Rec_Data[1];//Combine MSB and LSB of X Data output register
Z=Rec_Data[2]<<8 | Rec_Data[3];//Combine MSB and LSB of Z Data output register
Y=Rec_Data[4]<<8 | Rec_Data[5];//Combine MSB and LSB of Y Data output register
Angle= atan2((double)Y,(double)X)*(180/3.14159265)+180;//单位:角度 (0~360)
Angle*=10;
Acr=(uint)Angle;
printf(&quot;接收到的数据为:%d\t\t&quot;,Acr);
time_delay_ms(1000);
}
} |