Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system
Dependencies: FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem
Fork of AxedaGo-Freescal_FRDM-KL46Z by
MAG3110/MAG3110.cpp@2:2f9019c5a9fc, 2014-07-02 (annotated)
- Committer:
- AxedaCorp
- Date:
- Wed Jul 02 19:57:37 2014 +0000
- Revision:
- 2:2f9019c5a9fc
- Parent:
- 0:65004368569c
ip switch
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AxedaCorp | 0:65004368569c | 1 | |
AxedaCorp | 0:65004368569c | 2 | #include "MAG3110.h" |
AxedaCorp | 0:65004368569c | 3 | #include "mbed.h" |
AxedaCorp | 0:65004368569c | 4 | |
AxedaCorp | 0:65004368569c | 5 | /****************************************************************************** |
AxedaCorp | 0:65004368569c | 6 | * Constructors |
AxedaCorp | 0:65004368569c | 7 | ******************************************************************************/ |
AxedaCorp | 0:65004368569c | 8 | MAG3110::MAG3110(PinName sda, PinName scl): _i2c(sda, scl), |
AxedaCorp | 0:65004368569c | 9 | _i2c_address(0x1D), _pc(NULL), _debug(false) |
AxedaCorp | 0:65004368569c | 10 | { |
AxedaCorp | 0:65004368569c | 11 | begin(); |
AxedaCorp | 0:65004368569c | 12 | } |
AxedaCorp | 0:65004368569c | 13 | |
AxedaCorp | 0:65004368569c | 14 | MAG3110::MAG3110(PinName sda, PinName scl, Serial *pc): _i2c(sda, scl), |
AxedaCorp | 0:65004368569c | 15 | _i2c_address(0x1D), _pc(pc), _debug(true) |
AxedaCorp | 0:65004368569c | 16 | { |
AxedaCorp | 0:65004368569c | 17 | begin(); |
AxedaCorp | 0:65004368569c | 18 | } |
AxedaCorp | 0:65004368569c | 19 | |
AxedaCorp | 0:65004368569c | 20 | void MAG3110::begin() |
AxedaCorp | 0:65004368569c | 21 | { |
AxedaCorp | 0:65004368569c | 22 | char cmd[2]; |
AxedaCorp | 0:65004368569c | 23 | |
AxedaCorp | 0:65004368569c | 24 | cmd[0] = MAG_CTRL_REG2; |
AxedaCorp | 0:65004368569c | 25 | cmd[1] = 0x80; |
AxedaCorp | 0:65004368569c | 26 | _i2c.write(_i2c_address, cmd, 2); |
AxedaCorp | 0:65004368569c | 27 | |
AxedaCorp | 0:65004368569c | 28 | cmd[0] = MAG_CTRL_REG1; |
AxedaCorp | 0:65004368569c | 29 | cmd[1] = MAG_3110_SAMPLE80+MAG_3110_OVERSAMPLE2+MAG_3110_ACTIVE; |
AxedaCorp | 0:65004368569c | 30 | _i2c.write(_i2c_address, cmd, 2); |
AxedaCorp | 0:65004368569c | 31 | |
AxedaCorp | 0:65004368569c | 32 | // No adjustment initially |
AxedaCorp | 0:65004368569c | 33 | _avgX = 0; |
AxedaCorp | 0:65004368569c | 34 | _avgY = 0; |
AxedaCorp | 0:65004368569c | 35 | } |
AxedaCorp | 0:65004368569c | 36 | |
AxedaCorp | 0:65004368569c | 37 | // Read a single byte form 8 bit register, return as int |
AxedaCorp | 0:65004368569c | 38 | int MAG3110::readReg(char regAddr) |
AxedaCorp | 0:65004368569c | 39 | { |
AxedaCorp | 0:65004368569c | 40 | char cmd[1]; |
AxedaCorp | 0:65004368569c | 41 | |
AxedaCorp | 0:65004368569c | 42 | cmd[0] = regAddr; |
AxedaCorp | 0:65004368569c | 43 | _i2c.write(_i2c_address, cmd, 1); |
AxedaCorp | 0:65004368569c | 44 | |
AxedaCorp | 0:65004368569c | 45 | cmd[0] = 0x00; |
AxedaCorp | 0:65004368569c | 46 | _i2c.read(_i2c_address, cmd, 1); |
AxedaCorp | 0:65004368569c | 47 | return (int)( cmd[0]); |
AxedaCorp | 0:65004368569c | 48 | } |
AxedaCorp | 0:65004368569c | 49 | |
AxedaCorp | 0:65004368569c | 50 | |
AxedaCorp | 0:65004368569c | 51 | // read a register per, pass first reg value, reading 2 bytes increments register |
AxedaCorp | 0:65004368569c | 52 | // Reads MSB first then LSB |
AxedaCorp | 0:65004368569c | 53 | int MAG3110::readVal(char regAddr) |
AxedaCorp | 0:65004368569c | 54 | { |
AxedaCorp | 0:65004368569c | 55 | char cmd[2]; |
AxedaCorp | 0:65004368569c | 56 | |
AxedaCorp | 0:65004368569c | 57 | cmd[0] = regAddr; |
AxedaCorp | 0:65004368569c | 58 | _i2c.write(_i2c_address, cmd, 1); |
AxedaCorp | 0:65004368569c | 59 | |
AxedaCorp | 0:65004368569c | 60 | cmd[0] = 0x00; |
AxedaCorp | 0:65004368569c | 61 | cmd[1] = 0x00; |
AxedaCorp | 0:65004368569c | 62 | _i2c.read(_i2c_address, cmd, 2); |
AxedaCorp | 0:65004368569c | 63 | return (int)( (cmd[1]|(cmd[0] << 8))); //concatenate the MSB and LSB |
AxedaCorp | 0:65004368569c | 64 | } |
AxedaCorp | 0:65004368569c | 65 | |
AxedaCorp | 0:65004368569c | 66 | |
AxedaCorp | 0:65004368569c | 67 | float MAG3110::getHeading() |
AxedaCorp | 0:65004368569c | 68 | { |
AxedaCorp | 0:65004368569c | 69 | int xVal = readVal(MAG_OUT_X_MSB); |
AxedaCorp | 0:65004368569c | 70 | int yVal = readVal(MAG_OUT_Y_MSB); |
AxedaCorp | 0:65004368569c | 71 | return (atan2((double)(yVal - _avgY),(double)(xVal - _avgX)))*180/PI; |
AxedaCorp | 0:65004368569c | 72 | } |
AxedaCorp | 0:65004368569c | 73 | |
AxedaCorp | 0:65004368569c | 74 | void MAG3110::getValues(int *xVal, int *yVal, int *zVal) |
AxedaCorp | 0:65004368569c | 75 | { |
AxedaCorp | 0:65004368569c | 76 | *xVal = readVal(MAG_OUT_X_MSB); |
AxedaCorp | 0:65004368569c | 77 | *yVal = readVal(MAG_OUT_Y_MSB); |
AxedaCorp | 0:65004368569c | 78 | *zVal = readVal(MAG_OUT_Z_MSB); |
AxedaCorp | 0:65004368569c | 79 | } |
AxedaCorp | 0:65004368569c | 80 | |
AxedaCorp | 0:65004368569c | 81 | |
AxedaCorp | 0:65004368569c | 82 | void MAG3110::setCalibration(int minX, int maxX, int minY, int maxY ) |
AxedaCorp | 0:65004368569c | 83 | { |
AxedaCorp | 0:65004368569c | 84 | _avgX=(maxX+minX)/2; |
AxedaCorp | 0:65004368569c | 85 | _avgY=(maxY+minY)/2; |
AxedaCorp | 0:65004368569c | 86 | } |
AxedaCorp | 0:65004368569c | 87 | |
AxedaCorp | 0:65004368569c | 88 | |
AxedaCorp | 0:65004368569c | 89 | |
AxedaCorp | 0:65004368569c | 90 | void MAG3110::calXY(PinName pin, int activeValue ) |
AxedaCorp | 0:65004368569c | 91 | { |
AxedaCorp | 0:65004368569c | 92 | DigitalIn calPin(pin); |
AxedaCorp | 0:65004368569c | 93 | int tempXmax, tempXmin, tempYmax, tempYmin, newX, newY; |
AxedaCorp | 0:65004368569c | 94 | |
AxedaCorp | 0:65004368569c | 95 | |
AxedaCorp | 0:65004368569c | 96 | // Wait for Button Press and Release before beginning calibration |
AxedaCorp | 0:65004368569c | 97 | while(calPin != activeValue) {} |
AxedaCorp | 0:65004368569c | 98 | while(calPin == activeValue) {} |
AxedaCorp | 0:65004368569c | 99 | |
AxedaCorp | 0:65004368569c | 100 | |
AxedaCorp | 0:65004368569c | 101 | // Read initial values of magnetomoter - read it here to create a slight delay for calPin to settle |
AxedaCorp | 0:65004368569c | 102 | tempXmax = tempXmin = readVal(MAG_OUT_X_MSB); |
AxedaCorp | 0:65004368569c | 103 | tempYmax = tempYmin = readVal(MAG_OUT_Y_MSB); |
AxedaCorp | 0:65004368569c | 104 | |
AxedaCorp | 0:65004368569c | 105 | // Update min and max values until calPin asserted again |
AxedaCorp | 0:65004368569c | 106 | while(calPin != activeValue) { |
AxedaCorp | 0:65004368569c | 107 | newX = readVal(MAG_OUT_X_MSB); |
AxedaCorp | 0:65004368569c | 108 | newY = readVal(MAG_OUT_Y_MSB); |
AxedaCorp | 0:65004368569c | 109 | if (newX > tempXmax) tempXmax = newX; |
AxedaCorp | 0:65004368569c | 110 | if (newX < tempXmin) tempXmin = newX; |
AxedaCorp | 0:65004368569c | 111 | if (newY > tempYmax) tempYmax = newY; |
AxedaCorp | 0:65004368569c | 112 | if (newY < tempYmin) tempYmin = newY; |
AxedaCorp | 0:65004368569c | 113 | } |
AxedaCorp | 0:65004368569c | 114 | |
AxedaCorp | 0:65004368569c | 115 | setCalibration( tempXmin, tempXmax, tempYmin, tempYmax ); |
AxedaCorp | 0:65004368569c | 116 | |
AxedaCorp | 0:65004368569c | 117 | } |
AxedaCorp | 0:65004368569c | 118 |