打印

K60调试HMC5883l用i2c通信,求解

[复制链接]
195|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
第十代火影|  楼主 | 2019-11-22 16:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#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("接收到的数据为:%d\t\t",Acr);

        time_delay_ms(1000);
    }

}

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

363

主题

363

帖子

0

粉丝