This is the vcdMaker demo project. See http://vcdmaker.org for details. vcdMaker is supposed to help engineers to debug their applications and systems. It transforms text log files into the VCD format which can be easily displayed as a waveform.

Dependencies:   mbed vcdLogger vcdSignal

Committer:
ketjow
Date:
Fri Feb 12 21:38:04 2016 +0000
Revision:
0:9a59cffaafad
vcdMaker demo

Who changed what in which revision?

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