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