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 Andrew Lindsay

Committer:
SomeRandomBloke
Date:
Sun May 19 20:34:27 2013 +0000
Revision:
0:63a8594a3866
Child:
1:5a0e7a58d980
Initial library commit

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