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:
- 5:f3abe901c33a
- Parent:
- 1:5a0e7a58d980
- Child:
- 6:1da3fe7b3510
diff -r cf40601402b7 -r f3abe901c33a MAG3110.cpp
--- a/MAG3110.cpp Fri May 24 20:16:24 2013 +0000
+++ b/MAG3110.cpp Mon Apr 07 21:02:57 2014 +0000
@@ -6,13 +6,13 @@
* Constructors
******************************************************************************/
MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl),
- _i2c_address(0x1D), _pc(NULL), _debug(false)
+ _i2c_address(0x1d), _pc(NULL), _debug(false)
{
begin();
}
MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl),
- _i2c_address(0x1D), _pc(pc), _debug(true)
+ _i2c_address(0x1d), _pc(pc), _debug(true)
{
begin();
}
@@ -26,7 +26,7 @@
_i2c.write(_i2c_address, cmd, 2);
cmd[0] = MAG_CTRL_REG1;
- cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE;
+ cmd[1] = MAG_3110_ACTIVE;
_i2c.write(_i2c_address, cmd, 2);
// No adjustment initially
@@ -40,27 +40,34 @@
char cmd[1];
cmd[0] = regAddr;
- _i2c.write(_i2c_address, cmd, 1);
-
+ if(_i2c.write(_i2c_address, cmd, 1)) {
+ printf("MAG3110 write error\r\n");
+ _i2c.stop();
+ _i2c.start();
+ }
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];
-
+ int16_t t;
cmd[0] = regAddr;
- _i2c.write(_i2c_address, cmd, 1);
+ if(_i2c.write(_i2c_address, cmd, 1)) {
+ printf("MAG3110 write error\r\n");
+ _i2c.stop();
+ _i2c.start();
+ }
cmd[0] = 0x00;
cmd[1] = 0x00;
_i2c.read(_i2c_address, cmd, 2);
- return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB
+ t = (cmd[0] * 256) + (unsigned short) cmd[1];
+ return ((int) t); //concatenate the MSB and LSB
}
@@ -78,6 +85,24 @@
*zVal = readVal(MAG_OUT_Z_MSB);
}
+void MAG3110::ReadXYZ(float * mag)
+{
+ int x, y, z;
+ x = readVal(MAG_OUT_X_MSB);
+ y = readVal(MAG_OUT_Y_MSB);
+ z = readVal(MAG_OUT_Z_MSB);
+ mag[0] = (float) x / 10.0;
+ mag[1] = (float) y / 10.0;
+ mag[2] = (float) z / 10.0;
+
+}
+
+void MAG3110::ReadXYZraw(int16_t * mag_raw)
+{
+ mag_raw[0] = readVal(MAG_OUT_X_MSB);
+ mag_raw[1] = readVal(MAG_OUT_Y_MSB);
+ mag_raw[2] = readVal(MAG_OUT_Z_MSB);
+}
void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY )
{
