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.h
00001 #ifndef MMC5883L_H 00002 #define MMC5883L_H 00003 00004 #include "mbed.h" 00005 00006 /* 00007 * Defines 00008 */ 00009 00010 //----------- 00011 // Registers 00012 //----------- 00013 #define INTERNAL_CONTROL_0 0x08 00014 #define INTERNAL_CONTROL_1 0x09 00015 #define INTERNAL_CONTROL_2 0x0A 00016 #define OUTPUT_REG 0x00 00017 #define STATUS_REG 0x07 00018 00019 #define OUTPUT_RATE_OFF 0x00 00020 #define OUTPUT_RATE_14 0x01 00021 #define OUTPUT_RATE_5 0x02 00022 #define OUTPUT_RATE_2_2 0x03 00023 #define OUTPUT_RATE_1 0x04 00024 #define OUTPUT_RATE_0_5 0x05 00025 #define OUTPUT_RATE_0_25 0x06 00026 #define OUTPUT_RATE_0_12 0x07 00027 #define OUTPUT_RATE_0_06 0x08 00028 #define OUTPUT_RATE_0_03 0x09 00029 #define OUTPUT_RATE_0_015 0x0A 00030 #define DRDY_INTERRUPT_EN 0x40 00031 #define MOTION_INTERRUPT_EN 0x20 00032 00033 #define SW_RST 0x80 00034 #define Z_INHIBIT 0x40 00035 #define Y_INHIBIT 0x20 00036 #define X_INHIBIT 0x10 00037 #define DATARATE_100 0x00 00038 #define DATARATE_200 0x01 00039 #define DATARATE_400 0x02 00040 #define DATARATE_600 0x03 00041 00042 #define MAGNETIC_MEASUREMENT_START 0x01 00043 #define TEMP_MEASUREMENT_START 0x02 00044 #define MOTION_DETECT_START 0x04 00045 #define FLIP_SET 0x08 00046 #define FLIP_RESET 0x10 00047 00048 00049 // status register 00050 #define STATUS_M_DONE 0x01 00051 #define STATUS_T_DONE 0x02 00052 #define STATUS_MOTION_DET 0x04 00053 00054 // Utility 00055 #ifndef M_PI 00056 #define M_PI 3.1415926535897932384626433832795 00057 #endif 00058 00059 #define PI2 (2*M_PI) 00060 #define RAD_TO_DEG (180.0/M_PI) 00061 #define DEG_TO_RAD (M_PI/180.0) 00062 00063 // Once you have your heading, you must then add your 'Declination Angle', 00064 // which is the 'Error' of the magnetic field in your location. 00065 // Find yours here: http://www.magnetic-declination.com/ 00066 // Mine is: -1° 13' WEST which is -1.2167 Degrees, or (which we need) 00067 // 0,021234839232597676519238237683278 radians, I will use 0.02123 00068 // If you cannot find your Declination, put 0, your compass will be slightly off. 00069 00070 #define DECLINATION_ANGLE -0.02123 00071 //#define DECLINATION_ANGLE 0 00072 00073 00074 /** 00075 * The MMC5883L 3-Axis Digital Compass IC 00076 */ 00077 class MMC5883L 00078 { 00079 00080 public: 00081 00082 /** 00083 * The I2C address that can be passed directly to i2c object (it's already shifted 1 bit left). 00084 */ 00085 static const int I2C_ADDRESS = 0x60; 00086 00087 /** 00088 * Constructor. 00089 * 00090 * Calls init function 00091 * 00092 * @param sda - mbed pin to use for the SDA I2C line. 00093 * @param scl - mbed pin to use for the SCL I2C line. 00094 */ 00095 MMC5883L(PinName sda, PinName scl); 00096 00097 /** 00098 * Constructor that accepts external i2c interface object. 00099 * 00100 * Calls init function 00101 * 00102 * @param i2c The I2C interface object to use. 00103 */ 00104 MMC5883L(I2C &i2c) : i2c_(i2c) { 00105 init(); 00106 } 00107 00108 ~MMC5883L(); 00109 00110 void init(); 00111 00112 void setConfiguration0(char); 00113 void setConfiguration1(char); 00114 void setConfiguration2(char); 00115 00116 char getConfiguration0(); 00117 char getConfiguration1(); 00118 char getConfiguration2(); 00119 00120 void flipSet(); 00121 void flipReset(); 00122 00123 void startMeasurement_temp(); 00124 void startMeasurement_mag(); 00125 00126 void getXYZ_RAW(int16_t raw[3]); 00127 void getXYZ_OffsetRemoved_RAW(int16_t raw[3]); 00128 void getXYZ_nT(int32_t raw[3]); 00129 void getXYZ_OffsetRemoved_nT(int32_t raw[3]); 00130 00131 00132 char getStatus(); 00133 00134 double getHeadingXY(); 00135 double getHeadingXY(int16_t output[3]); 00136 double getHeadingXY(int32_t output[3]); 00137 double getHeadingXYDeg(); 00138 double getHeadingXYDeg_RAW(int16_t output[3]); 00139 double getHeadingXYDeg_nT(int32_t output[3]); 00140 00141 private: 00142 00143 I2C &i2c_; 00144 00145 /** 00146 * The raw buffer for allocating I2C object in its own without heap memory. 00147 */ 00148 char i2cRaw[sizeof(I2C)]; 00149 }; 00150 00151 #endif // MMC5883L 00152
Generated on Fri Sep 2 2022 00:57:54 by
1.7.2