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
Parent:
6:f510561f6107
Parent:
5:f3abe901c33a
merged pull request fro calXY

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 1:5a0e7a58d980 1 /*
SomeRandomBloke 1:5a0e7a58d980 2 * MAG3110 Sensor Library for mbed
SomeRandomBloke 0:63a8594a3866 3 * TODO: Add proper header
SomeRandomBloke 0:63a8594a3866 4 */
SomeRandomBloke 0:63a8594a3866 5
SomeRandomBloke 0:63a8594a3866 6 #ifndef MAG3110_H
SomeRandomBloke 0:63a8594a3866 7 #define MAG3110_H
SomeRandomBloke 0:63a8594a3866 8
SomeRandomBloke 0:63a8594a3866 9 #include "mbed.h"
SomeRandomBloke 0:63a8594a3866 10
SomeRandomBloke 0:63a8594a3866 11 #define PI 3.14159265359
SomeRandomBloke 0:63a8594a3866 12
SomeRandomBloke 0:63a8594a3866 13 #define MAG_ADDR 0x1D
SomeRandomBloke 0:63a8594a3866 14
SomeRandomBloke 0:63a8594a3866 15 // define registers
SomeRandomBloke 0:63a8594a3866 16 #define MAG_DR_STATUS 0x00
SomeRandomBloke 0:63a8594a3866 17 #define MAG_OUT_X_MSB 0x01
SomeRandomBloke 0:63a8594a3866 18 #define MAG_OUT_X_LSB 0x02
SomeRandomBloke 0:63a8594a3866 19 #define MAG_OUT_Y_MSB 0x03
SomeRandomBloke 0:63a8594a3866 20 #define MAG_OUT_Y_LSB 0x04
SomeRandomBloke 0:63a8594a3866 21 #define MAG_OUT_Z_MSB 0x05
SomeRandomBloke 0:63a8594a3866 22 #define MAG_OUT_Z_LSB 0x06
SomeRandomBloke 0:63a8594a3866 23 #define MAG_WHO_AM_I 0x07
SomeRandomBloke 0:63a8594a3866 24 #define MAG_SYSMOD 0x08
SomeRandomBloke 0:63a8594a3866 25 #define MAG_OFF_X_MSB 0x09
SomeRandomBloke 0:63a8594a3866 26 #define MAG_OFF_X_LSB 0x0A
SomeRandomBloke 0:63a8594a3866 27 #define MAG_OFF_Y_MSB 0x0B
SomeRandomBloke 0:63a8594a3866 28 #define MAG_OFF_Y_LSB 0x0C
SomeRandomBloke 0:63a8594a3866 29 #define MAG_OFF_Z_MSB 0x0D
SomeRandomBloke 0:63a8594a3866 30 #define MAG_OFF_Z_LSB 0x0E
SomeRandomBloke 0:63a8594a3866 31 #define MAG_DIE_TEMP 0x0F
SomeRandomBloke 0:63a8594a3866 32 #define MAG_CTRL_REG1 0x10
SomeRandomBloke 0:63a8594a3866 33 #define MAG_CTRL_REG2 0x11
SomeRandomBloke 0:63a8594a3866 34
SomeRandomBloke 0:63a8594a3866 35 // what should WHO_AM_I return?
SomeRandomBloke 0:63a8594a3866 36 #define MAG_3110_WHO_AM_I_VALUE 0xC4
SomeRandomBloke 0:63a8594a3866 37
SomeRandomBloke 0:63a8594a3866 38
SomeRandomBloke 0:63a8594a3866 39 // Fields in registers
SomeRandomBloke 0:63a8594a3866 40 // CTRL_REG1: dr2,dr1,dr0 os1,os0 fr tm ac
SomeRandomBloke 0:63a8594a3866 41
SomeRandomBloke 0:63a8594a3866 42 // Sampling rate from 80Hz down to 0.625Hz
SomeRandomBloke 0:63a8594a3866 43 #define MAG_3110_SAMPLE80 0
SomeRandomBloke 0:63a8594a3866 44 #define MAG_3110_SAMPLE40 0x20
SomeRandomBloke 0:63a8594a3866 45 #define MAG_3110_SAMPLE20 0x40
SomeRandomBloke 0:63a8594a3866 46 #define MAG_3110_SAMPLE10 0x60
SomeRandomBloke 0:63a8594a3866 47 #define MAG_3110_SAMPLE5 0x80
SomeRandomBloke 0:63a8594a3866 48 #define MAG_3110_SAMPLE2_5 0xA0
SomeRandomBloke 0:63a8594a3866 49 #define MAG_3110_SAMPLE1_25 0xC0
SomeRandomBloke 0:63a8594a3866 50 #define MAG_3110_SAMPLE0_625 0xE0
SomeRandomBloke 0:63a8594a3866 51
SomeRandomBloke 0:63a8594a3866 52 // How many samples to average (lowers data rate)
SomeRandomBloke 0:63a8594a3866 53 #define MAG_3110_OVERSAMPLE1 0
SomeRandomBloke 0:63a8594a3866 54 #define MAG_3110_OVERSAMPLE2 0x08
SomeRandomBloke 0:63a8594a3866 55 #define MAG_3110_OVERSAMPLE3 0x10
SomeRandomBloke 0:63a8594a3866 56 #define MAG_3110_OVERSAMPLE4 0x18
SomeRandomBloke 0:63a8594a3866 57
SomeRandomBloke 0:63a8594a3866 58 // read only 1 byte per axis
SomeRandomBloke 0:63a8594a3866 59 #define MAG_3110_FASTREAD 0x04
SomeRandomBloke 0:63a8594a3866 60 // do one measurement (even if in standby mode)
SomeRandomBloke 0:63a8594a3866 61 #define MAG_3110_TRIGGER 0x02
SomeRandomBloke 0:63a8594a3866 62 // put in active mode
SomeRandomBloke 0:63a8594a3866 63 #define MAG_3110_ACTIVE 0x01
SomeRandomBloke 0:63a8594a3866 64
SomeRandomBloke 0:63a8594a3866 65 // CTRL_REG2: AUTO_MRST_EN _ RAW MAG_RST _ _ _ _ _
SomeRandomBloke 0:63a8594a3866 66 // reset sensor after each reading
SomeRandomBloke 0:63a8594a3866 67 #define MAG_3110_AUTO_MRST_EN 0x80
SomeRandomBloke 0:63a8594a3866 68 // don't subtract user offsets
SomeRandomBloke 0:63a8594a3866 69 #define MAG_3110_RAW 0x20
SomeRandomBloke 0:63a8594a3866 70 // reset magnetic sensor after too-large field
SomeRandomBloke 0:63a8594a3866 71 #define MAG_3110_MAG_RST 0x10
SomeRandomBloke 0:63a8594a3866 72
SomeRandomBloke 0:63a8594a3866 73 // DR_STATUS Register ZYXOW ZOW YOW XOW ZYXDR ZDR YDR XDR
SomeRandomBloke 0:63a8594a3866 74 #define MAG_3110_ZYXDR 0x08
SomeRandomBloke 0:63a8594a3866 75
SomeRandomBloke 3:bc784c24f3b2 76 /**
SomeRandomBloke 3:bc784c24f3b2 77 * MAG3110 Class to read X/Y/Z data from the magentometer
SomeRandomBloke 3:bc784c24f3b2 78 *
SomeRandomBloke 3:bc784c24f3b2 79 */
SomeRandomBloke 0:63a8594a3866 80 class MAG3110
SomeRandomBloke 0:63a8594a3866 81 {
SomeRandomBloke 1:5a0e7a58d980 82 public:
SomeRandomBloke 1:5a0e7a58d980 83 /**
SomeRandomBloke 4:cf40601402b7 84 * Main constructor
SomeRandomBloke 1:5a0e7a58d980 85 * @param sda SDA pin
SomeRandomBloke 1:5a0e7a58d980 86 * @param sdl SCL pin
SomeRandomBloke 1:5a0e7a58d980 87 * @param addr addr of the I2C peripheral
SomeRandomBloke 1:5a0e7a58d980 88 */
SomeRandomBloke 1:5a0e7a58d980 89 MAG3110(PinName sda, PinName scl);
SomeRandomBloke 1:5a0e7a58d980 90 /**
SomeRandomBloke 4:cf40601402b7 91 * Debug version of constructor
SomeRandomBloke 1:5a0e7a58d980 92 * @param sda SDA pin
SomeRandomBloke 1:5a0e7a58d980 93 * @param sdl SCL pin
SomeRandomBloke 4:cf40601402b7 94 * @param addr Address of the I2C peripheral
SomeRandomBloke 4:cf40601402b7 95 * @param pc Serial object to output debug messages
SomeRandomBloke 1:5a0e7a58d980 96 */
SomeRandomBloke 1:5a0e7a58d980 97 MAG3110(PinName sda, PinName scl, Serial *pc); //pass serial for debug
SomeRandomBloke 1:5a0e7a58d980 98 /**
SomeRandomBloke 1:5a0e7a58d980 99 * Setup the Magnetometer
SomeRandomBloke 1:5a0e7a58d980 100 *
SomeRandomBloke 1:5a0e7a58d980 101 */
SomeRandomBloke 1:5a0e7a58d980 102 void begin();
SomeRandomBloke 1:5a0e7a58d980 103 /**
SomeRandomBloke 1:5a0e7a58d980 104 * Read a register, return its value as int
SomeRandomBloke 1:5a0e7a58d980 105 * @param regAddr The address to read
SomeRandomBloke 1:5a0e7a58d980 106 * @return value in register
SomeRandomBloke 1:5a0e7a58d980 107 */
SomeRandomBloke 1:5a0e7a58d980 108 int readReg(char regAddr);
SomeRandomBloke 1:5a0e7a58d980 109 /**
SomeRandomBloke 1:5a0e7a58d980 110 * Read a value from a pair of registers, return as int
SomeRandomBloke 1:5a0e7a58d980 111 * @param regAddr The address to read
SomeRandomBloke 1:5a0e7a58d980 112 * @return Value from 2 consecutive registers
SomeRandomBloke 1:5a0e7a58d980 113 */
SomeRandomBloke 1:5a0e7a58d980 114 int readVal(char regAddr);
SomeRandomBloke 1:5a0e7a58d980 115 /**
SomeRandomBloke 1:5a0e7a58d980 116 * Calculate the heading
SomeRandomBloke 1:5a0e7a58d980 117 * @return heading in degrees
SomeRandomBloke 1:5a0e7a58d980 118 */
SomeRandomBloke 1:5a0e7a58d980 119 float getHeading();
JimCarver 5:f3abe901c33a 120
JimCarver 5:f3abe901c33a 121 /**
JimCarver 5:f3abe901c33a 122 * Perform a read on the X, Y and Z values, converted to microteslas.
JimCarver 5:f3abe901c33a 123 * @paran mag Pointer to the 3 element array whare the results will be placed
JimCarver 5:f3abe901c33a 124 */
JimCarver 5:f3abe901c33a 125 void ReadXYZ(float * mag);
JimCarver 5:f3abe901c33a 126
JimCarver 5:f3abe901c33a 127 /**
JimCarver 5:f3abe901c33a 128 * Perform a read on the raw X, Y and Z values.
JimCarver 5:f3abe901c33a 129 * @paran mag Pointer to the 3 element array whare the results will be placed
JimCarver 5:f3abe901c33a 130 */
JimCarver 5:f3abe901c33a 131 void ReadXYZraw(int16_t * mag_raw);
JimCarver 5:f3abe901c33a 132
SomeRandomBloke 1:5a0e7a58d980 133 /**
SomeRandomBloke 1:5a0e7a58d980 134 * Perform a read on the X, Y and Z values.
SomeRandomBloke 1:5a0e7a58d980 135 * @param xVal Pointer to X value
SomeRandomBloke 1:5a0e7a58d980 136 * @param yVal Pointer to Y value
SomeRandomBloke 1:5a0e7a58d980 137 * @param zVal Pointer to Z value
SomeRandomBloke 1:5a0e7a58d980 138 */
SomeRandomBloke 1:5a0e7a58d980 139 void getValues(int *xVal, int *yVal, int *zVal);
SomeRandomBloke 1:5a0e7a58d980 140 /**
SomeRandomBloke 1:5a0e7a58d980 141 * Set the calibration parameters if required.
SomeRandomBloke 1:5a0e7a58d980 142 * @param minX Minimum value for X range
SomeRandomBloke 1:5a0e7a58d980 143 * @param maxX Maximum value for X range
SomeRandomBloke 1:5a0e7a58d980 144 * @param minY Minimum value for Y range
SomeRandomBloke 1:5a0e7a58d980 145 * @param maxY maximum value for Y range
SomeRandomBloke 1:5a0e7a58d980 146 */
SomeRandomBloke 1:5a0e7a58d980 147 void setCalibration(int minX, int maxX, int minY, int maxY);
mmaas 6:f510561f6107 148 /**
mmaas 6:f510561f6107 149 * Perfrom the calibration process.
mmaas 6:f510561f6107 150 * @param calPin GPIO pinName to use for coordinating the board rotation
mmaas 6:f510561f6107 151 * @param activeValue the GPIO pin value when input asserted
mmaas 6:f510561f6107 152 */
mmaas 6:f510561f6107 153 void calXY(PinName pin, int activeValue);
SomeRandomBloke 2:fb8024297377 154
SomeRandomBloke 2:fb8024297377 155 private:
SomeRandomBloke 4:cf40601402b7 156 I2C _i2c;
SomeRandomBloke 2:fb8024297377 157 int _i2c_address;
SomeRandomBloke 2:fb8024297377 158 Serial *_pc;
SomeRandomBloke 2:fb8024297377 159 bool _debug;
SomeRandomBloke 2:fb8024297377 160 int _avgX, _avgY;
JimCarver 5:f3abe901c33a 161 int x, y, z;
SomeRandomBloke 2:fb8024297377 162
SomeRandomBloke 0:63a8594a3866 163 };
SomeRandomBloke 1:5a0e7a58d980 164 #endif