totally useless
Dependents: Final_Homework1 ee202Homework1 EE202A_Homework1
Fork of MAG3110 by
MAG3110.cpp@5:34cf17e4dbf5, 2014-02-08 (annotated)
- Committer:
- bmdlh
- Date:
- Sat Feb 08 22:18:07 2014 +0000
- Revision:
- 5:34cf17e4dbf5
- Parent:
- 1:5a0e7a58d980
useless
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 0:63a8594a3866 | 1 | |
SomeRandomBloke | 0:63a8594a3866 | 2 | #include "MAG3110.h" |
SomeRandomBloke | 0:63a8594a3866 | 3 | #include "mbed.h" |
SomeRandomBloke | 0:63a8594a3866 | 4 | |
SomeRandomBloke | 0:63a8594a3866 | 5 | /****************************************************************************** |
SomeRandomBloke | 0:63a8594a3866 | 6 | * Constructors |
SomeRandomBloke | 0:63a8594a3866 | 7 | ******************************************************************************/ |
bmdlh | 5:34cf17e4dbf5 | 8 | MAG3110::MAG3110(PinName sda, PinName scl,float dateRate, int overSample): _i2c(sda, scl), |
bmdlh | 5:34cf17e4dbf5 | 9 | _i2c_address(0x0E<<1), _pc(NULL), _debug(false) |
bmdlh | 5:34cf17e4dbf5 | 10 | { |
bmdlh | 5:34cf17e4dbf5 | 11 | Setdr(dateRate); |
bmdlh | 5:34cf17e4dbf5 | 12 | Setosr(overSample); |
bmdlh | 5:34cf17e4dbf5 | 13 | begin(); |
bmdlh | 5:34cf17e4dbf5 | 14 | } |
bmdlh | 5:34cf17e4dbf5 | 15 | |
SomeRandomBloke | 1:5a0e7a58d980 | 16 | MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl), |
bmdlh | 5:34cf17e4dbf5 | 17 | _i2c_address(0x0E<<1), _pc(NULL), _debug(false),dr(MAG_3110_SAMPLE80),osr(MAG_3110_OVERSAMPLE2) |
SomeRandomBloke | 0:63a8594a3866 | 18 | { |
SomeRandomBloke | 1:5a0e7a58d980 | 19 | begin(); |
SomeRandomBloke | 1:5a0e7a58d980 | 20 | } |
SomeRandomBloke | 0:63a8594a3866 | 21 | |
SomeRandomBloke | 1:5a0e7a58d980 | 22 | MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl), |
bmdlh | 5:34cf17e4dbf5 | 23 | _i2c_address(0x0E<<1), _pc(pc), _debug(true),dr(MAG_3110_SAMPLE80),osr(MAG_3110_OVERSAMPLE2) |
SomeRandomBloke | 1:5a0e7a58d980 | 24 | { |
SomeRandomBloke | 1:5a0e7a58d980 | 25 | begin(); |
SomeRandomBloke | 0:63a8594a3866 | 26 | } |
SomeRandomBloke | 0:63a8594a3866 | 27 | |
SomeRandomBloke | 1:5a0e7a58d980 | 28 | void MAG3110::begin() |
SomeRandomBloke | 0:63a8594a3866 | 29 | { |
bmdlh | 5:34cf17e4dbf5 | 30 | |
SomeRandomBloke | 1:5a0e7a58d980 | 31 | char cmd[2]; |
SomeRandomBloke | 0:63a8594a3866 | 32 | |
SomeRandomBloke | 1:5a0e7a58d980 | 33 | cmd[0] = MAG_CTRL_REG2; |
SomeRandomBloke | 1:5a0e7a58d980 | 34 | cmd[1] = 0x80; |
SomeRandomBloke | 1:5a0e7a58d980 | 35 | _i2c.write(_i2c_address, cmd, 2); |
SomeRandomBloke | 1:5a0e7a58d980 | 36 | |
SomeRandomBloke | 1:5a0e7a58d980 | 37 | cmd[0] = MAG_CTRL_REG1; |
bmdlh | 5:34cf17e4dbf5 | 38 | // cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE; |
bmdlh | 5:34cf17e4dbf5 | 39 | cmd[1] = dr+osr+MAG_3110_ACTIVE; |
SomeRandomBloke | 1:5a0e7a58d980 | 40 | _i2c.write(_i2c_address, cmd, 2); |
SomeRandomBloke | 1:5a0e7a58d980 | 41 | |
SomeRandomBloke | 1:5a0e7a58d980 | 42 | // No adjustment initially |
SomeRandomBloke | 1:5a0e7a58d980 | 43 | _avgX = 0; |
SomeRandomBloke | 1:5a0e7a58d980 | 44 | _avgY = 0; |
SomeRandomBloke | 0:63a8594a3866 | 45 | } |
SomeRandomBloke | 0:63a8594a3866 | 46 | |
SomeRandomBloke | 0:63a8594a3866 | 47 | // Read a single byte form 8 bit register, return as int |
SomeRandomBloke | 0:63a8594a3866 | 48 | int MAG3110::readReg(char regAddr) |
SomeRandomBloke | 0:63a8594a3866 | 49 | { |
SomeRandomBloke | 0:63a8594a3866 | 50 | char cmd[1]; |
SomeRandomBloke | 0:63a8594a3866 | 51 | |
SomeRandomBloke | 0:63a8594a3866 | 52 | cmd[0] = regAddr; |
SomeRandomBloke | 1:5a0e7a58d980 | 53 | _i2c.write(_i2c_address, cmd, 1); |
SomeRandomBloke | 0:63a8594a3866 | 54 | |
SomeRandomBloke | 0:63a8594a3866 | 55 | cmd[0] = 0x00; |
SomeRandomBloke | 1:5a0e7a58d980 | 56 | _i2c.read(_i2c_address, cmd, 1); |
SomeRandomBloke | 0:63a8594a3866 | 57 | return (int)( cmd[0]); |
SomeRandomBloke | 0:63a8594a3866 | 58 | } |
SomeRandomBloke | 0:63a8594a3866 | 59 | |
SomeRandomBloke | 0:63a8594a3866 | 60 | |
SomeRandomBloke | 0:63a8594a3866 | 61 | // read a register per, pass first reg value, reading 2 bytes increments register |
SomeRandomBloke | 0:63a8594a3866 | 62 | // Reads MSB first then LSB |
SomeRandomBloke | 0:63a8594a3866 | 63 | int MAG3110::readVal(char regAddr) |
SomeRandomBloke | 0:63a8594a3866 | 64 | { |
SomeRandomBloke | 0:63a8594a3866 | 65 | char cmd[2]; |
SomeRandomBloke | 0:63a8594a3866 | 66 | |
SomeRandomBloke | 0:63a8594a3866 | 67 | cmd[0] = regAddr; |
SomeRandomBloke | 1:5a0e7a58d980 | 68 | _i2c.write(_i2c_address, cmd, 1); |
SomeRandomBloke | 0:63a8594a3866 | 69 | |
SomeRandomBloke | 0:63a8594a3866 | 70 | cmd[0] = 0x00; |
SomeRandomBloke | 0:63a8594a3866 | 71 | cmd[1] = 0x00; |
SomeRandomBloke | 1:5a0e7a58d980 | 72 | _i2c.read(_i2c_address, cmd, 2); |
SomeRandomBloke | 0:63a8594a3866 | 73 | return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB |
SomeRandomBloke | 0:63a8594a3866 | 74 | } |
SomeRandomBloke | 0:63a8594a3866 | 75 | |
SomeRandomBloke | 0:63a8594a3866 | 76 | |
SomeRandomBloke | 0:63a8594a3866 | 77 | float MAG3110::getHeading() |
SomeRandomBloke | 0:63a8594a3866 | 78 | { |
SomeRandomBloke | 0:63a8594a3866 | 79 | int xVal = readVal(MAG_OUT_X_MSB); |
SomeRandomBloke | 0:63a8594a3866 | 80 | int yVal = readVal(MAG_OUT_Y_MSB); |
SomeRandomBloke | 0:63a8594a3866 | 81 | return (atan2((double)(yVal - _avgY),(double)(xVal - _avgX)))*180/PI; |
SomeRandomBloke | 0:63a8594a3866 | 82 | } |
SomeRandomBloke | 0:63a8594a3866 | 83 | |
SomeRandomBloke | 0:63a8594a3866 | 84 | void MAG3110::getValues(int *xVal, int *yVal, int *zVal) |
SomeRandomBloke | 0:63a8594a3866 | 85 | { |
SomeRandomBloke | 0:63a8594a3866 | 86 | *xVal = readVal(MAG_OUT_X_MSB); |
SomeRandomBloke | 0:63a8594a3866 | 87 | *yVal = readVal(MAG_OUT_Y_MSB); |
SomeRandomBloke | 0:63a8594a3866 | 88 | *zVal = readVal(MAG_OUT_Z_MSB); |
SomeRandomBloke | 0:63a8594a3866 | 89 | } |
SomeRandomBloke | 0:63a8594a3866 | 90 | |
SomeRandomBloke | 0:63a8594a3866 | 91 | |
SomeRandomBloke | 0:63a8594a3866 | 92 | void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY ) |
SomeRandomBloke | 0:63a8594a3866 | 93 | { |
SomeRandomBloke | 0:63a8594a3866 | 94 | _avgX=(maxX+minX)/2; |
SomeRandomBloke | 0:63a8594a3866 | 95 | _avgY=(maxY+minY)/2; |
SomeRandomBloke | 0:63a8594a3866 | 96 | } |
SomeRandomBloke | 0:63a8594a3866 | 97 | |
bmdlh | 5:34cf17e4dbf5 | 98 | void MAG3110::Setdr(float dateRate) |
bmdlh | 5:34cf17e4dbf5 | 99 | { |
bmdlh | 5:34cf17e4dbf5 | 100 | if (dateRate==80) |
bmdlh | 5:34cf17e4dbf5 | 101 | dr=MAG_3110_SAMPLE80; |
bmdlh | 5:34cf17e4dbf5 | 102 | else if(dateRate==40) |
bmdlh | 5:34cf17e4dbf5 | 103 | dr=MAG_3110_SAMPLE40; |
bmdlh | 5:34cf17e4dbf5 | 104 | else if(dateRate==20) |
bmdlh | 5:34cf17e4dbf5 | 105 | dr=MAG_3110_SAMPLE20; |
bmdlh | 5:34cf17e4dbf5 | 106 | else if(dateRate==10) |
bmdlh | 5:34cf17e4dbf5 | 107 | dr=MAG_3110_SAMPLE10; |
bmdlh | 5:34cf17e4dbf5 | 108 | else if(dateRate==5) |
bmdlh | 5:34cf17e4dbf5 | 109 | dr=MAG_3110_SAMPLE5; |
bmdlh | 5:34cf17e4dbf5 | 110 | else if(dateRate==2.5) |
bmdlh | 5:34cf17e4dbf5 | 111 | dr=MAG_3110_SAMPLE2_5; |
bmdlh | 5:34cf17e4dbf5 | 112 | else if(dateRate==1.25) |
bmdlh | 5:34cf17e4dbf5 | 113 | dr=MAG_3110_SAMPLE1_25; |
bmdlh | 5:34cf17e4dbf5 | 114 | else if(dateRate==0.625) |
bmdlh | 5:34cf17e4dbf5 | 115 | dr=MAG_3110_SAMPLE0_625; |
bmdlh | 5:34cf17e4dbf5 | 116 | else |
bmdlh | 5:34cf17e4dbf5 | 117 | dr=MAG_3110_SAMPLE80; |
bmdlh | 5:34cf17e4dbf5 | 118 | } |
SomeRandomBloke | 0:63a8594a3866 | 119 | |
bmdlh | 5:34cf17e4dbf5 | 120 | void MAG3110::Setosr(int overSample) |
bmdlh | 5:34cf17e4dbf5 | 121 | { |
bmdlh | 5:34cf17e4dbf5 | 122 | switch(overSample) |
bmdlh | 5:34cf17e4dbf5 | 123 | { |
bmdlh | 5:34cf17e4dbf5 | 124 | case 16:osr=MAG_3110_OVERSAMPLE1;break; |
bmdlh | 5:34cf17e4dbf5 | 125 | case 32:osr=MAG_3110_OVERSAMPLE2;break; |
bmdlh | 5:34cf17e4dbf5 | 126 | case 64:osr=MAG_3110_OVERSAMPLE3;break; |
bmdlh | 5:34cf17e4dbf5 | 127 | case 128:osr=MAG_3110_OVERSAMPLE4;break; |
bmdlh | 5:34cf17e4dbf5 | 128 | default:osr=MAG_3110_OVERSAMPLE2;break; |
bmdlh | 5:34cf17e4dbf5 | 129 | } |
bmdlh | 5:34cf17e4dbf5 | 130 | } |
SomeRandomBloke | 0:63a8594a3866 | 131 | |
bmdlh | 5:34cf17e4dbf5 | 132 | void MAG3110::Overwrite_dr_osr(float dateRate,int overSample) |
bmdlh | 5:34cf17e4dbf5 | 133 | { |
bmdlh | 5:34cf17e4dbf5 | 134 | char cmd[2]; |
bmdlh | 5:34cf17e4dbf5 | 135 | Setdr(dateRate); |
bmdlh | 5:34cf17e4dbf5 | 136 | Setosr(overSample); |
bmdlh | 5:34cf17e4dbf5 | 137 | cmd[0] = MAG_CTRL_REG1; |
bmdlh | 5:34cf17e4dbf5 | 138 | cmd[1] = dr+osr+MAG_3110_ACTIVE; |
bmdlh | 5:34cf17e4dbf5 | 139 | _i2c.write(_i2c_address, cmd, 2); |
bmdlh | 5:34cf17e4dbf5 | 140 | } |