A class and a demo program to use with the DC-SS504 board from SureElectronics which uses MMC2120MG magnetometer from Memsic. The program glows leds depending on the direction it is turned to.
MMCx12xM.h@0:a44429321af8, 2009-12-02 (annotated)
- Committer:
- igorsk
- Date:
- Wed Dec 02 23:03:25 2009 +0000
- Revision:
- 0:a44429321af8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
igorsk | 0:a44429321af8 | 1 | #include <mbed.h> |
igorsk | 0:a44429321af8 | 2 | #ifndef __MMCx12xM__ |
igorsk | 0:a44429321af8 | 3 | #define __MMCx12xM__ |
igorsk | 0:a44429321af8 | 4 | |
igorsk | 0:a44429321af8 | 5 | // possible I2C addresses, depending on the chip number |
igorsk | 0:a44429321af8 | 6 | enum |
igorsk | 0:a44429321af8 | 7 | { |
igorsk | 0:a44429321af8 | 8 | MMCx120M = 0x60, |
igorsk | 0:a44429321af8 | 9 | MMCx121M = 0x64, |
igorsk | 0:a44429321af8 | 10 | MMCx122M = 0x68, |
igorsk | 0:a44429321af8 | 11 | MMCx123M = 0x6C, |
igorsk | 0:a44429321af8 | 12 | }; |
igorsk | 0:a44429321af8 | 13 | |
igorsk | 0:a44429321af8 | 14 | /* Class: MMCx12xM |
igorsk | 0:a44429321af8 | 15 | * Control a Memsic MMC212xM magnetometer over I2C |
igorsk | 0:a44429321af8 | 16 | * |
igorsk | 0:a44429321af8 | 17 | * Example: |
igorsk | 0:a44429321af8 | 18 | * > // MMC2120M at address 0x60 |
igorsk | 0:a44429321af8 | 19 | * > |
igorsk | 0:a44429321af8 | 20 | * > #include "mbed.h" |
igorsk | 0:a44429321af8 | 21 | * > |
igorsk | 0:a44429321af8 | 22 | * > I2C i2c(p28, p27); |
igorsk | 0:a44429321af8 | 23 | * > MMC212xM memsic1(i2c); |
igorsk | 0:a44429321af8 | 24 | * > |
igorsk | 0:a44429321af8 | 25 | * > int main() { |
igorsk | 0:a44429321af8 | 26 | * > int data[2]; |
igorsk | 0:a44429321af8 | 27 | * > memsic1.read_raw_values(data, 2); |
igorsk | 0:a44429321af8 | 28 | * > } |
igorsk | 0:a44429321af8 | 29 | */ |
igorsk | 0:a44429321af8 | 30 | |
igorsk | 0:a44429321af8 | 31 | class MMCx12xM : public Base |
igorsk | 0:a44429321af8 | 32 | { |
igorsk | 0:a44429321af8 | 33 | public: |
igorsk | 0:a44429321af8 | 34 | // constructor in case you already have an I2C bus instance |
igorsk | 0:a44429321af8 | 35 | MMCx12xM(I2C &i2c, int address = MMCx120M, const char *name = NULL); |
igorsk | 0:a44429321af8 | 36 | // use this constructor if the sensor is the only device on the bus |
igorsk | 0:a44429321af8 | 37 | MMCx12xM(PinName sda, PinName scl, int address = MMCx120M, const char *name = NULL); |
igorsk | 0:a44429321af8 | 38 | // send a SET coil command |
igorsk | 0:a44429321af8 | 39 | bool coil_set(); |
igorsk | 0:a44429321af8 | 40 | // send a RESET coil command |
igorsk | 0:a44429321af8 | 41 | bool coil_reset(); |
igorsk | 0:a44429321af8 | 42 | // read raw (12-bit) axis values |
igorsk | 0:a44429321af8 | 43 | bool read_raw_values(int *values, int count = 2); |
igorsk | 0:a44429321af8 | 44 | // start calibration |
igorsk | 0:a44429321af8 | 45 | void calibrate_begin(); |
igorsk | 0:a44429321af8 | 46 | // take a single measurement for calibration |
igorsk | 0:a44429321af8 | 47 | void calibrate_step(int count = 2); |
igorsk | 0:a44429321af8 | 48 | // finish calibration and calculate offset and sensitivity values |
igorsk | 0:a44429321af8 | 49 | void calibrate_end(); |
igorsk | 0:a44429321af8 | 50 | // read calibrated (-1.0 .. +1.0) axis values |
igorsk | 0:a44429321af8 | 51 | bool read_values(float *values, int count = 2); |
igorsk | 0:a44429321af8 | 52 | virtual ~MMCx12xM(); |
igorsk | 0:a44429321af8 | 53 | |
igorsk | 0:a44429321af8 | 54 | private: |
igorsk | 0:a44429321af8 | 55 | bool _send_command(int command); |
igorsk | 0:a44429321af8 | 56 | bool _wait_ready(int command); |
igorsk | 0:a44429321af8 | 57 | bool _read_axis(int *value, int index = -1); |
igorsk | 0:a44429321af8 | 58 | |
igorsk | 0:a44429321af8 | 59 | // reference to the I2C bus |
igorsk | 0:a44429321af8 | 60 | I2C *_I2C; |
igorsk | 0:a44429321af8 | 61 | // sensor slave address |
igorsk | 0:a44429321af8 | 62 | int _addr; |
igorsk | 0:a44429321af8 | 63 | // did we create the bus instance? (i.e. we should delete it on destruct) |
igorsk | 0:a44429321af8 | 64 | bool _own_i2c; |
igorsk | 0:a44429321af8 | 65 | // calibration values |
igorsk | 0:a44429321af8 | 66 | int _sensitivity[3]; |
igorsk | 0:a44429321af8 | 67 | int _offset[3]; |
igorsk | 0:a44429321af8 | 68 | // temporaries for calibration |
igorsk | 0:a44429321af8 | 69 | int _maxvals[3]; |
igorsk | 0:a44429321af8 | 70 | int _minvals[3]; |
igorsk | 0:a44429321af8 | 71 | }; |
igorsk | 0:a44429321af8 | 72 | |
igorsk | 0:a44429321af8 | 73 | #endif |