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: MotionSensor SDFileSystem mbed
Fork of TanecCon by
MAG3110.h@0:029ef267b1bc, 2017-02-10 (annotated)
- Committer:
- Nike3221
- Date:
- Fri Feb 10 08:39:51 2017 +0000
- Revision:
- 0:029ef267b1bc
tanekon
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Nike3221 | 0:029ef267b1bc | 1 | /* |
| Nike3221 | 0:029ef267b1bc | 2 | * MAG3110 Sensor Library for mbed |
| Nike3221 | 0:029ef267b1bc | 3 | * TODO: Add proper header |
| Nike3221 | 0:029ef267b1bc | 4 | */ |
| Nike3221 | 0:029ef267b1bc | 5 | |
| Nike3221 | 0:029ef267b1bc | 6 | #ifndef MAG3110_H |
| Nike3221 | 0:029ef267b1bc | 7 | #define MAG3110_H |
| Nike3221 | 0:029ef267b1bc | 8 | |
| Nike3221 | 0:029ef267b1bc | 9 | #include "mbed.h" |
| Nike3221 | 0:029ef267b1bc | 10 | #include "MotionSensor.h" |
| Nike3221 | 0:029ef267b1bc | 11 | |
| Nike3221 | 0:029ef267b1bc | 12 | #define PI 3.14159265359 |
| Nike3221 | 0:029ef267b1bc | 13 | |
| Nike3221 | 0:029ef267b1bc | 14 | #define MAG_ADDR 0x1D |
| Nike3221 | 0:029ef267b1bc | 15 | |
| Nike3221 | 0:029ef267b1bc | 16 | // define registers |
| Nike3221 | 0:029ef267b1bc | 17 | #define MAG_DR_STATUS 0x00 |
| Nike3221 | 0:029ef267b1bc | 18 | #define MAG_OUT_X_MSB 0x01 |
| Nike3221 | 0:029ef267b1bc | 19 | #define MAG_OUT_X_LSB 0x02 |
| Nike3221 | 0:029ef267b1bc | 20 | #define MAG_OUT_Y_MSB 0x03 |
| Nike3221 | 0:029ef267b1bc | 21 | #define MAG_OUT_Y_LSB 0x04 |
| Nike3221 | 0:029ef267b1bc | 22 | #define MAG_OUT_Z_MSB 0x05 |
| Nike3221 | 0:029ef267b1bc | 23 | #define MAG_OUT_Z_LSB 0x06 |
| Nike3221 | 0:029ef267b1bc | 24 | #define MAG_WHOAMI 0x07 |
| Nike3221 | 0:029ef267b1bc | 25 | #define MAG_SYSMOD 0x08 |
| Nike3221 | 0:029ef267b1bc | 26 | #define MAG_OFF_X_MSB 0x09 |
| Nike3221 | 0:029ef267b1bc | 27 | #define MAG_OFF_X_LSB 0x0A |
| Nike3221 | 0:029ef267b1bc | 28 | #define MAG_OFF_Y_MSB 0x0B |
| Nike3221 | 0:029ef267b1bc | 29 | #define MAG_OFF_Y_LSB 0x0C |
| Nike3221 | 0:029ef267b1bc | 30 | #define MAG_OFF_Z_MSB 0x0D |
| Nike3221 | 0:029ef267b1bc | 31 | #define MAG_OFF_Z_LSB 0x0E |
| Nike3221 | 0:029ef267b1bc | 32 | #define MAG_DIE_TEMP 0x0F |
| Nike3221 | 0:029ef267b1bc | 33 | #define MAG_CTRL_REG1 0x10 |
| Nike3221 | 0:029ef267b1bc | 34 | #define MAG_CTRL_REG2 0x11 |
| Nike3221 | 0:029ef267b1bc | 35 | |
| Nike3221 | 0:029ef267b1bc | 36 | // what should WHO_AM_I return? |
| Nike3221 | 0:029ef267b1bc | 37 | #define MAG_3110_WHO_AM_I_VALUE 0xC4 |
| Nike3221 | 0:029ef267b1bc | 38 | |
| Nike3221 | 0:029ef267b1bc | 39 | |
| Nike3221 | 0:029ef267b1bc | 40 | // Fields in registers |
| Nike3221 | 0:029ef267b1bc | 41 | // CTRL_REG1: dr2,dr1,dr0 os1,os0 fr tm ac |
| Nike3221 | 0:029ef267b1bc | 42 | |
| Nike3221 | 0:029ef267b1bc | 43 | // Sampling rate from 80Hz down to 0.625Hz |
| Nike3221 | 0:029ef267b1bc | 44 | #define MAG_3110_SAMPLE80 0 |
| Nike3221 | 0:029ef267b1bc | 45 | #define MAG_3110_SAMPLE40 0x20 |
| Nike3221 | 0:029ef267b1bc | 46 | #define MAG_3110_SAMPLE20 0x40 |
| Nike3221 | 0:029ef267b1bc | 47 | #define MAG_3110_SAMPLE10 0x60 |
| Nike3221 | 0:029ef267b1bc | 48 | #define MAG_3110_SAMPLE5 0x80 |
| Nike3221 | 0:029ef267b1bc | 49 | #define MAG_3110_SAMPLE2_5 0xA0 |
| Nike3221 | 0:029ef267b1bc | 50 | #define MAG_3110_SAMPLE1_25 0xC0 |
| Nike3221 | 0:029ef267b1bc | 51 | #define MAG_3110_SAMPLE0_625 0xE0 |
| Nike3221 | 0:029ef267b1bc | 52 | |
| Nike3221 | 0:029ef267b1bc | 53 | // How many samples to average (lowers data rate) |
| Nike3221 | 0:029ef267b1bc | 54 | #define MAG_3110_OVERSAMPLE1 0 |
| Nike3221 | 0:029ef267b1bc | 55 | #define MAG_3110_OVERSAMPLE2 0x08 |
| Nike3221 | 0:029ef267b1bc | 56 | #define MAG_3110_OVERSAMPLE3 0x10 |
| Nike3221 | 0:029ef267b1bc | 57 | #define MAG_3110_OVERSAMPLE4 0x18 |
| Nike3221 | 0:029ef267b1bc | 58 | |
| Nike3221 | 0:029ef267b1bc | 59 | // read only 1 byte per axis |
| Nike3221 | 0:029ef267b1bc | 60 | #define MAG_3110_FASTREAD 0x04 |
| Nike3221 | 0:029ef267b1bc | 61 | // do one measurement (even if in standby mode) |
| Nike3221 | 0:029ef267b1bc | 62 | #define MAG_3110_TRIGGER 0x02 |
| Nike3221 | 0:029ef267b1bc | 63 | // put in active mode |
| Nike3221 | 0:029ef267b1bc | 64 | #define MAG_3110_ACTIVE 0x01 |
| Nike3221 | 0:029ef267b1bc | 65 | |
| Nike3221 | 0:029ef267b1bc | 66 | // CTRL_REG2: AUTO_MRST_EN _ RAW MAG_RST _ _ _ _ _ |
| Nike3221 | 0:029ef267b1bc | 67 | // reset sensor after each reading |
| Nike3221 | 0:029ef267b1bc | 68 | #define MAG_3110_AUTO_MRST_EN 0x80 |
| Nike3221 | 0:029ef267b1bc | 69 | // don't subtract user offsets |
| Nike3221 | 0:029ef267b1bc | 70 | #define MAG_3110_RAW 0x20 |
| Nike3221 | 0:029ef267b1bc | 71 | // reset magnetic sensor after too-large field |
| Nike3221 | 0:029ef267b1bc | 72 | #define MAG_3110_MAG_RST 0x10 |
| Nike3221 | 0:029ef267b1bc | 73 | |
| Nike3221 | 0:029ef267b1bc | 74 | // DR_STATUS Register ZYXOW ZOW YOW XOW ZYXDR ZDR YDR XDR |
| Nike3221 | 0:029ef267b1bc | 75 | #define MAG_3110_ZYXDR 0x08 |
| Nike3221 | 0:029ef267b1bc | 76 | |
| Nike3221 | 0:029ef267b1bc | 77 | /** |
| Nike3221 | 0:029ef267b1bc | 78 | * MAG3110 Class to read X/Y/Z data from the magentometer |
| Nike3221 | 0:029ef267b1bc | 79 | * |
| Nike3221 | 0:029ef267b1bc | 80 | */ |
| Nike3221 | 0:029ef267b1bc | 81 | class MAG3110 : public MotionSensor |
| Nike3221 | 0:029ef267b1bc | 82 | { |
| Nike3221 | 0:029ef267b1bc | 83 | public: |
| Nike3221 | 0:029ef267b1bc | 84 | /** |
| Nike3221 | 0:029ef267b1bc | 85 | * Main constructor |
| Nike3221 | 0:029ef267b1bc | 86 | * @param sda SDA pin |
| Nike3221 | 0:029ef267b1bc | 87 | * @param sdl SCL pin |
| Nike3221 | 0:029ef267b1bc | 88 | * @param addr addr of the I2C peripheral |
| Nike3221 | 0:029ef267b1bc | 89 | */ |
| Nike3221 | 0:029ef267b1bc | 90 | MAG3110(PinName sda, PinName scl); |
| Nike3221 | 0:029ef267b1bc | 91 | |
| Nike3221 | 0:029ef267b1bc | 92 | void enable(void); |
| Nike3221 | 0:029ef267b1bc | 93 | void disable(void); |
| Nike3221 | 0:029ef267b1bc | 94 | uint32_t sampleRate(uint32_t fequency); |
| Nike3221 | 0:029ef267b1bc | 95 | uint32_t whoAmI(void); |
| Nike3221 | 0:029ef267b1bc | 96 | uint32_t dataReady(void); |
| Nike3221 | 0:029ef267b1bc | 97 | void getX(int16_t * x); |
| Nike3221 | 0:029ef267b1bc | 98 | void getY(int16_t * y); |
| Nike3221 | 0:029ef267b1bc | 99 | void getZ(int16_t * z); |
| Nike3221 | 0:029ef267b1bc | 100 | void getX(float * x); |
| Nike3221 | 0:029ef267b1bc | 101 | void getY(float * y); |
| Nike3221 | 0:029ef267b1bc | 102 | void getZ(float * z); |
| Nike3221 | 0:029ef267b1bc | 103 | void getAxis(MotionSensorDataCounts &data); |
| Nike3221 | 0:029ef267b1bc | 104 | void getAxis(MotionSensorDataUnits &data); |
| Nike3221 | 0:029ef267b1bc | 105 | void readRegs(int addr, uint8_t * data, int len); |
| Nike3221 | 0:029ef267b1bc | 106 | |
| Nike3221 | 0:029ef267b1bc | 107 | private: |
| Nike3221 | 0:029ef267b1bc | 108 | I2C m_i2c; |
| Nike3221 | 0:029ef267b1bc | 109 | char m_addr; |
| Nike3221 | 0:029ef267b1bc | 110 | int16_t getMagAxis(uint8_t addr); |
| Nike3221 | 0:029ef267b1bc | 111 | void writeRegs(uint8_t * data, int len); |
| Nike3221 | 0:029ef267b1bc | 112 | |
| Nike3221 | 0:029ef267b1bc | 113 | }; |
| Nike3221 | 0:029ef267b1bc | 114 | #endif |
