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: EventFramework mbed
MAG3110.cpp
00001 00002 #include "MAG3110.h" 00003 #include "mbed.h" 00004 00005 /****************************************************************************** 00006 * Constructors 00007 ******************************************************************************/ 00008 MAG3110::MAG3110(PinName sda, PinName scl,float dateRate, int overSample): _i2c(sda, scl), 00009 _i2c_address(0x0E<<1), _pc(NULL), _debug(false) 00010 { 00011 Setdr(dateRate); 00012 Setosr(overSample); 00013 begin(); 00014 } 00015 00016 MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl), 00017 _i2c_address(0x0E<<1), _pc(NULL), _debug(false),dr(MAG_3110_SAMPLE80),osr(MAG_3110_OVERSAMPLE2) 00018 { 00019 begin(); 00020 } 00021 00022 MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl), 00023 _i2c_address(0x0E<<1), _pc(pc), _debug(true),dr(MAG_3110_SAMPLE80),osr(MAG_3110_OVERSAMPLE2) 00024 { 00025 begin(); 00026 } 00027 00028 void MAG3110::begin() 00029 { 00030 00031 char cmd[2]; 00032 00033 cmd[0] = MAG_CTRL_REG2; 00034 cmd[1] = 0x80; 00035 _i2c.write(_i2c_address, cmd, 2); 00036 00037 cmd[0] = MAG_CTRL_REG1; 00038 // cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE; 00039 cmd[1] = dr+osr+MAG_3110_ACTIVE; 00040 _i2c.write(_i2c_address, cmd, 2); 00041 00042 // No adjustment initially 00043 _avgX = 0; 00044 _avgY = 0; 00045 } 00046 00047 // Read a single byte form 8 bit register, return as int 00048 int MAG3110::readReg(char regAddr) 00049 { 00050 char cmd[1]; 00051 00052 cmd[0] = regAddr; 00053 _i2c.write(_i2c_address, cmd, 1); 00054 00055 cmd[0] = 0x00; 00056 _i2c.read(_i2c_address, cmd, 1); 00057 return (int)( cmd[0]); 00058 } 00059 00060 00061 // read a register per, pass first reg value, reading 2 bytes increments register 00062 // Reads MSB first then LSB 00063 int MAG3110::readVal(char regAddr) 00064 { 00065 char cmd[2]; 00066 00067 cmd[0] = regAddr; 00068 _i2c.write(_i2c_address, cmd, 1); 00069 00070 cmd[0] = 0x00; 00071 cmd[1] = 0x00; 00072 _i2c.read(_i2c_address, cmd, 2); 00073 return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB 00074 } 00075 00076 00077 float MAG3110::getHeading() 00078 { 00079 int xVal = readVal(MAG_OUT_X_MSB); 00080 int yVal = readVal(MAG_OUT_Y_MSB); 00081 return (atan2((double)(yVal - _avgY),(double)(xVal - _avgX)))*180/PI; 00082 } 00083 00084 void MAG3110::getValues(int *xVal, int *yVal, int *zVal) 00085 { 00086 *xVal = readVal(MAG_OUT_X_MSB); 00087 *yVal = readVal(MAG_OUT_Y_MSB); 00088 *zVal = readVal(MAG_OUT_Z_MSB); 00089 } 00090 00091 00092 void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY ) 00093 { 00094 _avgX=(maxX+minX)/2; 00095 _avgY=(maxY+minY)/2; 00096 } 00097 00098 void MAG3110::Setdr(float dateRate) 00099 { 00100 if (dateRate==80) 00101 dr=MAG_3110_SAMPLE80; 00102 else if(dateRate==40) 00103 dr=MAG_3110_SAMPLE40; 00104 else if(dateRate==20) 00105 dr=MAG_3110_SAMPLE20; 00106 else if(dateRate==10) 00107 dr=MAG_3110_SAMPLE10; 00108 else if(dateRate==5) 00109 dr=MAG_3110_SAMPLE5; 00110 else if(dateRate==2.5) 00111 dr=MAG_3110_SAMPLE2_5; 00112 else if(dateRate==1.25) 00113 dr=MAG_3110_SAMPLE1_25; 00114 else if(dateRate==0.625) 00115 dr=MAG_3110_SAMPLE0_625; 00116 else 00117 dr=MAG_3110_SAMPLE80; 00118 } 00119 00120 void MAG3110::Setosr(int overSample) 00121 { 00122 switch(overSample) 00123 { 00124 case 16:osr=MAG_3110_OVERSAMPLE1;break; 00125 case 32:osr=MAG_3110_OVERSAMPLE2;break; 00126 case 64:osr=MAG_3110_OVERSAMPLE3;break; 00127 case 128:osr=MAG_3110_OVERSAMPLE4;break; 00128 default:osr=MAG_3110_OVERSAMPLE2;break; 00129 } 00130 } 00131 00132 void MAG3110::Overwrite_dr_osr(float dateRate,int overSample) 00133 { 00134 char cmd[2]; 00135 Setdr(dateRate); 00136 Setosr(overSample); 00137 cmd[0] = MAG_CTRL_REG1; 00138 cmd[1] = dr+osr+MAG_3110_ACTIVE; 00139 _i2c.write(_i2c_address, cmd, 2); 00140 }
Generated on Tue Jul 12 2022 18:55:06 by
1.7.2