blablabla

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAG3110.cpp Source File

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 
00091