MAG3110 library with calibrate operation included
Dependents: FRDM-KL46-Template AxedaGo-Freescal_FRDM-KL46Z revert AxedaGo-Freescal_FRDM-KL46Z HC-05_S2B_HelloWorld_WIZwiki-W7500_sensores
Fork of MAG3110 by
Diff: MAG3110.cpp
- Revision:
- 1:5a0e7a58d980
- Parent:
- 0:63a8594a3866
- Child:
- 5:f510561f6107
diff -r 63a8594a3866 -r 5a0e7a58d980 MAG3110.cpp --- a/MAG3110.cpp Sun May 19 20:34:27 2013 +0000 +++ b/MAG3110.cpp Fri May 24 20:04:48 2013 +0000 @@ -5,16 +5,33 @@ /****************************************************************************** * Constructors ******************************************************************************/ -MAG3110::MAG3110(I2C *i2c): - _i2c_address(0x1D), _i2c(i2c),_pc(NULL), _debug(false) +MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl), + _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) +{ + begin(); } -MAG3110::MAG3110(I2C *i2c, Serial *pc): - _i2c_address(0x1D), _i2c(i2c), _pc(pc), _debug(true) +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); + + // No adjustment initially + _avgX = 0; + _avgY = 0; } // Read a single byte form 8 bit register, return as int @@ -23,10 +40,10 @@ char cmd[1]; cmd[0] = regAddr; - _i2c->write(_i2c_address, cmd, 1); + _i2c.write(_i2c_address, cmd, 1); cmd[0] = 0x00; - _i2c->read(_i2c_address, cmd, 1); + _i2c.read(_i2c_address, cmd, 1); return (int)( cmd[0]); } @@ -38,28 +55,15 @@ char cmd[2]; cmd[0] = regAddr; - _i2c->write(_i2c_address, cmd, 1); + _i2c.write(_i2c_address, cmd, 1); cmd[0] = 0x00; cmd[1] = 0x00; - _i2c->read(_i2c_address, cmd, 2); + _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);