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.h
00001 /* 00002 * MAG3110 Sensor Library for mbed 00003 * TODO: Add proper header 00004 */ 00005 00006 #ifndef MAG3110_H 00007 #define MAG3110_H 00008 00009 #include "mbed.h" 00010 00011 #define PI 3.14159265359 00012 00013 #define MAG_ADDR 0x1D 00014 00015 // define registers 00016 #define MAG_DR_STATUS 0x00 00017 #define MAG_OUT_X_MSB 0x01 00018 #define MAG_OUT_X_LSB 0x02 00019 #define MAG_OUT_Y_MSB 0x03 00020 #define MAG_OUT_Y_LSB 0x04 00021 #define MAG_OUT_Z_MSB 0x05 00022 #define MAG_OUT_Z_LSB 0x06 00023 #define MAG_WHO_AM_I 0x07 00024 #define MAG_SYSMOD 0x08 00025 #define MAG_OFF_X_MSB 0x09 00026 #define MAG_OFF_X_LSB 0x0A 00027 #define MAG_OFF_Y_MSB 0x0B 00028 #define MAG_OFF_Y_LSB 0x0C 00029 #define MAG_OFF_Z_MSB 0x0D 00030 #define MAG_OFF_Z_LSB 0x0E 00031 #define MAG_DIE_TEMP 0x0F 00032 #define MAG_CTRL_REG1 0x10 00033 #define MAG_CTRL_REG2 0x11 00034 00035 // what should WHO_AM_I return? 00036 #define MAG_3110_WHO_AM_I_VALUE 0xC4 00037 00038 00039 // Fields in registers 00040 // CTRL_REG1: dr2,dr1,dr0 os1,os0 fr tm ac 00041 00042 // Sampling rate from 80Hz down to 0.625Hz 00043 #define MAG_3110_SAMPLE80 0 00044 #define MAG_3110_SAMPLE40 0x20 00045 #define MAG_3110_SAMPLE20 0x40 00046 #define MAG_3110_SAMPLE10 0x60 00047 #define MAG_3110_SAMPLE5 0x80 00048 #define MAG_3110_SAMPLE2_5 0xA0 00049 #define MAG_3110_SAMPLE1_25 0xC0 00050 #define MAG_3110_SAMPLE0_625 0xE0 00051 00052 // How many samples to average (lowers data rate) 00053 #define MAG_3110_OVERSAMPLE1 0 00054 #define MAG_3110_OVERSAMPLE2 0x08 00055 #define MAG_3110_OVERSAMPLE3 0x10 00056 #define MAG_3110_OVERSAMPLE4 0x18 00057 00058 // read only 1 byte per axis 00059 #define MAG_3110_FASTREAD 0x04 00060 // do one measurement (even if in standby mode) 00061 #define MAG_3110_TRIGGER 0x02 00062 // put in active mode 00063 #define MAG_3110_ACTIVE 0x01 00064 00065 // CTRL_REG2: AUTO_MRST_EN _ RAW MAG_RST _ _ _ _ _ 00066 // reset sensor after each reading 00067 #define MAG_3110_AUTO_MRST_EN 0x80 00068 // don't subtract user offsets 00069 #define MAG_3110_RAW 0x20 00070 // reset magnetic sensor after too-large field 00071 #define MAG_3110_MAG_RST 0x10 00072 00073 // DR_STATUS Register ZYXOW ZOW YOW XOW ZYXDR ZDR YDR XDR 00074 #define MAG_3110_ZYXDR 0x08 00075 00076 /** 00077 * MAG3110 Class to read X/Y/Z data from the magentometer 00078 * 00079 */ 00080 class MAG3110 00081 { 00082 public: 00083 /** 00084 * Main constructor 00085 * @param sda SDA pin 00086 * @param sdl SCL pin 00087 * @param addr addr of the I2C peripheral 00088 */ 00089 MAG3110(PinName sda, PinName scl, int i2c_adr=0x0E); 00090 /** 00091 * Setup the Magnetometer 00092 * 00093 */ 00094 void begin(void); 00095 /** 00096 * Read a register, return its value as int 00097 * @param regAddr The address to read 00098 * @return value in register 00099 */ 00100 int readReg(char regAddr); 00101 /** 00102 * Read a value from a pair of registers, return as int 00103 * @param regAddr The address to read 00104 * @return Value from 2 consecutive registers 00105 */ 00106 int16_t readVal(char regAddr); 00107 /** 00108 * Read all registers 00109 * @param buffer Buffer to store all registers 00110 * @return Value from register 0 00111 */ 00112 int readAll(char *buffer); 00113 /** 00114 * Calculate the heading 00115 * @return heading in degrees 00116 */ 00117 float getHeading(); 00118 /** 00119 * Perform a read on the X, Y and Z values. 00120 * @param xVal Pointer to X value 00121 * @param yVal Pointer to Y value 00122 * @param zVal Pointer to Z value 00123 */ 00124 void getValues(int *xVal, int *yVal, int *zVal); 00125 /** 00126 * Set the calibration parameters if required. 00127 * @param minX Minimum value for X range 00128 * @param maxX Maximum value for X range 00129 * @param minY Minimum value for Y range 00130 * @param maxY maximum value for Y range 00131 */ 00132 void setCalibration(int minX, int maxX, int minY, int maxY); 00133 /** 00134 * Read temperature. 00135 */ 00136 int8_t getTemperature(void); 00137 00138 private: 00139 I2C _i2c; 00140 int _i2c_address; 00141 int _avgX, _avgY; 00142 00143 }; 00144 #endif
Generated on Sat Jul 30 2022 15:40:23 by
