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:
SomeRandomBloke
Date:
Fri May 09 18:01:36 2014 +0000
Revision:
7:0f45239e157a
merged pull request fro calXY

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 7:0f45239e157a 1
SomeRandomBloke 7:0f45239e157a 2 #include "MAG3110.h"
SomeRandomBloke 7:0f45239e157a 3 #include "mbed.h"
SomeRandomBloke 7:0f45239e157a 4
SomeRandomBloke 7:0f45239e157a 5 /******************************************************************************
SomeRandomBloke 7:0f45239e157a 6 * Constructors
SomeRandomBloke 7:0f45239e157a 7 ******************************************************************************/
SomeRandomBloke 7:0f45239e157a 8 MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl),
SomeRandomBloke 7:0f45239e157a 9 _i2c_address(0x1D), _pc(NULL), _debug(false)
SomeRandomBloke 7:0f45239e157a 10 {
SomeRandomBloke 7:0f45239e157a 11 begin();
SomeRandomBloke 7:0f45239e157a 12 }
SomeRandomBloke 7:0f45239e157a 13
SomeRandomBloke 7:0f45239e157a 14 MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl),
SomeRandomBloke 7:0f45239e157a 15 _i2c_address(0x1D), _pc(pc), _debug(true)
SomeRandomBloke 7:0f45239e157a 16 {
SomeRandomBloke 7:0f45239e157a 17 begin();
SomeRandomBloke 7:0f45239e157a 18 }
SomeRandomBloke 7:0f45239e157a 19
SomeRandomBloke 7:0f45239e157a 20 void MAG3110::begin()
SomeRandomBloke 7:0f45239e157a 21 {
SomeRandomBloke 7:0f45239e157a 22 char cmd[2];
SomeRandomBloke 7:0f45239e157a 23
SomeRandomBloke 7:0f45239e157a 24 cmd[0] = MAG_CTRL_REG2;
SomeRandomBloke 7:0f45239e157a 25 cmd[1] = 0x80;
SomeRandomBloke 7:0f45239e157a 26 _i2c.write(_i2c_address, cmd, 2);
SomeRandomBloke 7:0f45239e157a 27
SomeRandomBloke 7:0f45239e157a 28 cmd[0] = MAG_CTRL_REG1;
SomeRandomBloke 7:0f45239e157a 29 cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE;
SomeRandomBloke 7:0f45239e157a 30 _i2c.write(_i2c_address, cmd, 2);
SomeRandomBloke 7:0f45239e157a 31
SomeRandomBloke 7:0f45239e157a 32 // No adjustment initially
SomeRandomBloke 7:0f45239e157a 33 _avgX = 0;
SomeRandomBloke 7:0f45239e157a 34 _avgY = 0;
SomeRandomBloke 7:0f45239e157a 35 }
SomeRandomBloke 7:0f45239e157a 36
SomeRandomBloke 7:0f45239e157a 37 // Read a single byte form 8 bit register, return as int
SomeRandomBloke 7:0f45239e157a 38 int MAG3110::readReg(char regAddr)
SomeRandomBloke 7:0f45239e157a 39 {
SomeRandomBloke 7:0f45239e157a 40 char cmd[1];
SomeRandomBloke 7:0f45239e157a 41
SomeRandomBloke 7:0f45239e157a 42 cmd[0] = regAddr;
SomeRandomBloke 7:0f45239e157a 43 _i2c.write(_i2c_address, cmd, 1);
SomeRandomBloke 7:0f45239e157a 44
SomeRandomBloke 7:0f45239e157a 45 cmd[0] = 0x00;
SomeRandomBloke 7:0f45239e157a 46 _i2c.read(_i2c_address, cmd, 1);
SomeRandomBloke 7:0f45239e157a 47 return (int)( cmd[0]);
SomeRandomBloke 7:0f45239e157a 48 }
SomeRandomBloke 7:0f45239e157a 49
SomeRandomBloke 7:0f45239e157a 50
SomeRandomBloke 7:0f45239e157a 51 // read a register per, pass first reg value, reading 2 bytes increments register
SomeRandomBloke 7:0f45239e157a 52 // Reads MSB first then LSB
SomeRandomBloke 7:0f45239e157a 53 int MAG3110::readVal(char regAddr)
SomeRandomBloke 7:0f45239e157a 54 {
SomeRandomBloke 7:0f45239e157a 55 char cmd[2];
SomeRandomBloke 7:0f45239e157a 56
SomeRandomBloke 7:0f45239e157a 57 cmd[0] = regAddr;
SomeRandomBloke 7:0f45239e157a 58 _i2c.write(_i2c_address, cmd, 1);
SomeRandomBloke 7:0f45239e157a 59
SomeRandomBloke 7:0f45239e157a 60 cmd[0] = 0x00;
SomeRandomBloke 7:0f45239e157a 61 cmd[1] = 0x00;
SomeRandomBloke 7:0f45239e157a 62 _i2c.read(_i2c_address, cmd, 2);
SomeRandomBloke 7:0f45239e157a 63 return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB
SomeRandomBloke 7:0f45239e157a 64 }
SomeRandomBloke 7:0f45239e157a 65
SomeRandomBloke 7:0f45239e157a 66
SomeRandomBloke 7:0f45239e157a 67 float MAG3110::getHeading()
SomeRandomBloke 7:0f45239e157a 68 {
SomeRandomBloke 7:0f45239e157a 69 int xVal = readVal(MAG_OUT_X_MSB);
SomeRandomBloke 7:0f45239e157a 70 int yVal = readVal(MAG_OUT_Y_MSB);
SomeRandomBloke 7:0f45239e157a 71 return (atan2((double)(yVal - _avgY),(double)(xVal - _avgX)))*180/PI;
SomeRandomBloke 7:0f45239e157a 72 }
SomeRandomBloke 7:0f45239e157a 73
SomeRandomBloke 7:0f45239e157a 74 void MAG3110::getValues(int *xVal, int *yVal, int *zVal)
SomeRandomBloke 7:0f45239e157a 75 {
SomeRandomBloke 7:0f45239e157a 76 *xVal = readVal(MAG_OUT_X_MSB);
SomeRandomBloke 7:0f45239e157a 77 *yVal = readVal(MAG_OUT_Y_MSB);
SomeRandomBloke 7:0f45239e157a 78 *zVal = readVal(MAG_OUT_Z_MSB);
SomeRandomBloke 7:0f45239e157a 79 }
SomeRandomBloke 7:0f45239e157a 80
SomeRandomBloke 7:0f45239e157a 81
SomeRandomBloke 7:0f45239e157a 82 void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY )
SomeRandomBloke 7:0f45239e157a 83 {
SomeRandomBloke 7:0f45239e157a 84 _avgX=(maxX+minX)/2;
SomeRandomBloke 7:0f45239e157a 85 _avgY=(maxY+minY)/2;
SomeRandomBloke 7:0f45239e157a 86 }
SomeRandomBloke 7:0f45239e157a 87
SomeRandomBloke 7:0f45239e157a 88
SomeRandomBloke 7:0f45239e157a 89
SomeRandomBloke 7:0f45239e157a 90 void MAG3110::calXY(PinName pin, int activeValue )
SomeRandomBloke 7:0f45239e157a 91 {
SomeRandomBloke 7:0f45239e157a 92 DigitalIn calPin(pin);
SomeRandomBloke 7:0f45239e157a 93 int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY;
SomeRandomBloke 7:0f45239e157a 94
SomeRandomBloke 7:0f45239e157a 95
SomeRandomBloke 7:0f45239e157a 96 // Wait for Button Press and Release before beginning calibration
SomeRandomBloke 7:0f45239e157a 97 while(calPin != activeValue) {}
SomeRandomBloke 7:0f45239e157a 98 while(calPin == activeValue) {}
SomeRandomBloke 7:0f45239e157a 99
SomeRandomBloke 7:0f45239e157a 100
SomeRandomBloke 7:0f45239e157a 101 // Read initial values of magnetomoter - read it here to create a slight delay for calPin to settle
SomeRandomBloke 7:0f45239e157a 102 tempXmax = tempXmin = readVal(MAG_OUT_X_MSB);
SomeRandomBloke 7:0f45239e157a 103 tempYmax = tempYmin = readVal(MAG_OUT_Y_MSB);
SomeRandomBloke 7:0f45239e157a 104
SomeRandomBloke 7:0f45239e157a 105 // Update min and max values until calPin asserted again
SomeRandomBloke 7:0f45239e157a 106 while(calPin != activeValue) {
SomeRandomBloke 7:0f45239e157a 107 newX = readVal(MAG_OUT_X_MSB);
SomeRandomBloke 7:0f45239e157a 108 newY = readVal(MAG_OUT_Y_MSB);
SomeRandomBloke 7:0f45239e157a 109 if (newX > tempXmax) tempXmax = newX;
SomeRandomBloke 7:0f45239e157a 110 if (newX < tempXmin) tempXmin = newX;
SomeRandomBloke 7:0f45239e157a 111 if (newY > tempYmax) tempYmax = newY;
SomeRandomBloke 7:0f45239e157a 112 if (newY < tempYmin) tempYmin = newY;
SomeRandomBloke 7:0f45239e157a 113 }
SomeRandomBloke 7:0f45239e157a 114
SomeRandomBloke 7:0f45239e157a 115 setCalibration( tempXmin, tempXmax, tempYmin, tempYmax );
SomeRandomBloke 7:0f45239e157a 116
SomeRandomBloke 7:0f45239e157a 117 }
SomeRandomBloke 7:0f45239e157a 118