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.
MMC5883L/MMC5883L.h@4:3618abce1646, 2022-04-21 (annotated)
- Committer:
- vitpetrik
- Date:
- Thu Apr 21 13:19:41 2022 +0000
- Revision:
- 4:3618abce1646
Share for Stepan Oslejsek
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| vitpetrik | 4:3618abce1646 | 1 | #ifndef MMC5883L_H |
| vitpetrik | 4:3618abce1646 | 2 | #define MMC5883L_H |
| vitpetrik | 4:3618abce1646 | 3 | |
| vitpetrik | 4:3618abce1646 | 4 | #include "mbed.h" |
| vitpetrik | 4:3618abce1646 | 5 | |
| vitpetrik | 4:3618abce1646 | 6 | /* |
| vitpetrik | 4:3618abce1646 | 7 | * Defines |
| vitpetrik | 4:3618abce1646 | 8 | */ |
| vitpetrik | 4:3618abce1646 | 9 | |
| vitpetrik | 4:3618abce1646 | 10 | //----------- |
| vitpetrik | 4:3618abce1646 | 11 | // Registers |
| vitpetrik | 4:3618abce1646 | 12 | //----------- |
| vitpetrik | 4:3618abce1646 | 13 | #define INTERNAL_CONTROL_0 0x08 |
| vitpetrik | 4:3618abce1646 | 14 | #define INTERNAL_CONTROL_1 0x09 |
| vitpetrik | 4:3618abce1646 | 15 | #define INTERNAL_CONTROL_2 0x0A |
| vitpetrik | 4:3618abce1646 | 16 | #define OUTPUT_REG 0x00 |
| vitpetrik | 4:3618abce1646 | 17 | #define STATUS_REG 0x07 |
| vitpetrik | 4:3618abce1646 | 18 | |
| vitpetrik | 4:3618abce1646 | 19 | #define OUTPUT_RATE_OFF 0x00 |
| vitpetrik | 4:3618abce1646 | 20 | #define OUTPUT_RATE_14 0x01 |
| vitpetrik | 4:3618abce1646 | 21 | #define OUTPUT_RATE_5 0x02 |
| vitpetrik | 4:3618abce1646 | 22 | #define OUTPUT_RATE_2_2 0x03 |
| vitpetrik | 4:3618abce1646 | 23 | #define OUTPUT_RATE_1 0x04 |
| vitpetrik | 4:3618abce1646 | 24 | #define OUTPUT_RATE_0_5 0x05 |
| vitpetrik | 4:3618abce1646 | 25 | #define OUTPUT_RATE_0_25 0x06 |
| vitpetrik | 4:3618abce1646 | 26 | #define OUTPUT_RATE_0_12 0x07 |
| vitpetrik | 4:3618abce1646 | 27 | #define OUTPUT_RATE_0_06 0x08 |
| vitpetrik | 4:3618abce1646 | 28 | #define OUTPUT_RATE_0_03 0x09 |
| vitpetrik | 4:3618abce1646 | 29 | #define OUTPUT_RATE_0_015 0x0A |
| vitpetrik | 4:3618abce1646 | 30 | #define DRDY_INTERRUPT_EN 0x40 |
| vitpetrik | 4:3618abce1646 | 31 | #define MOTION_INTERRUPT_EN 0x20 |
| vitpetrik | 4:3618abce1646 | 32 | |
| vitpetrik | 4:3618abce1646 | 33 | #define SW_RST 0x80 |
| vitpetrik | 4:3618abce1646 | 34 | #define Z_INHIBIT 0x40 |
| vitpetrik | 4:3618abce1646 | 35 | #define Y_INHIBIT 0x20 |
| vitpetrik | 4:3618abce1646 | 36 | #define X_INHIBIT 0x10 |
| vitpetrik | 4:3618abce1646 | 37 | #define DATARATE_100 0x00 |
| vitpetrik | 4:3618abce1646 | 38 | #define DATARATE_200 0x01 |
| vitpetrik | 4:3618abce1646 | 39 | #define DATARATE_400 0x02 |
| vitpetrik | 4:3618abce1646 | 40 | #define DATARATE_600 0x03 |
| vitpetrik | 4:3618abce1646 | 41 | |
| vitpetrik | 4:3618abce1646 | 42 | #define MAGNETIC_MEASUREMENT_START 0x01 |
| vitpetrik | 4:3618abce1646 | 43 | #define TEMP_MEASUREMENT_START 0x02 |
| vitpetrik | 4:3618abce1646 | 44 | #define MOTION_DETECT_START 0x04 |
| vitpetrik | 4:3618abce1646 | 45 | #define FLIP_SET 0x08 |
| vitpetrik | 4:3618abce1646 | 46 | #define FLIP_RESET 0x10 |
| vitpetrik | 4:3618abce1646 | 47 | |
| vitpetrik | 4:3618abce1646 | 48 | |
| vitpetrik | 4:3618abce1646 | 49 | // status register |
| vitpetrik | 4:3618abce1646 | 50 | #define STATUS_M_DONE 0x01 |
| vitpetrik | 4:3618abce1646 | 51 | #define STATUS_T_DONE 0x02 |
| vitpetrik | 4:3618abce1646 | 52 | #define STATUS_MOTION_DET 0x04 |
| vitpetrik | 4:3618abce1646 | 53 | |
| vitpetrik | 4:3618abce1646 | 54 | // Utility |
| vitpetrik | 4:3618abce1646 | 55 | #ifndef M_PI |
| vitpetrik | 4:3618abce1646 | 56 | #define M_PI 3.1415926535897932384626433832795 |
| vitpetrik | 4:3618abce1646 | 57 | #endif |
| vitpetrik | 4:3618abce1646 | 58 | |
| vitpetrik | 4:3618abce1646 | 59 | #define PI2 (2*M_PI) |
| vitpetrik | 4:3618abce1646 | 60 | #define RAD_TO_DEG (180.0/M_PI) |
| vitpetrik | 4:3618abce1646 | 61 | #define DEG_TO_RAD (M_PI/180.0) |
| vitpetrik | 4:3618abce1646 | 62 | |
| vitpetrik | 4:3618abce1646 | 63 | // Once you have your heading, you must then add your 'Declination Angle', |
| vitpetrik | 4:3618abce1646 | 64 | // which is the 'Error' of the magnetic field in your location. |
| vitpetrik | 4:3618abce1646 | 65 | // Find yours here: http://www.magnetic-declination.com/ |
| vitpetrik | 4:3618abce1646 | 66 | // Mine is: -1° 13' WEST which is -1.2167 Degrees, or (which we need) |
| vitpetrik | 4:3618abce1646 | 67 | // 0,021234839232597676519238237683278 radians, I will use 0.02123 |
| vitpetrik | 4:3618abce1646 | 68 | // If you cannot find your Declination, put 0, your compass will be slightly off. |
| vitpetrik | 4:3618abce1646 | 69 | |
| vitpetrik | 4:3618abce1646 | 70 | #define DECLINATION_ANGLE -0.02123 |
| vitpetrik | 4:3618abce1646 | 71 | //#define DECLINATION_ANGLE 0 |
| vitpetrik | 4:3618abce1646 | 72 | |
| vitpetrik | 4:3618abce1646 | 73 | |
| vitpetrik | 4:3618abce1646 | 74 | /** |
| vitpetrik | 4:3618abce1646 | 75 | * The MMC5883L 3-Axis Digital Compass IC |
| vitpetrik | 4:3618abce1646 | 76 | */ |
| vitpetrik | 4:3618abce1646 | 77 | class MMC5883L |
| vitpetrik | 4:3618abce1646 | 78 | { |
| vitpetrik | 4:3618abce1646 | 79 | |
| vitpetrik | 4:3618abce1646 | 80 | public: |
| vitpetrik | 4:3618abce1646 | 81 | |
| vitpetrik | 4:3618abce1646 | 82 | /** |
| vitpetrik | 4:3618abce1646 | 83 | * The I2C address that can be passed directly to i2c object (it's already shifted 1 bit left). |
| vitpetrik | 4:3618abce1646 | 84 | */ |
| vitpetrik | 4:3618abce1646 | 85 | static const int I2C_ADDRESS = 0x60; |
| vitpetrik | 4:3618abce1646 | 86 | |
| vitpetrik | 4:3618abce1646 | 87 | /** |
| vitpetrik | 4:3618abce1646 | 88 | * Constructor. |
| vitpetrik | 4:3618abce1646 | 89 | * |
| vitpetrik | 4:3618abce1646 | 90 | * Calls init function |
| vitpetrik | 4:3618abce1646 | 91 | * |
| vitpetrik | 4:3618abce1646 | 92 | * @param sda - mbed pin to use for the SDA I2C line. |
| vitpetrik | 4:3618abce1646 | 93 | * @param scl - mbed pin to use for the SCL I2C line. |
| vitpetrik | 4:3618abce1646 | 94 | */ |
| vitpetrik | 4:3618abce1646 | 95 | MMC5883L(PinName sda, PinName scl); |
| vitpetrik | 4:3618abce1646 | 96 | |
| vitpetrik | 4:3618abce1646 | 97 | /** |
| vitpetrik | 4:3618abce1646 | 98 | * Constructor that accepts external i2c interface object. |
| vitpetrik | 4:3618abce1646 | 99 | * |
| vitpetrik | 4:3618abce1646 | 100 | * Calls init function |
| vitpetrik | 4:3618abce1646 | 101 | * |
| vitpetrik | 4:3618abce1646 | 102 | * @param i2c The I2C interface object to use. |
| vitpetrik | 4:3618abce1646 | 103 | */ |
| vitpetrik | 4:3618abce1646 | 104 | MMC5883L(I2C &i2c) : i2c_(i2c) { |
| vitpetrik | 4:3618abce1646 | 105 | init(); |
| vitpetrik | 4:3618abce1646 | 106 | } |
| vitpetrik | 4:3618abce1646 | 107 | |
| vitpetrik | 4:3618abce1646 | 108 | ~MMC5883L(); |
| vitpetrik | 4:3618abce1646 | 109 | |
| vitpetrik | 4:3618abce1646 | 110 | void init(); |
| vitpetrik | 4:3618abce1646 | 111 | |
| vitpetrik | 4:3618abce1646 | 112 | void setConfiguration0(char); |
| vitpetrik | 4:3618abce1646 | 113 | void setConfiguration1(char); |
| vitpetrik | 4:3618abce1646 | 114 | void setConfiguration2(char); |
| vitpetrik | 4:3618abce1646 | 115 | |
| vitpetrik | 4:3618abce1646 | 116 | char getConfiguration0(); |
| vitpetrik | 4:3618abce1646 | 117 | char getConfiguration1(); |
| vitpetrik | 4:3618abce1646 | 118 | char getConfiguration2(); |
| vitpetrik | 4:3618abce1646 | 119 | |
| vitpetrik | 4:3618abce1646 | 120 | void flipSet(); |
| vitpetrik | 4:3618abce1646 | 121 | void flipReset(); |
| vitpetrik | 4:3618abce1646 | 122 | |
| vitpetrik | 4:3618abce1646 | 123 | void startMeasurement_temp(); |
| vitpetrik | 4:3618abce1646 | 124 | void startMeasurement_mag(); |
| vitpetrik | 4:3618abce1646 | 125 | |
| vitpetrik | 4:3618abce1646 | 126 | void getXYZ_RAW(int16_t raw[3]); |
| vitpetrik | 4:3618abce1646 | 127 | void getXYZ_OffsetRemoved_RAW(int16_t raw[3]); |
| vitpetrik | 4:3618abce1646 | 128 | void getXYZ_nT(int32_t raw[3]); |
| vitpetrik | 4:3618abce1646 | 129 | void getXYZ_OffsetRemoved_nT(int32_t raw[3]); |
| vitpetrik | 4:3618abce1646 | 130 | |
| vitpetrik | 4:3618abce1646 | 131 | |
| vitpetrik | 4:3618abce1646 | 132 | char getStatus(); |
| vitpetrik | 4:3618abce1646 | 133 | |
| vitpetrik | 4:3618abce1646 | 134 | double getHeadingXY(); |
| vitpetrik | 4:3618abce1646 | 135 | double getHeadingXY(int16_t output[3]); |
| vitpetrik | 4:3618abce1646 | 136 | double getHeadingXY(int32_t output[3]); |
| vitpetrik | 4:3618abce1646 | 137 | double getHeadingXYDeg(); |
| vitpetrik | 4:3618abce1646 | 138 | double getHeadingXYDeg_RAW(int16_t output[3]); |
| vitpetrik | 4:3618abce1646 | 139 | double getHeadingXYDeg_nT(int32_t output[3]); |
| vitpetrik | 4:3618abce1646 | 140 | |
| vitpetrik | 4:3618abce1646 | 141 | private: |
| vitpetrik | 4:3618abce1646 | 142 | |
| vitpetrik | 4:3618abce1646 | 143 | I2C &i2c_; |
| vitpetrik | 4:3618abce1646 | 144 | |
| vitpetrik | 4:3618abce1646 | 145 | /** |
| vitpetrik | 4:3618abce1646 | 146 | * The raw buffer for allocating I2C object in its own without heap memory. |
| vitpetrik | 4:3618abce1646 | 147 | */ |
| vitpetrik | 4:3618abce1646 | 148 | char i2cRaw[sizeof(I2C)]; |
| vitpetrik | 4:3618abce1646 | 149 | }; |
| vitpetrik | 4:3618abce1646 | 150 | |
| vitpetrik | 4:3618abce1646 | 151 | #endif // MMC5883L |
| vitpetrik | 4:3618abce1646 | 152 |