![](/media/cache/group/IMG_1289.JPG.50x50_q85.jpg)
Test program for MAG3110
Dependencies: MAG3110 mbed vt100
main.cpp@0:43029bebd87f, 2015-12-25 (annotated)
- Committer:
- Rhyme
- Date:
- Fri Dec 25 07:51:26 2015 +0000
- Revision:
- 0:43029bebd87f
commit before publishing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:43029bebd87f | 1 | #include "mbed.h" |
Rhyme | 0:43029bebd87f | 2 | #include "vt100.h" |
Rhyme | 0:43029bebd87f | 3 | #include "MAG3110.h" |
Rhyme | 0:43029bebd87f | 4 | #include "MSS.h" |
Rhyme | 0:43029bebd87f | 5 | |
Rhyme | 0:43029bebd87f | 6 | #define MAG3110_I2C_ADDRESS (0x0E) |
Rhyme | 0:43029bebd87f | 7 | |
Rhyme | 0:43029bebd87f | 8 | MAG3110 *mag3110 = 0 ; |
Rhyme | 0:43029bebd87f | 9 | |
Rhyme | 0:43029bebd87f | 10 | vt100 *tty = 0 ; |
Rhyme | 0:43029bebd87f | 11 | |
Rhyme | 0:43029bebd87f | 12 | void reportMAG3110(MAG3110 *mag, int x, int y) |
Rhyme | 0:43029bebd87f | 13 | { |
Rhyme | 0:43029bebd87f | 14 | uint8_t status = 0 ; |
Rhyme | 0:43029bebd87f | 15 | int16_t mX, mY, mZ ; |
Rhyme | 0:43029bebd87f | 16 | float fx, fy, fz ; |
Rhyme | 0:43029bebd87f | 17 | mag->activate() ; |
Rhyme | 0:43029bebd87f | 18 | wait(0.01) ; |
Rhyme | 0:43029bebd87f | 19 | status = mag->getStatus() ; |
Rhyme | 0:43029bebd87f | 20 | while((status & 0x80) == 0) { // wait for ZYXOW |
Rhyme | 0:43029bebd87f | 21 | wait(0.01) ; |
Rhyme | 0:43029bebd87f | 22 | status = mag->getStatus() ; |
Rhyme | 0:43029bebd87f | 23 | } |
Rhyme | 0:43029bebd87f | 24 | |
Rhyme | 0:43029bebd87f | 25 | tty->locate(x+5, y) ; |
Rhyme | 0:43029bebd87f | 26 | printf("--- MAG3110 ---") ; |
Rhyme | 0:43029bebd87f | 27 | tty->locate(x, y+4) ; |
Rhyme | 0:43029bebd87f | 28 | printf("Temperature: %02X",mag->getTemp()) ; |
Rhyme | 0:43029bebd87f | 29 | tty->locate(x, y+5) ; |
Rhyme | 0:43029bebd87f | 30 | printf("status: 0x%02X",mag->getStatus()) ; |
Rhyme | 0:43029bebd87f | 31 | tty->locate(x, y+6) ; |
Rhyme | 0:43029bebd87f | 32 | printf("ctrl reg1: 0x%02X ", mag->getCtrlReg1()) ; |
Rhyme | 0:43029bebd87f | 33 | printf("reg2: 0x%02X", mag->getCtrlReg2()) ; |
Rhyme | 0:43029bebd87f | 34 | mX = mag->getX() ; // here reset takes place! |
Rhyme | 0:43029bebd87f | 35 | mY = mag->getY() ; |
Rhyme | 0:43029bebd87f | 36 | mZ = mag->getZ() ; |
Rhyme | 0:43029bebd87f | 37 | fx = 10000 * (float)mX / (float)0x7FFF ; |
Rhyme | 0:43029bebd87f | 38 | fy = 10000 * (float)mY / (float)0x7FFF ; |
Rhyme | 0:43029bebd87f | 39 | fz = 10000 * (float)mZ / (float)0x7FFF ; |
Rhyme | 0:43029bebd87f | 40 | tty->locate(x, y+1) ; |
Rhyme | 0:43029bebd87f | 41 | printf("X : ") ; |
Rhyme | 0:43029bebd87f | 42 | if (fx >= 0) { printf(" ") ; } |
Rhyme | 0:43029bebd87f | 43 | printf("%.2f uT : 0x%08X ", fx, mX) ; |
Rhyme | 0:43029bebd87f | 44 | |
Rhyme | 0:43029bebd87f | 45 | tty->locate(x, y+2) ; |
Rhyme | 0:43029bebd87f | 46 | printf("Y : ") ; |
Rhyme | 0:43029bebd87f | 47 | if (fy >= 0) { printf(" ") ; } |
Rhyme | 0:43029bebd87f | 48 | printf("%.2f uT : 0x%08X ", fy, mY) ; |
Rhyme | 0:43029bebd87f | 49 | |
Rhyme | 0:43029bebd87f | 50 | tty->locate(x, y+3) ; |
Rhyme | 0:43029bebd87f | 51 | printf("Z : ") ; |
Rhyme | 0:43029bebd87f | 52 | if (fz >= 0) { printf(" ") ; } |
Rhyme | 0:43029bebd87f | 53 | printf("%.2f uT : 0x%08X ", fz, mZ) ; |
Rhyme | 0:43029bebd87f | 54 | mag->standby() ; |
Rhyme | 0:43029bebd87f | 55 | } |
Rhyme | 0:43029bebd87f | 56 | |
Rhyme | 0:43029bebd87f | 57 | int main() { |
Rhyme | 0:43029bebd87f | 58 | int x = 4 ; |
Rhyme | 0:43029bebd87f | 59 | int y = 2 ; |
Rhyme | 0:43029bebd87f | 60 | uint8_t id ; |
Rhyme | 0:43029bebd87f | 61 | |
Rhyme | 0:43029bebd87f | 62 | tty = new vt100() ; |
Rhyme | 0:43029bebd87f | 63 | tty->cls() ; |
Rhyme | 0:43029bebd87f | 64 | |
Rhyme | 0:43029bebd87f | 65 | mag3110 = new MAG3110(PIN_SDA, PIN_SCL, MAG3110_I2C_ADDRESS) ; |
Rhyme | 0:43029bebd87f | 66 | // id = mag3110->getID() ; |
Rhyme | 0:43029bebd87f | 67 | // printf("ID = 0x%02X\n\r", id) ; |
Rhyme | 0:43029bebd87f | 68 | while(1) { |
Rhyme | 0:43029bebd87f | 69 | reportMAG3110(mag3110, x, y) ; |
Rhyme | 0:43029bebd87f | 70 | wait(0.2) ; |
Rhyme | 0:43029bebd87f | 71 | } |
Rhyme | 0:43029bebd87f | 72 | } |