HW-15.1

Dependencies:   MMA8451Q SLCD_minus_mod mbed

Fork of ACC_LCD_341_Basic by Stanley Cohen

Committer:
scohennm
Date:
Mon Nov 14 15:50:33 2016 +0000
Revision:
5:29c6ba524263
Parent:
4:1d559bac561a
Child:
6:f53933d232fc
Changed SLCD

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 0:203b4129a213 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
scohennm 0:203b4129a213 14
scohennm 3:c5291c70af48 15 #define PROGNAME "ACCLCD341-541\r\n"
scohennm 0:203b4129a213 16
scohennm 0:203b4129a213 17 #define PRINTDBUG
scohennm 2:6003ed409def 18 //
scohennm 0:203b4129a213 19 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
scohennm 2:6003ed409def 20 PinName const SDA = PTE25; // Data pins for the accelerometer/magnetometer.
scohennm 2:6003ed409def 21 PinName const SCL = PTE24; // DO NOT CHANGE
scohennm 0:203b4129a213 22 #elif defined (TARGET_KL05Z)
scohennm 0:203b4129a213 23 PinName const SDA = PTB4;
scohennm 0:203b4129a213 24 PinName const SCL = PTB3;
scohennm 0:203b4129a213 25 #else
scohennm 0:203b4129a213 26 #error TARGET NOT DEFINED
scohennm 0:203b4129a213 27 #endif
scohennm 0:203b4129a213 28
scohennm 0:203b4129a213 29 #define MMA8451_I2C_ADDRESS (0x1d<<1)
scohennm 0:203b4129a213 30
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);
scohennm 3:c5291c70af48 36 Timer dataTimer;
scohennm 0:203b4129a213 37
scohennm 2:6003ed409def 38
scohennm 0:203b4129a213 39
scohennm 3:c5291c70af48 40 void LCDMess(char *lMess){
scohennm 0:203b4129a213 41 slcd.Home();
scohennm 0:203b4129a213 42 slcd.clear();
scohennm 0:203b4129a213 43 slcd.printf(lMess);
scohennm 0:203b4129a213 44 }
scohennm 4:1d559bac561a 45
scohennm 4:1d559bac561a 46 void LCDsignedFloat(float theNumber){
scohennm 5:29c6ba524263 47 sprintf (lcdData," %3.2f",theNumber);
scohennm 5:29c6ba524263 48 if (theNumber < 0.0) sprintf (lcdData,"<%3.2f",fabs(theNumber));
scohennm 4:1d559bac561a 49 LCDMess(lcdData);
scohennm 4:1d559bac561a 50 }
scohennm 4:1d559bac561a 51
scohennm 3:c5291c70af48 52 void initialize_global_vars(){
scohennm 3:c5291c70af48 53 pc.printf(PROGNAME);
scohennm 3:c5291c70af48 54 dataTimer.start();
scohennm 3:c5291c70af48 55 dataTimer.reset();
scohennm 3:c5291c70af48 56 }
scohennm 0:203b4129a213 57
scohennm 0:203b4129a213 58 int main() {
scohennm 0:203b4129a213 59 float xAcc;
scohennm 3:c5291c70af48 60 float yAcc;
scohennm 4:1d559bac561a 61 float zAcc;
scohennm 0:203b4129a213 62
scohennm 3:c5291c70af48 63 initialize_global_vars();
scohennm 0:203b4129a213 64 // main loop forever
scohennm 0:203b4129a213 65 while(true) {
scohennm 3:c5291c70af48 66 if(dataTimer.read() > DATAINTERVAL){
scohennm 3:c5291c70af48 67 dataTimer.reset();
scohennm 0:203b4129a213 68 //Get accelerometer data - tilt angles minus offset for zero mark.
scohennm 3:c5291c70af48 69 xAcc = acc.getAccX();
scohennm 4:1d559bac561a 70 yAcc = acc.getAccY();
scohennm 4:1d559bac561a 71 zAcc = acc.getAccZ();
scohennm 0:203b4129a213 72
scohennm 0:203b4129a213 73 #ifdef PRINTDBUG
scohennm 1:9340a340e588 74 pc.printf("xAcc = %f\r\n", xAcc);
scohennm 1:9340a340e588 75 pc.printf("yAcc = %f\r\n", yAcc);
scohennm 4:1d559bac561a 76 pc.printf("zAcc = %f\r\n", zAcc);
scohennm 0:203b4129a213 77 #endif
scohennm 0:203b4129a213 78
scohennm 5:29c6ba524263 79 LCDsignedFloat(xAcc);
scohennm 0:203b4129a213 80 // Wait then do the whole thing again.
scohennm 3:c5291c70af48 81 }
scohennm 0:203b4129a213 82 }
scohennm 0:203b4129a213 83 }