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