Playing around with accelerometer and magnetometer on mbed KL46Z

Dependencies:   MAG3110 MMA8451Q PinDetect mbed TSI

Committer:
oliverfang
Date:
Sun Feb 02 22:07:05 2014 +0000
Revision:
0:648dde0c4ef8
Child:
2:bb31f097af0f
Accelerometer and magnetometer data collection with different sampling rates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oliverfang 0:648dde0c4ef8 1 #include "mbed.h"
oliverfang 0:648dde0c4ef8 2 #include "PinDetect.h"
oliverfang 0:648dde0c4ef8 3 #include "MMA8451Q.h"
oliverfang 0:648dde0c4ef8 4 #include "MAG3110.h"
oliverfang 0:648dde0c4ef8 5
oliverfang 0:648dde0c4ef8 6 #define MMA8451_I2C_ADDRESS (0x1d<<1)
oliverfang 0:648dde0c4ef8 7
oliverfang 0:648dde0c4ef8 8 // Declare output LEDs
oliverfang 0:648dde0c4ef8 9 DigitalOut ledgreen(PTD5);
oliverfang 0:648dde0c4ef8 10 DigitalOut ledred(PTE29);
oliverfang 0:648dde0c4ef8 11
oliverfang 0:648dde0c4ef8 12 // Declare USB serial connection
oliverfang 0:648dde0c4ef8 13 Serial pc(USBTX,USBRX);
oliverfang 0:648dde0c4ef8 14
oliverfang 0:648dde0c4ef8 15 // Declare timer interrupt
oliverfang 0:648dde0c4ef8 16 Ticker timerAcc;
oliverfang 0:648dde0c4ef8 17 Ticker timerMag;
oliverfang 0:648dde0c4ef8 18
oliverfang 0:648dde0c4ef8 19 // Declare pointer variables
oliverfang 0:648dde0c4ef8 20 float xAcc;
oliverfang 0:648dde0c4ef8 21 float yAcc;
oliverfang 0:648dde0c4ef8 22 float zAcc;
oliverfang 0:648dde0c4ef8 23 int xMag;
oliverfang 0:648dde0c4ef8 24 int yMag;
oliverfang 0:648dde0c4ef8 25 int zMag;
oliverfang 0:648dde0c4ef8 26
oliverfang 0:648dde0c4ef8 27 // Declare Accelerometer pins and I2C address
oliverfang 0:648dde0c4ef8 28 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS, 0, 0);
oliverfang 0:648dde0c4ef8 29 // Declare Magnetometer pins
oliverfang 0:648dde0c4ef8 30 MAG3110 mag(PTE25, PTE24);
oliverfang 0:648dde0c4ef8 31
oliverfang 0:648dde0c4ef8 32 // Functions
oliverfang 0:648dde0c4ef8 33 void init();
oliverfang 0:648dde0c4ef8 34 void accTime();
oliverfang 0:648dde0c4ef8 35 void magTime();
oliverfang 0:648dde0c4ef8 36
oliverfang 0:648dde0c4ef8 37 void init()
oliverfang 0:648dde0c4ef8 38 {
oliverfang 0:648dde0c4ef8 39 // Attach timerAcc
oliverfang 0:648dde0c4ef8 40 timerAcc.attach(&accTime, 0.5);
oliverfang 0:648dde0c4ef8 41 timerMag.attach(&magTime, 0.75);
oliverfang 0:648dde0c4ef8 42 ledred = 0;
oliverfang 0:648dde0c4ef8 43 ledgreen = 0;
oliverfang 0:648dde0c4ef8 44 }
oliverfang 0:648dde0c4ef8 45
oliverfang 0:648dde0c4ef8 46 int main()
oliverfang 0:648dde0c4ef8 47 {
oliverfang 0:648dde0c4ef8 48 // Initialize
oliverfang 0:648dde0c4ef8 49 init();
oliverfang 0:648dde0c4ef8 50
oliverfang 0:648dde0c4ef8 51 while(1)
oliverfang 0:648dde0c4ef8 52 {
oliverfang 0:648dde0c4ef8 53 // Read and print data from accelerometer
oliverfang 0:648dde0c4ef8 54 pc.puts("Accelerometer Data:\r\n");
oliverfang 0:648dde0c4ef8 55 pc.printf("X: %f, Y: %f, Z: %f\r\n", xAcc, yAcc, zAcc);
oliverfang 0:648dde0c4ef8 56 // Read data from magnetometer
oliverfang 0:648dde0c4ef8 57 pc.puts("Magnetometer Data:\r\n");
oliverfang 0:648dde0c4ef8 58 pc.printf("X: %d, Y: %d, Z: %d\r\n", xMag, yMag, zMag);
oliverfang 0:648dde0c4ef8 59 wait(0.5);
oliverfang 0:648dde0c4ef8 60 }
oliverfang 0:648dde0c4ef8 61 }
oliverfang 0:648dde0c4ef8 62
oliverfang 0:648dde0c4ef8 63 void accTime()
oliverfang 0:648dde0c4ef8 64 {
oliverfang 0:648dde0c4ef8 65 xAcc = abs(acc.getAccX());
oliverfang 0:648dde0c4ef8 66 yAcc = abs(acc.getAccY());
oliverfang 0:648dde0c4ef8 67 zAcc = abs(acc.getAccZ());
oliverfang 0:648dde0c4ef8 68 ledgreen = !ledgreen;
oliverfang 0:648dde0c4ef8 69 }
oliverfang 0:648dde0c4ef8 70
oliverfang 0:648dde0c4ef8 71 void magTime()
oliverfang 0:648dde0c4ef8 72 {
oliverfang 0:648dde0c4ef8 73 xMag = mag.getXVal();
oliverfang 0:648dde0c4ef8 74 yMag = mag.getYVal();
oliverfang 0:648dde0c4ef8 75 zMag = mag.getZVal();
oliverfang 0:648dde0c4ef8 76 ledred = !ledred;
oliverfang 0:648dde0c4ef8 77 }