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

Dependencies:   MotionSensor

Dependents:   Wi-Go-MagnetometerTest EE202A_HW1_MH serialtoxively mbed_nanosec_timer ... more

Committer:
JimCarver
Date:
Mon Apr 07 21:02:57 2014 +0000
Revision:
5:f3abe901c33a
Parent:
1:5a0e7a58d980
Child:
7:0f45239e157a
Child:
9:1da3fe7b3510
Added a function to retrieve raw data from the sensor

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 1:5a0e7a58d980 8 MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl),
JimCarver 5:f3abe901c33a 9 _i2c_address(0x1d), _pc(NULL), _debug(false)
SomeRandomBloke 0:63a8594a3866 10 {
SomeRandomBloke 1:5a0e7a58d980 11 begin();
SomeRandomBloke 1:5a0e7a58d980 12 }
SomeRandomBloke 0:63a8594a3866 13
SomeRandomBloke 1:5a0e7a58d980 14 MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl),
JimCarver 5:f3abe901c33a 15 _i2c_address(0x1d), _pc(pc), _debug(true)
SomeRandomBloke 1:5a0e7a58d980 16 {
SomeRandomBloke 1:5a0e7a58d980 17 begin();
SomeRandomBloke 0:63a8594a3866 18 }
SomeRandomBloke 0:63a8594a3866 19
SomeRandomBloke 1:5a0e7a58d980 20 void MAG3110::begin()
SomeRandomBloke 0:63a8594a3866 21 {
SomeRandomBloke 1:5a0e7a58d980 22 char cmd[2];
SomeRandomBloke 0:63a8594a3866 23
SomeRandomBloke 1:5a0e7a58d980 24 cmd[0] = MAG_CTRL_REG2;
SomeRandomBloke 1:5a0e7a58d980 25 cmd[1] = 0x80;
SomeRandomBloke 1:5a0e7a58d980 26 _i2c.write(_i2c_address, cmd, 2);
SomeRandomBloke 1:5a0e7a58d980 27
SomeRandomBloke 1:5a0e7a58d980 28 cmd[0] = MAG_CTRL_REG1;
JimCarver 5:f3abe901c33a 29 cmd[1] = MAG_3110_ACTIVE;
SomeRandomBloke 1:5a0e7a58d980 30 _i2c.write(_i2c_address, cmd, 2);
SomeRandomBloke 1:5a0e7a58d980 31
SomeRandomBloke 1:5a0e7a58d980 32 // No adjustment initially
SomeRandomBloke 1:5a0e7a58d980 33 _avgX = 0;
SomeRandomBloke 1:5a0e7a58d980 34 _avgY = 0;
SomeRandomBloke 0:63a8594a3866 35 }
SomeRandomBloke 0:63a8594a3866 36
SomeRandomBloke 0:63a8594a3866 37 // Read a single byte form 8 bit register, return as int
SomeRandomBloke 0:63a8594a3866 38 int MAG3110::readReg(char regAddr)
SomeRandomBloke 0:63a8594a3866 39 {
SomeRandomBloke 0:63a8594a3866 40 char cmd[1];
SomeRandomBloke 0:63a8594a3866 41
SomeRandomBloke 0:63a8594a3866 42 cmd[0] = regAddr;
JimCarver 5:f3abe901c33a 43 if(_i2c.write(_i2c_address, cmd, 1)) {
JimCarver 5:f3abe901c33a 44 printf("MAG3110 write error\r\n");
JimCarver 5:f3abe901c33a 45 _i2c.stop();
JimCarver 5:f3abe901c33a 46 _i2c.start();
JimCarver 5:f3abe901c33a 47 }
SomeRandomBloke 0:63a8594a3866 48 cmd[0] = 0x00;
SomeRandomBloke 1:5a0e7a58d980 49 _i2c.read(_i2c_address, cmd, 1);
SomeRandomBloke 0:63a8594a3866 50 return (int)( cmd[0]);
SomeRandomBloke 0:63a8594a3866 51 }
SomeRandomBloke 0:63a8594a3866 52
SomeRandomBloke 0:63a8594a3866 53 // read a register per, pass first reg value, reading 2 bytes increments register
SomeRandomBloke 0:63a8594a3866 54 // Reads MSB first then LSB
SomeRandomBloke 0:63a8594a3866 55 int MAG3110::readVal(char regAddr)
SomeRandomBloke 0:63a8594a3866 56 {
SomeRandomBloke 0:63a8594a3866 57 char cmd[2];
JimCarver 5:f3abe901c33a 58 int16_t t;
SomeRandomBloke 0:63a8594a3866 59 cmd[0] = regAddr;
JimCarver 5:f3abe901c33a 60 if(_i2c.write(_i2c_address, cmd, 1)) {
JimCarver 5:f3abe901c33a 61 printf("MAG3110 write error\r\n");
JimCarver 5:f3abe901c33a 62 _i2c.stop();
JimCarver 5:f3abe901c33a 63 _i2c.start();
JimCarver 5:f3abe901c33a 64 }
SomeRandomBloke 0:63a8594a3866 65
SomeRandomBloke 0:63a8594a3866 66 cmd[0] = 0x00;
SomeRandomBloke 0:63a8594a3866 67 cmd[1] = 0x00;
SomeRandomBloke 1:5a0e7a58d980 68 _i2c.read(_i2c_address, cmd, 2);
JimCarver 5:f3abe901c33a 69 t = (cmd[0] * 256) + (unsigned short) cmd[1];
JimCarver 5:f3abe901c33a 70 return ((int) t); //concatenate the MSB and LSB
SomeRandomBloke 0:63a8594a3866 71 }
SomeRandomBloke 0:63a8594a3866 72
SomeRandomBloke 0:63a8594a3866 73
SomeRandomBloke 0:63a8594a3866 74 float MAG3110::getHeading()
SomeRandomBloke 0:63a8594a3866 75 {
SomeRandomBloke 0:63a8594a3866 76 int xVal = readVal(MAG_OUT_X_MSB);
SomeRandomBloke 0:63a8594a3866 77 int yVal = readVal(MAG_OUT_Y_MSB);
SomeRandomBloke 0:63a8594a3866 78 return (atan2((double)(yVal - _avgY),(double)(xVal - _avgX)))*180/PI;
SomeRandomBloke 0:63a8594a3866 79 }
SomeRandomBloke 0:63a8594a3866 80
SomeRandomBloke 0:63a8594a3866 81 void MAG3110::getValues(int *xVal, int *yVal, int *zVal)
SomeRandomBloke 0:63a8594a3866 82 {
SomeRandomBloke 0:63a8594a3866 83 *xVal = readVal(MAG_OUT_X_MSB);
SomeRandomBloke 0:63a8594a3866 84 *yVal = readVal(MAG_OUT_Y_MSB);
SomeRandomBloke 0:63a8594a3866 85 *zVal = readVal(MAG_OUT_Z_MSB);
SomeRandomBloke 0:63a8594a3866 86 }
SomeRandomBloke 0:63a8594a3866 87
JimCarver 5:f3abe901c33a 88 void MAG3110::ReadXYZ(float * mag)
JimCarver 5:f3abe901c33a 89 {
JimCarver 5:f3abe901c33a 90 int x, y, z;
JimCarver 5:f3abe901c33a 91 x = readVal(MAG_OUT_X_MSB);
JimCarver 5:f3abe901c33a 92 y = readVal(MAG_OUT_Y_MSB);
JimCarver 5:f3abe901c33a 93 z = readVal(MAG_OUT_Z_MSB);
JimCarver 5:f3abe901c33a 94 mag[0] = (float) x / 10.0;
JimCarver 5:f3abe901c33a 95 mag[1] = (float) y / 10.0;
JimCarver 5:f3abe901c33a 96 mag[2] = (float) z / 10.0;
JimCarver 5:f3abe901c33a 97
JimCarver 5:f3abe901c33a 98 }
JimCarver 5:f3abe901c33a 99
JimCarver 5:f3abe901c33a 100 void MAG3110::ReadXYZraw(int16_t * mag_raw)
JimCarver 5:f3abe901c33a 101 {
JimCarver 5:f3abe901c33a 102 mag_raw[0] = readVal(MAG_OUT_X_MSB);
JimCarver 5:f3abe901c33a 103 mag_raw[1] = readVal(MAG_OUT_Y_MSB);
JimCarver 5:f3abe901c33a 104 mag_raw[2] = readVal(MAG_OUT_Z_MSB);
JimCarver 5:f3abe901c33a 105 }
SomeRandomBloke 0:63a8594a3866 106
SomeRandomBloke 0:63a8594a3866 107 void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY )
SomeRandomBloke 0:63a8594a3866 108 {
SomeRandomBloke 0:63a8594a3866 109 _avgX=(maxX+minX)/2;
SomeRandomBloke 0:63a8594a3866 110 _avgY=(maxY+minY)/2;
SomeRandomBloke 0:63a8594a3866 111 }
SomeRandomBloke 0:63a8594a3866 112
SomeRandomBloke 0:63a8594a3866 113
SomeRandomBloke 0:63a8594a3866 114
SomeRandomBloke 0:63a8594a3866 115
SomeRandomBloke 0:63a8594a3866 116