วันเสาร์ที่ 6 ตุลาคม พ.ศ. 2555

HMC6352 + UniconBoard

โมดูลเข็มทิศขนาดเล็กเพียง 11.7x14.9x13.65 มม.

ดาวน์โหลดข้อมูล เอกสารการใช้งาน




 Unicon Board




ArduinoCode 

#include <Wire.h>

int HMC6352Address = 0x42; //address ของ HMC6352 ที่ทางโรงงานนั้นกำหนดมาคือ 0x42 
int slaveAddress;
int ledPin = 31;
boolean ledState = false;
byte headingData[2];
int i, headingValue;

void setup()
{
// Shift the device's documented slave address (0x42) 1 bit right
// This compensates for how the TWI library only wants the
// 7 most significant bits (with the high bit padded with 0)
slaveAddress = HMC6352Address >> 1;   // This results in 0x21 as the address to pass to TWI
Serial.begin(9600);
pinMode(ledPin, OUTPUT);      // Set the LED pin as output
Wire.begin();
}


void loop()
{
  // Flash the LED on pin 13 just to show that something is happening   //unicon pin 31
  // Also serves as an indication that we're not "stuck" waiting for TWI data
  ledState = !ledState;
  if (ledState) {
    digitalWrite(ledPin,HIGH);
  }
  else
  {
    digitalWrite(ledPin,LOW);
  }
  // Send a "A" command to the HMC6352
  // This requests the current heading data
  Wire.beginTransmission(slaveAddress);
  Wire.write("A");        // The "Get Data" command
  Wire.endTransmission();
  delay(10);             // The HMC6352 needs at least a 70us (microsecond) delay
  
  // หลังจากที่ได้ส่ง A ซึ่งเป็นคำสั่งให้โมดูลส่ง Output ออกมาเราจะก็จะทำการอ่านค่า Output โดยใช้ Heading Mode ของตัวไอซี
// ตัวอย่าง เราจะอ่านค่าออกมาเป็น 1345    ค่าองศาอซิมุสจะเท่ากับ   134.5 degrees

  Wire.requestFrom(slaveAddress, 2); // Request the 2 byte heading (MSB comes first)
  i = 0;
  while(Wire.available() && i < 2)
  { 
    headingData[i] = Wire.read();
    i++;
  }

  headingValue = headingData[0]*256 + headingData[1];  // Put the MSB and LSB together
  Serial.print("Current heading: ");
  Serial.print(int (headingValue / 10));  // The whole number part of the heading
  Serial.print(".");
  Serial.print(int (headingValue % 10));  // The fractional part of the heading
  Serial.println(" degrees");
  delay(100);
}







ไม่มีความคิดเห็น:

แสดงความคิดเห็น