Sbart-SSD341-HW-15.1
Dependencies: MMA8451Q SLCD_minus_mod mbed
Fork of ACC_LCD_341_Basic by
acc_341.cpp@0:203b4129a213, 2014-09-25 (annotated)
- Committer:
- scohennm
- Date:
- Thu Sep 25 15:53:53 2014 +0000
- Revision:
- 0:203b4129a213
- Child:
- 1:9340a340e588
Midterm test program for publishing practice SSD 341 AY2014-2015 NMHU
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:203b4129a213 | 1 | #include "mbed.h" |
scohennm | 0:203b4129a213 | 2 | #include "MMA8451Q.h" |
scohennm | 0:203b4129a213 | 3 | #include "SLCD.h" |
scohennm | 0:203b4129a213 | 4 | |
scohennm | 0:203b4129a213 | 5 | /* |
scohennm | 0:203b4129a213 | 6 | Test of the accelerometer, digital I/O, on-board LCD screen. |
scohennm | 0:203b4129a213 | 7 | Looing at vector product of the x-y components of the accelerometer. |
scohennm | 0:203b4129a213 | 8 | Works pretty well. Still rough, program wise - sc 140710 |
scohennm | 0:203b4129a213 | 9 | */ |
scohennm | 0:203b4129a213 | 10 | |
scohennm | 0:203b4129a213 | 11 | #define BLINKTIME 0.700 |
scohennm | 0:203b4129a213 | 12 | #define DATATIME 0.200 |
scohennm | 0:203b4129a213 | 13 | |
scohennm | 0:203b4129a213 | 14 | #define PROGNAME "ACCLCD341\r/n" |
scohennm | 0:203b4129a213 | 15 | |
scohennm | 0:203b4129a213 | 16 | #define PRINTDBUG |
scohennm | 0:203b4129a213 | 17 | |
scohennm | 0:203b4129a213 | 18 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 0:203b4129a213 | 19 | PinName const SDA = PTE25; |
scohennm | 0:203b4129a213 | 20 | PinName const SCL = PTE24; |
scohennm | 0:203b4129a213 | 21 | #elif defined (TARGET_KL05Z) |
scohennm | 0:203b4129a213 | 22 | PinName const SDA = PTB4; |
scohennm | 0:203b4129a213 | 23 | PinName const SCL = PTB3; |
scohennm | 0:203b4129a213 | 24 | #else |
scohennm | 0:203b4129a213 | 25 | #error TARGET NOT DEFINED |
scohennm | 0:203b4129a213 | 26 | #endif |
scohennm | 0:203b4129a213 | 27 | |
scohennm | 0:203b4129a213 | 28 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 0:203b4129a213 | 29 | |
scohennm | 0:203b4129a213 | 30 | SLCD slcd; //define LCD display |
scohennm | 0:203b4129a213 | 31 | |
scohennm | 0:203b4129a213 | 32 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 0:203b4129a213 | 33 | Serial pc(USBTX, USBRX); |
scohennm | 0:203b4129a213 | 34 | |
scohennm | 0:203b4129a213 | 35 | |
scohennm | 0:203b4129a213 | 36 | |
scohennm | 0:203b4129a213 | 37 | void LCDMess(char *lMess, float dWait){ |
scohennm | 0:203b4129a213 | 38 | slcd.Home(); |
scohennm | 0:203b4129a213 | 39 | slcd.clear(); |
scohennm | 0:203b4129a213 | 40 | slcd.printf(lMess); |
scohennm | 0:203b4129a213 | 41 | wait(dWait); |
scohennm | 0:203b4129a213 | 42 | } |
scohennm | 0:203b4129a213 | 43 | |
scohennm | 0:203b4129a213 | 44 | |
scohennm | 0:203b4129a213 | 45 | int main() { |
scohennm | 0:203b4129a213 | 46 | float xAcc; |
scohennm | 0:203b4129a213 | 47 | float yAcc; |
scohennm | 0:203b4129a213 | 48 | float vector; |
scohennm | 0:203b4129a213 | 49 | char lcdData[10]; //buffer needs places dor decimal pt and colon |
scohennm | 0:203b4129a213 | 50 | |
scohennm | 0:203b4129a213 | 51 | #ifdef PRINTDBUG |
scohennm | 0:203b4129a213 | 52 | pc.printf(PROGNAME); |
scohennm | 0:203b4129a213 | 53 | #endif |
scohennm | 0:203b4129a213 | 54 | // main loop forever |
scohennm | 0:203b4129a213 | 55 | while(true) { |
scohennm | 0:203b4129a213 | 56 | |
scohennm | 0:203b4129a213 | 57 | //Get accelerometer data - tilt angles minus offset for zero mark. |
scohennm | 0:203b4129a213 | 58 | xAcc = abs(acc.getAccX()); |
scohennm | 0:203b4129a213 | 59 | yAcc = abs(acc.getAccY()); |
scohennm | 0:203b4129a213 | 60 | // Calulate vector sum of x and y reading. |
scohennm | 0:203b4129a213 | 61 | vector = sqrt(pow(xAcc,2) + pow(yAcc,2)); |
scohennm | 0:203b4129a213 | 62 | |
scohennm | 0:203b4129a213 | 63 | |
scohennm | 0:203b4129a213 | 64 | #ifdef PRINTDBUG |
scohennm | 0:203b4129a213 | 65 | pc.printf("vector = %f\r\n", vector); |
scohennm | 0:203b4129a213 | 66 | #endif |
scohennm | 0:203b4129a213 | 67 | |
scohennm | 0:203b4129a213 | 68 | sprintf (lcdData,"%4.3f",vector); |
scohennm | 0:203b4129a213 | 69 | LCDMess(lcdData, DATATIME); |
scohennm | 0:203b4129a213 | 70 | // Wait then do the whole thing again. |
scohennm | 0:203b4129a213 | 71 | wait(DATATIME); |
scohennm | 0:203b4129a213 | 72 | } |
scohennm | 0:203b4129a213 | 73 | } |