KBrat-SSD541-HW-15-1

Dependencies:   MMA8451Q KBrat-SSD541-HW-15-1 mbed

Fork of ACC_LCD_341_Basic by Stanley Cohen

Committer:
tisbrat
Date:
Mon Nov 28 16:40:30 2016 +0000
Revision:
6:4750e5732aca
Parent:
5:29c6ba524263
KBrat-SSD541-HW-15-1

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"
tisbrat 6:4750e5732aca 5 #define NUMBUTS 2 //two buttons
tisbrat 6:4750e5732aca 6 #define LBUT PTC12 //left button // port addresses for buttons
tisbrat 6:4750e5732aca 7 #define RBUT PTC3 //right button
scohennm 0:203b4129a213 8
scohennm 0:203b4129a213 9 /*
scohennm 0:203b4129a213 10 Test of the accelerometer, digital I/O, on-board LCD screen.
scohennm 0:203b4129a213 11 Looing at vector product of the x-y components of the accelerometer.
scohennm 0:203b4129a213 12 Works pretty well. Still rough, program wise - sc 140710
scohennm 0:203b4129a213 13 */
scohennm 0:203b4129a213 14
tisbrat 6:4750e5732aca 15 //#define DATAINTERVAL 0.200
tisbrat 6:4750e5732aca 16 #define DATAINTERVAL 1.0
scohennm 4:1d559bac561a 17 #define LCDDATALEN 10
tisbrat 6:4750e5732aca 18 #define BUTTONTIME 0.2
tisbrat 6:4750e5732aca 19 #define PROGNAME "KBrat-SSD541-HW-15.1\r\n"
scohennm 0:203b4129a213 20
scohennm 0:203b4129a213 21 #define PRINTDBUG
tisbrat 6:4750e5732aca 22
tisbrat 6:4750e5732aca 23 //----------------------------//
tisbrat 6:4750e5732aca 24
tisbrat 6:4750e5732aca 25
tisbrat 6:4750e5732aca 26
tisbrat 6:4750e5732aca 27 //----------------------------//
tisbrat 6:4750e5732aca 28
scohennm 0:203b4129a213 29 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
scohennm 2:6003ed409def 30 PinName const SDA = PTE25; // Data pins for the accelerometer/magnetometer.
scohennm 2:6003ed409def 31 PinName const SCL = PTE24; // DO NOT CHANGE
scohennm 0:203b4129a213 32 #elif defined (TARGET_KL05Z)
scohennm 0:203b4129a213 33 PinName const SDA = PTB4;
scohennm 0:203b4129a213 34 PinName const SCL = PTB3;
scohennm 0:203b4129a213 35 #else
scohennm 0:203b4129a213 36 #error TARGET NOT DEFINED
scohennm 0:203b4129a213 37 #endif
scohennm 0:203b4129a213 38
scohennm 0:203b4129a213 39 #define MMA8451_I2C_ADDRESS (0x1d<<1)
scohennm 0:203b4129a213 40
scohennm 0:203b4129a213 41 SLCD slcd; //define LCD display
scohennm 4:1d559bac561a 42 char lcdData[LCDDATALEN]; //buffer needs places dor decimal pt and colon
scohennm 0:203b4129a213 43
scohennm 0:203b4129a213 44 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
tisbrat 6:4750e5732aca 45 Serial pc(USBTX, USBRX);// set up USB as communicationis to Host PC via USB connectons
tisbrat 6:4750e5732aca 46 Timer ButtonTimer; // for reading button states
scohennm 0:203b4129a213 47
tisbrat 6:4750e5732aca 48 DigitalIn buttons[NUMBUTS] = {RBUT, LBUT};
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){
scohennm 5:29c6ba524263 57 sprintf (lcdData," %3.2f",theNumber);
scohennm 5:29c6ba524263 58 if (theNumber < 0.0) sprintf (lcdData,"<%3.2f",fabs(theNumber));
scohennm 4:1d559bac561a 59 LCDMess(lcdData);
scohennm 4:1d559bac561a 60 }
scohennm 4:1d559bac561a 61
scohennm 3:c5291c70af48 62 void initialize_global_vars(){
tisbrat 6:4750e5732aca 63 pc.printf(PROGNAME);
tisbrat 6:4750e5732aca 64 // set up DAQ timers
tisbrat 6:4750e5732aca 65 ButtonTimer.start();
tisbrat 6:4750e5732aca 66 ButtonTimer.reset();
scohennm 3:c5291c70af48 67 }
scohennm 0:203b4129a213 68
tisbrat 6:4750e5732aca 69
scohennm 0:203b4129a213 70 int main() {
scohennm 0:203b4129a213 71 float xAcc;
scohennm 3:c5291c70af48 72 float yAcc;
scohennm 4:1d559bac561a 73 float zAcc;
scohennm 0:203b4129a213 74
tisbrat 6:4750e5732aca 75 //initialize_global_vars();
tisbrat 6:4750e5732aca 76
tisbrat 6:4750e5732aca 77 int i;
tisbrat 6:4750e5732aca 78
tisbrat 6:4750e5732aca 79 initialize_global_vars(); //keep things organized
tisbrat 6:4750e5732aca 80
tisbrat 6:4750e5732aca 81 // End of setup
scohennm 0:203b4129a213 82 while(true) {
tisbrat 6:4750e5732aca 83 if (ButtonTimer > BUTTONTIME){
tisbrat 6:4750e5732aca 84 for (i=0; i<NUMBUTS; i++){ // index will be 0 or 1
tisbrat 6:4750e5732aca 85 if(!buttons[i]) {
tisbrat 6:4750e5732aca 86 if (i==1)
tisbrat 6:4750e5732aca 87 xAcc = acc.getAccX();
tisbrat 6:4750e5732aca 88 yAcc = acc.getAccY();
tisbrat 6:4750e5732aca 89 zAcc = acc.getAccZ();
scohennm 0:203b4129a213 90
tisbrat 6:4750e5732aca 91 slcd.printf("X");
tisbrat 6:4750e5732aca 92 wait(0.3);
tisbrat 6:4750e5732aca 93 slcd.clear();
tisbrat 6:4750e5732aca 94 pc.printf("xAcc = %f\r\n", xAcc);
tisbrat 6:4750e5732aca 95 LCDsignedFloat(xAcc);
tisbrat 6:4750e5732aca 96 wait(1.5);
tisbrat 6:4750e5732aca 97
tisbrat 6:4750e5732aca 98 slcd.printf("Y");
tisbrat 6:4750e5732aca 99 wait(0.3);
tisbrat 6:4750e5732aca 100 slcd.clear();
tisbrat 6:4750e5732aca 101 pc.printf("yAcc = %f\r\n", yAcc);
tisbrat 6:4750e5732aca 102 LCDsignedFloat(yAcc);
tisbrat 6:4750e5732aca 103 wait(1.5);
tisbrat 6:4750e5732aca 104
tisbrat 6:4750e5732aca 105 slcd.printf("z");
tisbrat 6:4750e5732aca 106 wait(0.3);
tisbrat 6:4750e5732aca 107 slcd.clear();
tisbrat 6:4750e5732aca 108 pc.printf("zAcc = %f\r\n", zAcc);
tisbrat 6:4750e5732aca 109 LCDsignedFloat(zAcc);
tisbrat 6:4750e5732aca 110
scohennm 0:203b4129a213 111 // Wait then do the whole thing again.
tisbrat 6:4750e5732aca 112
tisbrat 6:4750e5732aca 113
tisbrat 6:4750e5732aca 114 } // if ! buttons
tisbrat 6:4750e5732aca 115 }// for loop to look at buttons
tisbrat 6:4750e5732aca 116 ButtonTimer.reset();
tisbrat 6:4750e5732aca 117 }
tisbrat 6:4750e5732aca 118
scohennm 0:203b4129a213 119 }
tisbrat 6:4750e5732aca 120
tisbrat 6:4750e5732aca 121 }
tisbrat 6:4750e5732aca 122
tisbrat 6:4750e5732aca 123
tisbrat 6:4750e5732aca 124
tisbrat 6:4750e5732aca 125
tisbrat 6:4750e5732aca 126
tisbrat 6:4750e5732aca 127