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