Simple library for MAG3110 magenetometer as built into Avnet Wi-Go module

Dependents:   202A_HW1

Fork of MAG3110 by Andrew Lindsay

Committer:
mja054
Date:
Wed Feb 12 22:23:00 2014 +0000
Revision:
5:760476084e70
Parent:
1:5a0e7a58d980
Added a new constructor function.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 0:63a8594a3866 1
SomeRandomBloke 0:63a8594a3866 2 #include "MAG3110.h"
SomeRandomBloke 0:63a8594a3866 3 #include "mbed.h"
SomeRandomBloke 0:63a8594a3866 4
SomeRandomBloke 0:63a8594a3866 5 /******************************************************************************
SomeRandomBloke 0:63a8594a3866 6 * Constructors
SomeRandomBloke 0:63a8594a3866 7 ******************************************************************************/
mja054 5:760476084e70 8 MAG3110::MAG3110(PinName sda, PinName scl, int addr): _i2c(sda, scl),
mja054 5:760476084e70 9 _i2c_address(addr), _pc(NULL), _debug(false)
mja054 5:760476084e70 10 {
mja054 5:760476084e70 11 begin();
mja054 5:760476084e70 12 }
SomeRandomBloke 1:5a0e7a58d980 13 MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl),
SomeRandomBloke 1:5a0e7a58d980 14 _i2c_address(0x1D), _pc(NULL), _debug(false)
SomeRandomBloke 0:63a8594a3866 15 {
SomeRandomBloke 1:5a0e7a58d980 16 begin();
SomeRandomBloke 1:5a0e7a58d980 17 }
SomeRandomBloke 0:63a8594a3866 18
SomeRandomBloke 1:5a0e7a58d980 19 MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl),
SomeRandomBloke 1:5a0e7a58d980 20 _i2c_address(0x1D), _pc(pc), _debug(true)
SomeRandomBloke 1:5a0e7a58d980 21 {
SomeRandomBloke 1:5a0e7a58d980 22 begin();
SomeRandomBloke 0:63a8594a3866 23 }
SomeRandomBloke 0:63a8594a3866 24
SomeRandomBloke 1:5a0e7a58d980 25 void MAG3110::begin()
SomeRandomBloke 0:63a8594a3866 26 {
SomeRandomBloke 1:5a0e7a58d980 27 char cmd[2];
SomeRandomBloke 0:63a8594a3866 28
SomeRandomBloke 1:5a0e7a58d980 29 cmd[0] = MAG_CTRL_REG2;
SomeRandomBloke 1:5a0e7a58d980 30 cmd[1] = 0x80;
SomeRandomBloke 1:5a0e7a58d980 31 _i2c.write(_i2c_address, cmd, 2);
SomeRandomBloke 1:5a0e7a58d980 32
SomeRandomBloke 1:5a0e7a58d980 33 cmd[0] = MAG_CTRL_REG1;
SomeRandomBloke 1:5a0e7a58d980 34 cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE;
SomeRandomBloke 1:5a0e7a58d980 35 _i2c.write(_i2c_address, cmd, 2);
SomeRandomBloke 1:5a0e7a58d980 36
SomeRandomBloke 1:5a0e7a58d980 37 // No adjustment initially
SomeRandomBloke 1:5a0e7a58d980 38 _avgX = 0;
SomeRandomBloke 1:5a0e7a58d980 39 _avgY = 0;
SomeRandomBloke 0:63a8594a3866 40 }
SomeRandomBloke 0:63a8594a3866 41
SomeRandomBloke 0:63a8594a3866 42 // Read a single byte form 8 bit register, return as int
SomeRandomBloke 0:63a8594a3866 43 int MAG3110::readReg(char regAddr)
SomeRandomBloke 0:63a8594a3866 44 {
SomeRandomBloke 0:63a8594a3866 45 char cmd[1];
SomeRandomBloke 0:63a8594a3866 46
SomeRandomBloke 0:63a8594a3866 47 cmd[0] = regAddr;
SomeRandomBloke 1:5a0e7a58d980 48 _i2c.write(_i2c_address, cmd, 1);
SomeRandomBloke 0:63a8594a3866 49
SomeRandomBloke 0:63a8594a3866 50 cmd[0] = 0x00;
SomeRandomBloke 1:5a0e7a58d980 51 _i2c.read(_i2c_address, cmd, 1);
SomeRandomBloke 0:63a8594a3866 52 return (int)( cmd[0]);
SomeRandomBloke 0:63a8594a3866 53 }
SomeRandomBloke 0:63a8594a3866 54
SomeRandomBloke 0:63a8594a3866 55
SomeRandomBloke 0:63a8594a3866 56 // read a register per, pass first reg value, reading 2 bytes increments register
SomeRandomBloke 0:63a8594a3866 57 // Reads MSB first then LSB
SomeRandomBloke 0:63a8594a3866 58 int MAG3110::readVal(char regAddr)
SomeRandomBloke 0:63a8594a3866 59 {
SomeRandomBloke 0:63a8594a3866 60 char cmd[2];
SomeRandomBloke 0:63a8594a3866 61
SomeRandomBloke 0:63a8594a3866 62 cmd[0] = regAddr;
SomeRandomBloke 1:5a0e7a58d980 63 _i2c.write(_i2c_address, cmd, 1);
SomeRandomBloke 0:63a8594a3866 64
SomeRandomBloke 0:63a8594a3866 65 cmd[0] = 0x00;
SomeRandomBloke 0:63a8594a3866 66 cmd[1] = 0x00;
SomeRandomBloke 1:5a0e7a58d980 67 _i2c.read(_i2c_address, cmd, 2);
SomeRandomBloke 0:63a8594a3866 68 return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB
SomeRandomBloke 0:63a8594a3866 69 }
SomeRandomBloke 0:63a8594a3866 70
SomeRandomBloke 0:63a8594a3866 71
SomeRandomBloke 0:63a8594a3866 72 float MAG3110::getHeading()
SomeRandomBloke 0:63a8594a3866 73 {
SomeRandomBloke 0:63a8594a3866 74 int xVal = readVal(MAG_OUT_X_MSB);
SomeRandomBloke 0:63a8594a3866 75 int yVal = readVal(MAG_OUT_Y_MSB);
SomeRandomBloke 0:63a8594a3866 76 return (atan2((double)(yVal - _avgY),(double)(xVal - _avgX)))*180/PI;
SomeRandomBloke 0:63a8594a3866 77 }
SomeRandomBloke 0:63a8594a3866 78
SomeRandomBloke 0:63a8594a3866 79 void MAG3110::getValues(int *xVal, int *yVal, int *zVal)
SomeRandomBloke 0:63a8594a3866 80 {
SomeRandomBloke 0:63a8594a3866 81 *xVal = readVal(MAG_OUT_X_MSB);
SomeRandomBloke 0:63a8594a3866 82 *yVal = readVal(MAG_OUT_Y_MSB);
SomeRandomBloke 0:63a8594a3866 83 *zVal = readVal(MAG_OUT_Z_MSB);
SomeRandomBloke 0:63a8594a3866 84 }
SomeRandomBloke 0:63a8594a3866 85
SomeRandomBloke 0:63a8594a3866 86
SomeRandomBloke 0:63a8594a3866 87 void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY )
SomeRandomBloke 0:63a8594a3866 88 {
SomeRandomBloke 0:63a8594a3866 89 _avgX=(maxX+minX)/2;
SomeRandomBloke 0:63a8594a3866 90 _avgY=(maxY+minY)/2;
SomeRandomBloke 0:63a8594a3866 91 }
SomeRandomBloke 0:63a8594a3866 92
SomeRandomBloke 0:63a8594a3866 93
SomeRandomBloke 0:63a8594a3866 94
SomeRandomBloke 0:63a8594a3866 95
SomeRandomBloke 0:63a8594a3866 96