MAG3110 library with calibrate operation included
Dependents: FRDM-KL46-Template AxedaGo-Freescal_FRDM-KL46Z revert AxedaGo-Freescal_FRDM-KL46Z HC-05_S2B_HelloWorld_WIZwiki-W7500_sensores
Fork of MAG3110 by
MAG3110.cpp
00001 00002 #include "MAG3110.h" 00003 #include "mbed.h" 00004 00005 /****************************************************************************** 00006 * Constructors 00007 ******************************************************************************/ 00008 MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl), 00009 _i2c_address(0x1D), _pc(NULL), _debug(false) 00010 { 00011 begin(); 00012 } 00013 00014 MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl), 00015 _i2c_address(0x1D), _pc(pc), _debug(true) 00016 { 00017 begin(); 00018 } 00019 00020 void MAG3110::begin() 00021 { 00022 char cmd[2]; 00023 00024 cmd[0] = MAG_CTRL_REG2; 00025 cmd[1] = 0x80; 00026 _i2c.write(_i2c_address, cmd, 2); 00027 00028 cmd[0] = MAG_CTRL_REG1; 00029 cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE; 00030 _i2c.write(_i2c_address, cmd, 2); 00031 00032 // No adjustment initially 00033 _avgX = 0; 00034 _avgY = 0; 00035 } 00036 00037 // Read a single byte form 8 bit register, return as int 00038 int MAG3110::readReg(char regAddr) 00039 { 00040 char cmd[1]; 00041 00042 cmd[0] = regAddr; 00043 _i2c.write(_i2c_address, cmd, 1); 00044 00045 cmd[0] = 0x00; 00046 _i2c.read(_i2c_address, cmd, 1); 00047 return (int)( cmd[0]); 00048 } 00049 00050 00051 // read a register per, pass first reg value, reading 2 bytes increments register 00052 // Reads MSB first then LSB 00053 int MAG3110::readVal(char regAddr) 00054 { 00055 char cmd[2]; 00056 00057 cmd[0] = regAddr; 00058 _i2c.write(_i2c_address, cmd, 1); 00059 00060 cmd[0] = 0x00; 00061 cmd[1] = 0x00; 00062 _i2c.read(_i2c_address, cmd, 2); 00063 return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB 00064 } 00065 00066 00067 float MAG3110::getHeading() 00068 { 00069 int xVal = readVal(MAG_OUT_X_MSB); 00070 int yVal = readVal(MAG_OUT_Y_MSB); 00071 return (atan2((double)(yVal - _avgY),(double)(xVal - _avgX)))*180/PI; 00072 } 00073 00074 void MAG3110::getValues(int *xVal, int *yVal, int *zVal) 00075 { 00076 *xVal = readVal(MAG_OUT_X_MSB); 00077 *yVal = readVal(MAG_OUT_Y_MSB); 00078 *zVal = readVal(MAG_OUT_Z_MSB); 00079 } 00080 00081 00082 void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY ) 00083 { 00084 _avgX=(maxX+minX)/2; 00085 _avgY=(maxY+minY)/2; 00086 } 00087 00088 00089 00090 void MAG3110::calXY(PinName pin, int activeValue ) 00091 { 00092 DigitalIn calPin(pin); 00093 int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY; 00094 00095 00096 // Wait for Button Press and Release before beginning calibration 00097 while(calPin != activeValue) {} 00098 while(calPin == activeValue) {} 00099 00100 00101 // Read initial values of magnetomoter - read it here to create a slight delay for calPin to settle 00102 tempXmax = tempXmin = readVal(MAG_OUT_X_MSB); 00103 tempYmax = tempYmin = readVal(MAG_OUT_Y_MSB); 00104 00105 // Update min and max values until calPin asserted again 00106 while(calPin != activeValue) { 00107 newX = readVal(MAG_OUT_X_MSB); 00108 newY = readVal(MAG_OUT_Y_MSB); 00109 if (newX > tempXmax) tempXmax = newX; 00110 if (newX < tempXmin) tempXmin = newX; 00111 if (newY > tempYmax) tempYmax = newY; 00112 if (newY < tempYmin) tempYmin = newY; 00113 } 00114 00115 setCalibration( tempXmin, tempXmax, tempYmin, tempYmax ); 00116 00117 } 00118
Generated on Sun Jul 24 2022 14:01:56 by 1.7.2