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: mbed Rejestrator
Diff: MAG3110/MAG3110.cpp
- Revision:
- 1:5ad44a4edff9
- Parent:
- 0:fa31f8461c63
--- a/MAG3110/MAG3110.cpp Sat Apr 18 17:01:57 2015 +0000 +++ b/MAG3110/MAG3110.cpp Sat May 09 15:06:38 2015 +0000 @@ -1,6 +1,24 @@ #include "MAG3110.h" #include "mbed.h" +/* + Mag regs: FF FC 5B 02 8A 0C EA C4 02 00 00 00 00 00 00 00 19 00 5F FF . +64601 650 3306 + +FF +x FB B9 +y 01 21 +z 0D 44 +id C4 +System mode 02 +off x 00 00 +off y 00 00 +off z 00 00 +temp 00 +ctrl reg1 19 +ctrl reg2 00 +BF FF . +*/ /****************************************************************************** * Constructors @@ -11,18 +29,24 @@ begin(); } -void MAG3110::begin() +void MAG3110::begin(void) { char cmd[2]; cmd[0] = MAG_CTRL_REG2; - cmd[1] = MAG_3110_AUTO_MRST_EN + MAG_3110_MAG_RST; + cmd[1] = MAG_3110_MAG_RST; _i2c.write(_i2c_address, cmd, 2); cmd[0] = MAG_CTRL_REG1; - cmd[1] = MAG_3110_SAMPLE80 + MAG_3110_OVERSAMPLE4 + MAG_3110_ACTIVE; + cmd[1] = MAG_3110_SAMPLE80 + MAG_3110_OVERSAMPLE3 + MAG_3110_ACTIVE; _i2c.write(_i2c_address, cmd, 2); + cmd[0] = MAG_WHO_AM_I; + _i2c.write(_i2c_address, cmd, 1); + cmd[0] = 0x00; + _i2c.read(_i2c_address, cmd, 1); +// if (cmd[0] == MAG_3110_WHO_AM_I_VALUE) + // No adjustment initially _avgX = 0; _avgY = 0; @@ -44,7 +68,7 @@ // read a register per, pass first reg value, reading 2 bytes increments register // Reads MSB first then LSB -int MAG3110::readVal(char regAddr) +int16_t MAG3110::readVal(char regAddr) { char cmd[2]; @@ -58,11 +82,22 @@ } +int MAG3110::readAll(char *buffer) +{ + char cmd[2]; + + cmd[0] = 0; + _i2c.write(_i2c_address, cmd, 1); + + _i2c.read(_i2c_address, buffer, 20); + return (int)buffer[0]; //status +} + 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; + return (atan2((double)(yVal - _avgY),(double)(xVal - _avgX)))*180./PI; } void MAG3110::getValues(int *xVal, int *yVal, int *zVal)