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