Forked from Jose R. Padron and Aaron Berk's library, customized for my specific application using 9DoF-Stick by Sparkfun.

Fork of HMC5843 by Jose R Padron

HMC5843 is triple axis, digital interface compass (geomagnetic sensor).

This library is forked from Jose R. Padron and Aaron Berk's work.

This library is for specific application using 9DoF-Stick.

Datasheet:

http://www.sparkfun.com/datasheets/Sensors/Magneto/HMC5843.pdf

HMC5843 は3軸のデジタルインターフェースを備えたコンパス(地磁気センサ)です。

このライブラリは 9DoF-Stick を使用した特定の企画のために保守しています。

mbed IDEが日本語をサポートするまでは英語でコメントを書いていきますが、サポートした後もきっと英語で書いていくでしょう。

Files at this revision

API Documentation at this revision

Comitter:
gltest26
Date:
Sun Sep 09 17:26:31 2012 +0000
Parent:
2:fdab96fc6fff
Child:
4:7a90125eedcf
Commit message:
Fixed a bug getData() have returned incorrect negative values.

Changed in this revision

HMC5843.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/HMC5843.cpp	Tue Nov 09 23:18:46 2010 +0000
+++ b/HMC5843.cpp	Sun Sep 09 17:26:31 2012 +0000
@@ -112,25 +112,38 @@
 
   
     char tx[1];
-    char rx[2];
+/*    char rx[2];
     
     
     tx[0]=HMC5843_X_MSB;
     i2c_->write(HMC5843_I2C_READ,tx,1);
     i2c_->read(HMC5843_I2C_READ,rx,2);
-    readings[0]= (int)rx[0]<<8|(int)rx[1];
+    readings[0]= (int)int16_t(uint16_t(rx[0])<<8|uint16_t(rx[1]));
 
      
     tx[0]=HMC5843_Y_MSB;
     i2c_->write(HMC5843_I2C_READ,tx,1);
     i2c_->read(HMC5843_I2C_READ,rx,2);
-    readings[1]= (int)rx[0]<<8|(int)rx[1];
+    readings[1]= (int)int16_t(uint16_t(rx[0])<<8|uint16_t(rx[1]));
      
     tx[0]=HMC5843_Z_MSB;
     i2c_->write(HMC5843_I2C_READ,tx,1);
     i2c_->read(HMC5843_I2C_READ,rx,2);
-    readings[2]= (int)rx[0]<<8|(int)rx[1];
-    
+    readings[2]= (int)int16_t(uint16_t(rx[0])<<8|uint16_t(rx[1]));*/
+
+    // Burst read all registers to enhance bus speed.
+    tx[0]=HMC5843_X_MSB;
+    char rx[6];
+    i2c_->write(HMC5843_I2C_READ, tx, 1);
+    i2c_->read(HMC5843_I2C_READ, rx, 6);
+
+    // Readings are expressed in 16bit 2's complement, so we must first
+    // concatenate two bytes to make a word and sign extend it to obtain
+    // correct negative values.
+    readings[0] = int(int16_t(uint16_t(rx[0]) << 8 | uint16_t(rx[1])));
+    readings[1] = int(int16_t(uint16_t(rx[2]) << 8 | uint16_t(rx[3])));
+    readings[2] = int(int16_t(uint16_t(rx[4]) << 8 | uint16_t(rx[5])));
+
 }
 
 int HMC5843::getMx() {