Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MotionSensor mbed
Fork of MAG3110 by
Diff: MAG3110.cpp
- Revision:
- 0:63a8594a3866
- Child:
- 1:5a0e7a58d980
diff -r 000000000000 -r 63a8594a3866 MAG3110.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MAG3110.cpp Sun May 19 20:34:27 2013 +0000
@@ -0,0 +1,87 @@
+
+#include "MAG3110.h"
+#include "mbed.h"
+
+/******************************************************************************
+ * Constructors
+ ******************************************************************************/
+MAG3110::MAG3110(I2C *i2c):
+ _i2c_address(0x1D), _i2c(i2c),_pc(NULL), _debug(false)
+{
+
+}
+
+MAG3110::MAG3110(I2C *i2c, Serial *pc):
+ _i2c_address(0x1D), _i2c(i2c), _pc(pc), _debug(true)
+{
+
+}
+
+// Read a single byte form 8 bit register, return as int
+int MAG3110::readReg(char regAddr)
+{
+ char cmd[1];
+
+ cmd[0] = regAddr;
+ _i2c->write(_i2c_address, cmd, 1);
+
+ cmd[0] = 0x00;
+ _i2c->read(_i2c_address, cmd, 1);
+ return (int)( cmd[0]);
+}
+
+
+// read a register per, pass first reg value, reading 2 bytes increments register
+// Reads MSB first then LSB
+int MAG3110::readVal(char regAddr)
+{
+ char cmd[2];
+
+ cmd[0] = regAddr;
+ _i2c->write(_i2c_address, cmd, 1);
+
+ cmd[0] = 0x00;
+ cmd[1] = 0x00;
+ _i2c->read(_i2c_address, cmd, 2);
+ return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB
+}
+
+
+void MAG3110::begin()
+{
+ char cmd[2];
+
+ cmd[0] = MAG_CTRL_REG2;
+ cmd[1] = 0x80;
+ _i2c->write(_i2c_address, cmd, 2);
+
+ cmd[0] = MAG_CTRL_REG1;
+ cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE;
+ _i2c->write(_i2c_address, cmd, 2);
+}
+
+float MAG3110::getHeading()
+{
+ int xVal = readVal(MAG_OUT_X_MSB);
+ int yVal = readVal(MAG_OUT_Y_MSB);
+ return (atan2((double)(yVal - _avgY),(double)(xVal - _avgX)))*180/PI;
+}
+
+void MAG3110::getValues(int *xVal, int *yVal, int *zVal)
+{
+ *xVal = readVal(MAG_OUT_X_MSB);
+ *yVal = readVal(MAG_OUT_Y_MSB);
+ *zVal = readVal(MAG_OUT_Z_MSB);
+}
+
+
+void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY )
+{
+ _avgX=(maxX+minX)/2;
+ _avgY=(maxY+minY)/2;
+}
+
+
+
+
+
