ok
Dependencies: MMA8451Q SLCD mbed
Fork of ACC_LCD_341_all_axies by
acc_all_axies.cpp@6:88a2bae825ae, 2016-12-02 (annotated)
- Committer:
- menchacaeli
- Date:
- Fri Dec 02 09:02:18 2016 +0000
- Revision:
- 6:88a2bae825ae
- Parent:
- 5:9e4173e23e05
LCD changed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:203b4129a213 | 1 | #include "mbed.h" |
menchacaeli | 6:88a2bae825ae | 2 | #include <math.h> |
menchacaeli | 6:88a2bae825ae | 3 | //#include <tgmath.h> |
menchacaeli | 6:88a2bae825ae | 4 | #include <stdio.h> |
scohennm | 0:203b4129a213 | 5 | #include "MMA8451Q.h" |
scohennm | 0:203b4129a213 | 6 | #include "SLCD.h" |
scohennm | 0:203b4129a213 | 7 | |
menchacaeli | 6:88a2bae825ae | 8 | |
scohennm | 5:9e4173e23e05 | 9 | #define NUMAXES 3 |
scohennm | 5:9e4173e23e05 | 10 | #define XAXIS 0 |
scohennm | 5:9e4173e23e05 | 11 | #define YAXIS 1 |
scohennm | 5:9e4173e23e05 | 12 | #define ZAXIS 2 |
scohennm | 5:9e4173e23e05 | 13 | #define NUMBUTS 2 |
scohennm | 5:9e4173e23e05 | 14 | #define LBUT PTC12 // port addresses for buttons |
scohennm | 5:9e4173e23e05 | 15 | #define RBUT PTC3 |
scohennm | 5:9e4173e23e05 | 16 | #define BUTTONTIME 0.200 |
scohennm | 3:c5291c70af48 | 17 | #define DATAINTERVAL 0.200 |
scohennm | 5:9e4173e23e05 | 18 | #define LCDWAIT 1.5 |
scohennm | 4:1d559bac561a | 19 | #define LCDDATALEN 10 |
menchacaeli | 6:88a2bae825ae | 20 | #define PI 3.14159265 |
scohennm | 0:203b4129a213 | 21 | |
scohennm | 5:9e4173e23e05 | 22 | #define PROGNAME "ACC_LCD_all_axes_v1\r\n" |
scohennm | 0:203b4129a213 | 23 | |
scohennm | 0:203b4129a213 | 24 | #define PRINTDBUG |
scohennm | 2:6003ed409def | 25 | // |
scohennm | 0:203b4129a213 | 26 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
scohennm | 2:6003ed409def | 27 | PinName const SDA = PTE25; // Data pins for the accelerometer/magnetometer. |
scohennm | 2:6003ed409def | 28 | PinName const SCL = PTE24; // DO NOT CHANGE |
scohennm | 0:203b4129a213 | 29 | #elif defined (TARGET_KL05Z) |
scohennm | 0:203b4129a213 | 30 | PinName const SDA = PTB4; |
scohennm | 0:203b4129a213 | 31 | PinName const SCL = PTB3; |
scohennm | 0:203b4129a213 | 32 | #else |
scohennm | 0:203b4129a213 | 33 | #error TARGET NOT DEFINED |
scohennm | 0:203b4129a213 | 34 | #endif |
scohennm | 0:203b4129a213 | 35 | |
scohennm | 0:203b4129a213 | 36 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
scohennm | 0:203b4129a213 | 37 | |
scohennm | 0:203b4129a213 | 38 | SLCD slcd; //define LCD display |
scohennm | 4:1d559bac561a | 39 | char lcdData[LCDDATALEN]; //buffer needs places dor decimal pt and colon |
scohennm | 5:9e4173e23e05 | 40 | int currentAxis = XAXIS; // xaxis |
scohennm | 0:203b4129a213 | 41 | |
scohennm | 0:203b4129a213 | 42 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
scohennm | 0:203b4129a213 | 43 | Serial pc(USBTX, USBRX); |
scohennm | 3:c5291c70af48 | 44 | Timer dataTimer; |
scohennm | 5:9e4173e23e05 | 45 | Timer ButtonTimer; // for reading button states |
scohennm | 5:9e4173e23e05 | 46 | DigitalIn buttons[NUMBUTS] = {RBUT, LBUT}; // set up buttons |
scohennm | 0:203b4129a213 | 47 | |
scohennm | 5:9e4173e23e05 | 48 | char axisName[NUMAXES][LCDDATALEN] = {"<X<","<Y<", "<Z<"}; |
scohennm | 0:203b4129a213 | 49 | |
scohennm | 3:c5291c70af48 | 50 | void LCDMess(char *lMess){ |
scohennm | 0:203b4129a213 | 51 | slcd.Home(); |
scohennm | 0:203b4129a213 | 52 | slcd.clear(); |
scohennm | 0:203b4129a213 | 53 | slcd.printf(lMess); |
scohennm | 0:203b4129a213 | 54 | } |
scohennm | 4:1d559bac561a | 55 | |
scohennm | 4:1d559bac561a | 56 | void LCDsignedFloat(float theNumber){ |
menchacaeli | 6:88a2bae825ae | 57 | sprintf (lcdData," %3.0d",theNumber); |
scohennm | 5:9e4173e23e05 | 58 | // changed SLCD.cpp to interpret < as - |
menchacaeli | 6:88a2bae825ae | 59 | if (theNumber < 0.0) sprintf (lcdData,"<%3.0d",fabs(theNumber)); |
scohennm | 4:1d559bac561a | 60 | LCDMess(lcdData); |
scohennm | 4:1d559bac561a | 61 | } |
scohennm | 4:1d559bac561a | 62 | |
menchacaeli | 6:88a2bae825ae | 63 | void LCDsignedAngle(float theAngle){ |
menchacaeli | 6:88a2bae825ae | 64 | sprintf (lcdData," %2.0f@",theAngle); |
menchacaeli | 6:88a2bae825ae | 65 | // changed SLCD.cpp to interpret @ as superscript 0 (degrees) |
menchacaeli | 6:88a2bae825ae | 66 | if (theAngle < 0.0) sprintf (lcdData,"<%2.0f@",fabs(theAngle)); |
menchacaeli | 6:88a2bae825ae | 67 | LCDMess(lcdData); |
menchacaeli | 6:88a2bae825ae | 68 | } |
menchacaeli | 6:88a2bae825ae | 69 | |
scohennm | 3:c5291c70af48 | 70 | void initialize_global_vars(){ |
scohennm | 3:c5291c70af48 | 71 | pc.printf(PROGNAME); |
scohennm | 5:9e4173e23e05 | 72 | // set up DAQ timers |
scohennm | 5:9e4173e23e05 | 73 | ButtonTimer.start(); |
scohennm | 5:9e4173e23e05 | 74 | ButtonTimer.reset(); |
scohennm | 3:c5291c70af48 | 75 | dataTimer.start(); |
scohennm | 3:c5291c70af48 | 76 | dataTimer.reset(); |
scohennm | 5:9e4173e23e05 | 77 | LCDMess(axisName[currentAxis]); |
scohennm | 5:9e4173e23e05 | 78 | wait(LCDWAIT); |
scohennm | 5:9e4173e23e05 | 79 | |
scohennm | 3:c5291c70af48 | 80 | } |
scohennm | 0:203b4129a213 | 81 | |
scohennm | 0:203b4129a213 | 82 | int main() { |
scohennm | 5:9e4173e23e05 | 83 | int i; // loop index |
menchacaeli | 6:88a2bae825ae | 84 | float axisValue[NUMAXES]; // set up an array of axis values |
scohennm | 5:9e4173e23e05 | 85 | int buttonSum = 0; |
menchacaeli | 6:88a2bae825ae | 86 | |
scohennm | 3:c5291c70af48 | 87 | initialize_global_vars(); |
scohennm | 0:203b4129a213 | 88 | // main loop forever |
scohennm | 0:203b4129a213 | 89 | while(true) { |
scohennm | 5:9e4173e23e05 | 90 | while (ButtonTimer > BUTTONTIME){ //Use a while instead of an if |
scohennm | 5:9e4173e23e05 | 91 | buttonSum = 0; // Note |
scohennm | 5:9e4173e23e05 | 92 | buttonSum = !buttons[0] + !buttons[1]; |
menchacaeli | 6:88a2bae825ae | 93 | if (buttonSum != 0) { |
menchacaeli | 6:88a2bae825ae | 94 | currentAxis = (currentAxis + 1 ) % NUMAXES; |
menchacaeli | 6:88a2bae825ae | 95 | // Cycle through the |
scohennm | 5:9e4173e23e05 | 96 | // Axis with modulus |
scohennm | 5:9e4173e23e05 | 97 | // to reset back to zero |
scohennm | 5:9e4173e23e05 | 98 | LCDMess(axisName[currentAxis]); |
scohennm | 5:9e4173e23e05 | 99 | wait(LCDWAIT); |
scohennm | 5:9e4173e23e05 | 100 | }// for loop to look at buttons |
scohennm | 5:9e4173e23e05 | 101 | ButtonTimer.reset(); // Make sure you reset the timer or.. |
scohennm | 5:9e4173e23e05 | 102 | // Groundhog Day. |
scohennm | 5:9e4173e23e05 | 103 | } |
scohennm | 5:9e4173e23e05 | 104 | while (dataTimer.read() > DATAINTERVAL){ |
scohennm | 3:c5291c70af48 | 105 | dataTimer.reset(); |
menchacaeli | 6:88a2bae825ae | 106 | axisValue[XAXIS] = acc.getAccX(); |
scohennm | 5:9e4173e23e05 | 107 | axisValue[YAXIS] = acc.getAccY(); |
scohennm | 5:9e4173e23e05 | 108 | axisValue[ZAXIS] = acc.getAccZ(); |
scohennm | 0:203b4129a213 | 109 | #ifdef PRINTDBUG |
scohennm | 5:9e4173e23e05 | 110 | for (i=0; i< NUMAXES;i++){ |
menchacaeli | 6:88a2bae825ae | 111 | pc.printf("Acc %d = %f@\r\n",i, axisValue[i]); |
scohennm | 5:9e4173e23e05 | 112 | } |
menchacaeli | 6:88a2bae825ae | 113 | #endif |
menchacaeli | 6:88a2bae825ae | 114 | //axisRead = axisValue[currentAxis]; |
menchacaeli | 6:88a2bae825ae | 115 | LCDsignedAngle(axisValue[currentAxis]); |
scohennm | 3:c5291c70af48 | 116 | } |
scohennm | 0:203b4129a213 | 117 | } |
scohennm | 0:203b4129a213 | 118 | } |