Simple library for MAG3110 magenetometer as built into Avnet Wi-Go module
Dependents: Wi-Go-MagnetometerTest EE202A_HW1_MH serialtoxively mbed_nanosec_timer ... more
MAG3110.cpp@6:f510561f6107, 2013-12-31 (annotated)
- Committer:
- mmaas
- Date:
- Tue Dec 31 17:16:37 2013 +0000
- Revision:
- 6:f510561f6107
- Parent:
- 1:5a0e7a58d980
- Child:
- 7:0f45239e157a
Moved calibrate operation into the library
Who changed what in which revision?
User | Revision | Line number | New 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 | ******************************************************************************/ |
mmaas | 6:f510561f6107 | 8 | MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl), |
SomeRandomBloke | 1:5a0e7a58d980 | 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 | |
mmaas | 6:f510561f6107 | 14 | MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl), |
mmaas | 6:f510561f6107 | 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; |
SomeRandomBloke | 1:5a0e7a58d980 | 29 | cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE; |
SomeRandomBloke | 1:5a0e7a58d980 | 30 | _i2c.write(_i2c_address, cmd, 2); |
mmaas | 6:f510561f6107 | 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; |
SomeRandomBloke | 1:5a0e7a58d980 | 43 | _i2c.write(_i2c_address, cmd, 1); |
SomeRandomBloke | 0:63a8594a3866 | 44 | |
SomeRandomBloke | 0:63a8594a3866 | 45 | cmd[0] = 0x00; |
SomeRandomBloke | 1:5a0e7a58d980 | 46 | _i2c.read(_i2c_address, cmd, 1); |
SomeRandomBloke | 0:63a8594a3866 | 47 | return (int)( cmd[0]); |
SomeRandomBloke | 0:63a8594a3866 | 48 | } |
SomeRandomBloke | 0:63a8594a3866 | 49 | |
SomeRandomBloke | 0:63a8594a3866 | 50 | |
SomeRandomBloke | 0:63a8594a3866 | 51 | // read a register per, pass first reg value, reading 2 bytes increments register |
SomeRandomBloke | 0:63a8594a3866 | 52 | // Reads MSB first then LSB |
SomeRandomBloke | 0:63a8594a3866 | 53 | int MAG3110::readVal(char regAddr) |
SomeRandomBloke | 0:63a8594a3866 | 54 | { |
SomeRandomBloke | 0:63a8594a3866 | 55 | char cmd[2]; |
SomeRandomBloke | 0:63a8594a3866 | 56 | |
SomeRandomBloke | 0:63a8594a3866 | 57 | cmd[0] = regAddr; |
SomeRandomBloke | 1:5a0e7a58d980 | 58 | _i2c.write(_i2c_address, cmd, 1); |
SomeRandomBloke | 0:63a8594a3866 | 59 | |
SomeRandomBloke | 0:63a8594a3866 | 60 | cmd[0] = 0x00; |
SomeRandomBloke | 0:63a8594a3866 | 61 | cmd[1] = 0x00; |
SomeRandomBloke | 1:5a0e7a58d980 | 62 | _i2c.read(_i2c_address, cmd, 2); |
SomeRandomBloke | 0:63a8594a3866 | 63 | return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB |
SomeRandomBloke | 0:63a8594a3866 | 64 | } |
SomeRandomBloke | 0:63a8594a3866 | 65 | |
SomeRandomBloke | 0:63a8594a3866 | 66 | |
SomeRandomBloke | 0:63a8594a3866 | 67 | float MAG3110::getHeading() |
SomeRandomBloke | 0:63a8594a3866 | 68 | { |
SomeRandomBloke | 0:63a8594a3866 | 69 | int xVal = readVal(MAG_OUT_X_MSB); |
SomeRandomBloke | 0:63a8594a3866 | 70 | int yVal = readVal(MAG_OUT_Y_MSB); |
SomeRandomBloke | 0:63a8594a3866 | 71 | return (atan2((double)(yVal - _avgY),(double)(xVal - _avgX)))*180/PI; |
SomeRandomBloke | 0:63a8594a3866 | 72 | } |
SomeRandomBloke | 0:63a8594a3866 | 73 | |
SomeRandomBloke | 0:63a8594a3866 | 74 | void MAG3110::getValues(int *xVal, int *yVal, int *zVal) |
SomeRandomBloke | 0:63a8594a3866 | 75 | { |
SomeRandomBloke | 0:63a8594a3866 | 76 | *xVal = readVal(MAG_OUT_X_MSB); |
SomeRandomBloke | 0:63a8594a3866 | 77 | *yVal = readVal(MAG_OUT_Y_MSB); |
SomeRandomBloke | 0:63a8594a3866 | 78 | *zVal = readVal(MAG_OUT_Z_MSB); |
SomeRandomBloke | 0:63a8594a3866 | 79 | } |
SomeRandomBloke | 0:63a8594a3866 | 80 | |
SomeRandomBloke | 0:63a8594a3866 | 81 | |
SomeRandomBloke | 0:63a8594a3866 | 82 | void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY ) |
SomeRandomBloke | 0:63a8594a3866 | 83 | { |
SomeRandomBloke | 0:63a8594a3866 | 84 | _avgX=(maxX+minX)/2; |
SomeRandomBloke | 0:63a8594a3866 | 85 | _avgY=(maxY+minY)/2; |
SomeRandomBloke | 0:63a8594a3866 | 86 | } |
SomeRandomBloke | 0:63a8594a3866 | 87 | |
SomeRandomBloke | 0:63a8594a3866 | 88 | |
SomeRandomBloke | 0:63a8594a3866 | 89 | |
mmaas | 6:f510561f6107 | 90 | void MAG3110::calXY(PinName pin, int activeValue ) |
mmaas | 6:f510561f6107 | 91 | { |
mmaas | 6:f510561f6107 | 92 | DigitalIn calPin(pin); |
mmaas | 6:f510561f6107 | 93 | int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY; |
mmaas | 6:f510561f6107 | 94 | |
mmaas | 6:f510561f6107 | 95 | |
mmaas | 6:f510561f6107 | 96 | // Wait for Button Press and Release before beginning calibration |
mmaas | 6:f510561f6107 | 97 | while(calPin != activeValue) {} |
mmaas | 6:f510561f6107 | 98 | while(calPin == activeValue) {} |
SomeRandomBloke | 0:63a8594a3866 | 99 | |
SomeRandomBloke | 0:63a8594a3866 | 100 | |
mmaas | 6:f510561f6107 | 101 | // Read initial values of magnetomoter - read it here to create a slight delay for calPin to settle |
mmaas | 6:f510561f6107 | 102 | tempXmax = tempXmin = readVal(MAG_OUT_X_MSB); |
mmaas | 6:f510561f6107 | 103 | tempYmax = tempYmin = readVal(MAG_OUT_Y_MSB); |
mmaas | 6:f510561f6107 | 104 | |
mmaas | 6:f510561f6107 | 105 | // Update min and max values until calPin asserted again |
mmaas | 6:f510561f6107 | 106 | while(calPin != activeValue) { |
mmaas | 6:f510561f6107 | 107 | newX = readVal(MAG_OUT_X_MSB); |
mmaas | 6:f510561f6107 | 108 | newY = readVal(MAG_OUT_Y_MSB); |
mmaas | 6:f510561f6107 | 109 | if (newX > tempXmax) tempXmax = newX; |
mmaas | 6:f510561f6107 | 110 | if (newX < tempXmin) tempXmin = newX; |
mmaas | 6:f510561f6107 | 111 | if (newY > tempYmax) tempYmax = newY; |
mmaas | 6:f510561f6107 | 112 | if (newY < tempYmin) tempYmin = newY; |
mmaas | 6:f510561f6107 | 113 | } |
mmaas | 6:f510561f6107 | 114 | |
mmaas | 6:f510561f6107 | 115 | setCalibration( tempXmin, tempXmax, tempYmin, tempYmax ); |
mmaas | 6:f510561f6107 | 116 | |
mmaas | 6:f510561f6107 | 117 | } |
mmaas | 6:f510561f6107 | 118 |