3 axis accel
Dependencies: MMA8451Q SLCD_minus_mod mbed
Fork of ACC_LCD_341_Basic by
acc_341.cpp@6:0f05e120b749, 2016-11-21 (annotated)
- Committer:
- eseifert
- Date:
- Mon Nov 21 04:43:21 2016 +0000
- Revision:
- 6:0f05e120b749
- Parent:
- 5:29c6ba524263
ssd541-hw14.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:203b4129a213 | 1 | #include "mbed.h" |
eseifert | 6:0f05e120b749 | 2 | #include <math.h> |
scohennm | 0:203b4129a213 | 3 | #include "MMA8451Q.h" |
scohennm | 0:203b4129a213 | 4 | #include "SLCD.h" |
scohennm | 0:203b4129a213 | 5 | |
eseifert | 6:0f05e120b749 | 6 | /* |
scohennm | 0:203b4129a213 | 7 | Test of the accelerometer, digital I/O, on-board LCD screen. |
scohennm | 0:203b4129a213 | 8 | Looing at vector product of the x-y components of the accelerometer. |
scohennm | 0:203b4129a213 | 9 | Works pretty well. Still rough, program wise - sc 140710 |
scohennm | 0:203b4129a213 | 10 | */ |
scohennm | 0:203b4129a213 | 11 | |
scohennm | 3:c5291c70af48 | 12 | #define DATAINTERVAL 0.200 |
scohennm | 4:1d559bac561a | 13 | #define LCDDATALEN 10 |
eseifert | 6:0f05e120b749 | 14 | // buttons |
eseifert | 6:0f05e120b749 | 15 | #define NUMBUTS 1 |
eseifert | 6:0f05e120b749 | 16 | #define LBUT PTC12 // port addresses for buttons |
eseifert | 6:0f05e120b749 | 17 | //#define RBUT PTC3 |
eseifert | 6:0f05e120b749 | 18 | #define BUTTONTIME 0.2 |
eseifert | 6:0f05e120b749 | 19 | // lcd title |
eseifert | 6:0f05e120b749 | 20 | #define LCDTITLE "ACCL" |
eseifert | 6:0f05e120b749 | 21 | #define TITLEWAIT .8 |
eseifert | 6:0f05e120b749 | 22 | // serial title |
scohennm | 3:c5291c70af48 | 23 | #define PROGNAME "ACCLCD341-541\r\n" |
scohennm | 0:203b4129a213 | 24 | |
scohennm | 0:203b4129a213 | 25 | #define PRINTDBUG |
eseifert | 6:0f05e120b749 | 26 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
eseifert | 6:0f05e120b749 | 27 | PinName const SDA = PTE25; // Data pins for the accelerometer/magnetometer. |
eseifert | 6:0f05e120b749 | 28 | PinName const SCL = PTE24; // DO NOT CHANGE |
scohennm | 0:203b4129a213 | 29 | #endif |
scohennm | 0:203b4129a213 | 30 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 0:203b4129a213 | 31 | SLCD slcd; //define LCD display |
scohennm | 4:1d559bac561a | 32 | char lcdData[LCDDATALEN]; //buffer needs places dor decimal pt and colon |
scohennm | 0:203b4129a213 | 33 | |
scohennm | 0:203b4129a213 | 34 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 0:203b4129a213 | 35 | Serial pc(USBTX, USBRX); |
eseifert | 6:0f05e120b749 | 36 | Timer DataTimer; |
scohennm | 0:203b4129a213 | 37 | |
eseifert | 6:0f05e120b749 | 38 | Timer ButtonTimer; // for reading button states |
eseifert | 6:0f05e120b749 | 39 | DigitalIn buttons[NUMBUTS] = {LBUT}; |
scohennm | 0:203b4129a213 | 40 | |
eseifert | 6:0f05e120b749 | 41 | void LCDMess(char *lMess) |
eseifert | 6:0f05e120b749 | 42 | { |
eseifert | 6:0f05e120b749 | 43 | slcd.Home(); |
eseifert | 6:0f05e120b749 | 44 | slcd.clear(); |
eseifert | 6:0f05e120b749 | 45 | slcd.printf(lMess); |
eseifert | 6:0f05e120b749 | 46 | } |
scohennm | 4:1d559bac561a | 47 | |
eseifert | 6:0f05e120b749 | 48 | void initialize_global_vars() |
eseifert | 6:0f05e120b749 | 49 | { |
eseifert | 6:0f05e120b749 | 50 | // print lcd title |
eseifert | 6:0f05e120b749 | 51 | pc.printf(PROGNAME); |
eseifert | 6:0f05e120b749 | 52 | LCDMess(LCDTITLE); |
eseifert | 6:0f05e120b749 | 53 | wait(TITLEWAIT); |
eseifert | 6:0f05e120b749 | 54 | // set up DAQ timers |
eseifert | 6:0f05e120b749 | 55 | DataTimer.start(); |
eseifert | 6:0f05e120b749 | 56 | DataTimer.reset(); |
eseifert | 6:0f05e120b749 | 57 | // set up button timers |
eseifert | 6:0f05e120b749 | 58 | ButtonTimer.start(); |
eseifert | 6:0f05e120b749 | 59 | ButtonTimer.reset(); |
eseifert | 6:0f05e120b749 | 60 | } |
scohennm | 4:1d559bac561a | 61 | |
eseifert | 6:0f05e120b749 | 62 | void LCDsignedFloat(float theNumber) |
eseifert | 6:0f05e120b749 | 63 | { |
eseifert | 6:0f05e120b749 | 64 | sprintf (lcdData," %3.2f",theNumber); |
eseifert | 6:0f05e120b749 | 65 | if (theNumber < 0.0) sprintf (lcdData,"<%3.2f",fabs(theNumber)); |
eseifert | 6:0f05e120b749 | 66 | LCDMess(lcdData); |
eseifert | 6:0f05e120b749 | 67 | } |
scohennm | 0:203b4129a213 | 68 | |
eseifert | 6:0f05e120b749 | 69 | int main() |
eseifert | 6:0f05e120b749 | 70 | { |
eseifert | 6:0f05e120b749 | 71 | int displayState = 0; |
eseifert | 6:0f05e120b749 | 72 | char axis = ' '; |
eseifert | 6:0f05e120b749 | 73 | float accl[3] = {0,0,0}; |
eseifert | 6:0f05e120b749 | 74 | |
scohennm | 3:c5291c70af48 | 75 | initialize_global_vars(); |
scohennm | 0:203b4129a213 | 76 | while(true) { |
eseifert | 6:0f05e120b749 | 77 | if(DataTimer.read() > DATAINTERVAL) { |
eseifert | 6:0f05e120b749 | 78 | DataTimer.reset(); |
eseifert | 6:0f05e120b749 | 79 | accl[0] = acc.getAccX(); |
eseifert | 6:0f05e120b749 | 80 | accl[1] = acc.getAccY(); |
eseifert | 6:0f05e120b749 | 81 | accl[2] = acc.getAccZ(); |
eseifert | 6:0f05e120b749 | 82 | LCDsignedFloat(accl[displayState]); |
eseifert | 6:0f05e120b749 | 83 | } |
eseifert | 6:0f05e120b749 | 84 | if (ButtonTimer > BUTTONTIME) { |
eseifert | 6:0f05e120b749 | 85 | if(!buttons[0]) { |
eseifert | 6:0f05e120b749 | 86 | // a button is pressed |
eseifert | 6:0f05e120b749 | 87 | switch(displayState) { |
eseifert | 6:0f05e120b749 | 88 | case 0: |
eseifert | 6:0f05e120b749 | 89 | axis = 'x'; |
eseifert | 6:0f05e120b749 | 90 | LCDMess(&axis); |
eseifert | 6:0f05e120b749 | 91 | wait(.5); |
eseifert | 6:0f05e120b749 | 92 | break; |
eseifert | 6:0f05e120b749 | 93 | case 1: |
eseifert | 6:0f05e120b749 | 94 | axis = 'y'; |
eseifert | 6:0f05e120b749 | 95 | LCDMess(&axis); |
eseifert | 6:0f05e120b749 | 96 | wait(.5); |
eseifert | 6:0f05e120b749 | 97 | break; |
eseifert | 6:0f05e120b749 | 98 | case 2: |
eseifert | 6:0f05e120b749 | 99 | axis = 'z'; |
eseifert | 6:0f05e120b749 | 100 | LCDMess(&axis); |
eseifert | 6:0f05e120b749 | 101 | wait(.5); |
eseifert | 6:0f05e120b749 | 102 | break; |
eseifert | 6:0f05e120b749 | 103 | } |
eseifert | 6:0f05e120b749 | 104 | displayState++; |
eseifert | 6:0f05e120b749 | 105 | displayState = displayState%3; |
eseifert | 6:0f05e120b749 | 106 | ButtonTimer.reset(); |
eseifert | 6:0f05e120b749 | 107 | } |
eseifert | 6:0f05e120b749 | 108 | } |
scohennm | 0:203b4129a213 | 109 | } |
scohennm | 0:203b4129a213 | 110 | } |