FRDM-KL46Z board sLCD demo code
Fork of FRDM-KL46Z LCD rtc Demo by
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 // Fields in registers 00039 // CTRL_REG1: dr2,dr1,dr0 os1,os0 fr tm ac 00040 00041 // Sampling rate from 80Hz down to 0.625Hz 00042 #define MAG_3110_SAMPLE80 0 00043 #define MAG_3110_SAMPLE40 0x20 00044 #define MAG_3110_SAMPLE20 0x40 00045 #define MAG_3110_SAMPLE10 0x60 00046 #define MAG_3110_SAMPLE5 0x80 00047 #define MAG_3110_SAMPLE2_5 0xA0 00048 #define MAG_3110_SAMPLE1_25 0xC0 00049 #define MAG_3110_SAMPLE0_625 0xE0 00050 00051 // How many samples to average (lowers data rate) 00052 #define MAG_3110_OVERSAMPLE1 0 00053 #define MAG_3110_OVERSAMPLE2 0x08 00054 #define MAG_3110_OVERSAMPLE3 0x10 00055 #define MAG_3110_OVERSAMPLE4 0x18 00056 00057 // read only 1 byte per axis 00058 #define MAG_3110_FASTREAD 0x04 00059 // do one measurement (even if in standby mode) 00060 #define MAG_3110_TRIGGER 0x02 00061 // put in active mode 00062 #define MAG_3110_ACTIVE 0x01 00063 00064 // CTRL_REG2: AUTO_MRST_EN _ RAW MAG_RST _ _ _ _ _ 00065 // reset sensor after each reading 00066 #define MAG_3110_AUTO_MRST_EN 0x80 00067 // don't subtract user offsets 00068 #define MAG_3110_RAW 0x20 00069 // reset magnetic sensor after too-large field 00070 #define MAG_3110_MAG_RST 0x10 00071 00072 // DR_STATUS Register ZYXOW ZOW YOW XOW ZYXDR ZDR YDR XDR 00073 #define MAG_3110_ZYXDR 0x08 00074 00075 /** 00076 * MAG3110 Class to read X/Y/Z data from the magentometer 00077 * 00078 */ 00079 class MAG3110 00080 { 00081 public: 00082 /** 00083 * Main constructor 00084 * @param sda SDA pin 00085 * @param sdl SCL pin 00086 * @param addr addr of the I2C peripheral 00087 */ 00088 MAG3110(PinName sda, PinName scl); 00089 /** 00090 * Debug version of constructor 00091 * @param sda SDA pin 00092 * @param sdl SCL pin 00093 * @param addr Address of the I2C peripheral 00094 * @param pc Serial object to output debug messages 00095 */ 00096 MAG3110(PinName sda, PinName scl, Serial *pc); //pass serial for debug 00097 /** 00098 * Setup the Magnetometer 00099 * 00100 */ 00101 void begin(); 00102 /** 00103 * Read a register, return its value as int 00104 * @param regAddr The address to read 00105 * @return value in register 00106 */ 00107 int readReg(char regAddr); 00108 /** 00109 * Read a value from a pair of registers, return as int 00110 * @param regAddr The address to read 00111 * @return Value from 2 consecutive registers 00112 */ 00113 int readVal(char regAddr); 00114 /** 00115 * Calculate the heading 00116 * @return heading in degrees 00117 */ 00118 float getHeading(); 00119 /** 00120 * Perform a read on the X, Y and Z values. 00121 * @param xVal Pointer to X value 00122 * @param yVal Pointer to Y value 00123 * @param zVal Pointer to Z value 00124 */ 00125 void getValues(int *xVal, int *yVal, int *zVal); 00126 /** 00127 * Set the calibration parameters if required. 00128 * @param minX Minimum value for X range 00129 * @param maxX Maximum value for X range 00130 * @param minY Minimum value for Y range 00131 * @param maxY maximum value for Y range 00132 */ 00133 void setCalibration(int minX, int maxX, int minY, int maxY); 00134 00135 private: 00136 I2C _i2c; 00137 int _i2c_address; 00138 Serial *_pc; 00139 bool _debug; 00140 int _avgX, _avgY; 00141 00142 }; 00143 #endif
Generated on Fri Jul 15 2022 16:32:49 by 1.7.2