久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2642|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

分享一段HMC5883L的代碼 但是不知道為何沒用

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:241138 發(fā)表于 2017-10-20 00:02 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式

#define HMC5883_WriteAddress 0x1E //  i.e 0x3C >> 1
#define HMC5883_ModeRegisterAddress 0x02
#define HMC5883_ContinuousModeCommand 0x00
#define HMC5883_DataOutputXMSBAddress  0x03
#include <Wire.h>
int regb=0x01;
int regbdata=0x40;

int outputData[6];

void setup()
{   
  //Initiate the Wire library and join the I2C bus as a master
    Serial.begin(9600);
    Wire.begin();     

}

void loop() {

    int i,x,y,z;
    double angle;

    Wire.beginTransmission(HMC5883_WriteAddress);
    Wire.write(regb);
    Wire.write(regbdata);
    Wire.endTransmission();

    delay(1000);
    Wire.beginTransmission(HMC5883_WriteAddress); //Initiate a transmission with HMC5883 (Write address).
    Wire.write(HMC5883_ModeRegisterAddress);       //Place the Mode Register Address in send-buffer.
    Wire.write(HMC5883_ContinuousModeCommand);     //Place the command for Continuous operation Mode in send-buffer.
    Wire.endTransmission();                       //Send the send-buffer to HMC5883 and end the I2C transmission.
    delay(100);


    Wire.beginTransmission(HMC5883_WriteAddress);  //Initiate a transmission with HMC5883 (Write address).
    Wire.requestFrom(HMC5883_WriteAddress,6);      //Request 6 bytes of data from the address specified.

    delay(500);


    //Read the value of magnetic components X,Y and Z

    if(6 <= Wire.available()) // If the number of bytes available for reading be <=6.
    {
        for(i=0;i<6;i++)
        {
            outputData=Wire.read();  //Store the data in outputData buffer
        }
    }

    x=outputData[0] << 8 | outputData[1]; //Combine MSB and LSB of X Data output register
    z=outputData[2] << 8 | outputData[3]; //Combine MSB and LSB of Z Data output register
    y=outputData[4] << 8 | outputData[5]; //Combine MSB and LSB of Y Data output register


    angle= atan2((double)y,(double)x) * (180 / 3.14159265) + 180; // angle in degrees

    /*

  Refer the following application note for heading calculation.
  http://www.ssec.honeywell.com/magnetic/datasheets/lowcost.pdf  
  ----------------------------------------------------------------------------------------
  atan2(y, x) is the angle in radians between the positive x-axis of a plane and the point
  given by the coordinates (x, y) on it.
  ----------------------------------------------------------------------------------------

  This sketch does not utilize the magnetic component Z as tilt compensation can not be done without an Accelerometer

  ----------------->y
  |
  |
  |
  |
  |
  |
\/
  x



     N
NW  |  NE
     |  
W----------E
     |
SW  |  SE
     S

*/


    //Print the approximate direction

    Serial.print("You are heading ");
    if((angle < 22.5) || (angle > 337.5 ))
        Serial.print("South");
    if((angle > 22.5) && (angle < 67.5 ))
        Serial.print("South-West");
    if((angle > 67.5) && (angle < 112.5 ))
        Serial.print("West");
    if((angle > 112.5) && (angle < 157.5 ))
        Serial.print("North-West");
    if((angle > 157.5) && (angle < 202.5 ))
        Serial.print("North");
    if((angle > 202.5) && (angle < 247.5 ))
        Serial.print("NorthEast");
    if((angle > 247.5) && (angle < 292.5 ))
        Serial.print("East");
    if((angle > 292.5) && (angle < 337.5 ))
        Serial.print("SouthEast");

    Serial.print(": Angle between X-axis and the South direction ");
    if((0 < angle) && (angle < 180) )
    {
        angle=angle;
    }
    else
    {
        angle=360-angle;
    }
    Serial.print(angle,2);
    Serial.println(" Deg");
    delay(100);
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 国产福利在线播放麻豆 | 亚洲综合资源 | 欧洲一区二区视频 | 久久久国产一区二区三区 | www.干| a级大片免费观看 | 亚洲成人国产精品 | 日日射夜夜骑 | 国产精品久久久久久 | 亚洲精品二三区 | 久久精品免费观看 | 欧美lesbianxxxxhd视频社区 | 国产成人精品免高潮在线观看 | 欧美黄a | 国产福利一区二区 | 免费亚洲成人 | 狠狠操狠狠干 | 欧美一卡二卡在线 | 中文字幕在线欧美 | 成人免费视频观看视频 | h片在线观看免费 | 国产精品久久九九 | 日本精品久久久一区二区三区 | 成人久草 | 天天操夜夜爽 | 欧美黄色一区 | 国产玖玖 | 久精品久久 | 在线综合视频 | 国产清纯白嫩初高生在线播放视频 | 一级毛片免费看 | 国产一区在线看 | 精品国产乱码久久久久久a丨 | 久久99精品久久久久久秒播九色 | 亚洲人在线 | 一级黄在线观看 | 亚洲欧美精品国产一级在线 | 日本在线综合 | 久草网在线视频 | 综合久| 国产一区 |