fastest sampling rate

Dependencies:   mbed

Committer:
TimeString
Date:
Mon Feb 03 02:45:03 2014 +0000
Revision:
0:459f1731fa6a
fastest sampling rate for all sensors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
TimeString 0:459f1731fa6a 1
TimeString 0:459f1731fa6a 2 #include "MAG3110.h"
TimeString 0:459f1731fa6a 3 #include "mbed.h"
TimeString 0:459f1731fa6a 4
TimeString 0:459f1731fa6a 5 /******************************************************************************
TimeString 0:459f1731fa6a 6 * Constructors
TimeString 0:459f1731fa6a 7 ******************************************************************************/
TimeString 0:459f1731fa6a 8 MAG3110::MAG3110(PinName sda, PinName scl, int addr): _i2c(sda, scl),
TimeString 0:459f1731fa6a 9 _i2c_address(addr), _pc(NULL), _debug(false)
TimeString 0:459f1731fa6a 10 {
TimeString 0:459f1731fa6a 11 begin();
TimeString 0:459f1731fa6a 12 }
TimeString 0:459f1731fa6a 13
TimeString 0:459f1731fa6a 14 MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl),
TimeString 0:459f1731fa6a 15 _i2c_address(0x1D), _pc(pc), _debug(true)
TimeString 0:459f1731fa6a 16 {
TimeString 0:459f1731fa6a 17 begin();
TimeString 0:459f1731fa6a 18 }
TimeString 0:459f1731fa6a 19
TimeString 0:459f1731fa6a 20 void MAG3110::begin()
TimeString 0:459f1731fa6a 21 {
TimeString 0:459f1731fa6a 22 char cmd[2];
TimeString 0:459f1731fa6a 23
TimeString 0:459f1731fa6a 24 cmd[0] = MAG_CTRL_REG2;
TimeString 0:459f1731fa6a 25 cmd[1] = 0x80;
TimeString 0:459f1731fa6a 26 _i2c.write(_i2c_address, cmd, 2);
TimeString 0:459f1731fa6a 27
TimeString 0:459f1731fa6a 28 cmd[0] = MAG_CTRL_REG1;
TimeString 0:459f1731fa6a 29 cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE;
TimeString 0:459f1731fa6a 30 _i2c.write(_i2c_address, cmd, 2);
TimeString 0:459f1731fa6a 31
TimeString 0:459f1731fa6a 32 // No adjustment initially
TimeString 0:459f1731fa6a 33 _avgX = 0;
TimeString 0:459f1731fa6a 34 _avgY = 0;
TimeString 0:459f1731fa6a 35 }
TimeString 0:459f1731fa6a 36
TimeString 0:459f1731fa6a 37 // Read a single byte form 8 bit register, return as int
TimeString 0:459f1731fa6a 38 int MAG3110::readReg(char regAddr)
TimeString 0:459f1731fa6a 39 {
TimeString 0:459f1731fa6a 40 char cmd[1];
TimeString 0:459f1731fa6a 41
TimeString 0:459f1731fa6a 42 cmd[0] = regAddr;
TimeString 0:459f1731fa6a 43 _i2c.write(_i2c_address, cmd, 1);
TimeString 0:459f1731fa6a 44
TimeString 0:459f1731fa6a 45 cmd[0] = 0x00;
TimeString 0:459f1731fa6a 46 _i2c.read(_i2c_address, cmd, 1);
TimeString 0:459f1731fa6a 47 return (int)( cmd[0]);
TimeString 0:459f1731fa6a 48 }
TimeString 0:459f1731fa6a 49
TimeString 0:459f1731fa6a 50
TimeString 0:459f1731fa6a 51 // read a register per, pass first reg value, reading 2 bytes increments register
TimeString 0:459f1731fa6a 52 // Reads MSB first then LSB
TimeString 0:459f1731fa6a 53 int MAG3110::readVal(char regAddr)
TimeString 0:459f1731fa6a 54 {
TimeString 0:459f1731fa6a 55 char cmd[2];
TimeString 0:459f1731fa6a 56
TimeString 0:459f1731fa6a 57 cmd[0] = regAddr;
TimeString 0:459f1731fa6a 58 _i2c.write(_i2c_address, cmd, 1);
TimeString 0:459f1731fa6a 59
TimeString 0:459f1731fa6a 60 cmd[0] = 0x00;
TimeString 0:459f1731fa6a 61 cmd[1] = 0x00;
TimeString 0:459f1731fa6a 62 _i2c.read(_i2c_address, cmd, 2);
TimeString 0:459f1731fa6a 63 return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB
TimeString 0:459f1731fa6a 64 }
TimeString 0:459f1731fa6a 65
TimeString 0:459f1731fa6a 66
TimeString 0:459f1731fa6a 67 float MAG3110::getHeading()
TimeString 0:459f1731fa6a 68 {
TimeString 0:459f1731fa6a 69 int xVal = readVal(MAG_OUT_X_MSB);
TimeString 0:459f1731fa6a 70 int yVal = readVal(MAG_OUT_Y_MSB);
TimeString 0:459f1731fa6a 71 return (atan2((double)(yVal - _avgY),(double)(xVal - _avgX)))*180/PI;
TimeString 0:459f1731fa6a 72 }
TimeString 0:459f1731fa6a 73
TimeString 0:459f1731fa6a 74 void MAG3110::getValues(int *xVal, int *yVal, int *zVal)
TimeString 0:459f1731fa6a 75 {
TimeString 0:459f1731fa6a 76 *xVal = readVal(MAG_OUT_X_MSB);
TimeString 0:459f1731fa6a 77 *yVal = readVal(MAG_OUT_Y_MSB);
TimeString 0:459f1731fa6a 78 *zVal = readVal(MAG_OUT_Z_MSB);
TimeString 0:459f1731fa6a 79 }
TimeString 0:459f1731fa6a 80
TimeString 0:459f1731fa6a 81
TimeString 0:459f1731fa6a 82 void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY )
TimeString 0:459f1731fa6a 83 {
TimeString 0:459f1731fa6a 84 _avgX=(maxX+minX)/2;
TimeString 0:459f1731fa6a 85 _avgY=(maxY+minY)/2;
TimeString 0:459f1731fa6a 86 }
TimeString 0:459f1731fa6a 87
TimeString 0:459f1731fa6a 88
TimeString 0:459f1731fa6a 89
TimeString 0:459f1731fa6a 90
TimeString 0:459f1731fa6a 91