ok

Dependencies:   MMA8451Q SLCD mbed

Fork of ACC_LCD_341_all_axies by Stanley Cohen

Committer:
scohennm
Date:
Mon Nov 28 15:43:04 2016 +0000
Revision:
5:9e4173e23e05
Parent:
acc_341.cpp@4:1d559bac561a
Child:
6:88a2bae825ae
Display all axis with button push

Who changed what in which revision?

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