added switch case for g limit

Dependencies:   EMench_MMA8451Q SLCD mbed

Fork of LCD_ACC_46_v4-2g by Stanley Cohen

Committer:
menchacaeli
Date:
Wed Feb 22 03:08:49 2017 +0000
Revision:
5:cba53136f4b9
Parent:
4:41920817d234
EMench_SSD645_HW6.1 added a new switch case for the g limit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 4:41920817d234 1 #include "mbed.h"
scohennm 4:41920817d234 2 #include "MMA8451Q.h"
menchacaeli 5:cba53136f4b9 3 #include "MMA8451Q8.h"
scohennm 4:41920817d234 4 #include "SLCD.h"
scohennm 4:41920817d234 5 /*
scohennm 4:41920817d234 6 Test of the accelerometer, digital I/O, on-board LCD screen, and 16-bit ADC.
scohennm 4:41920817d234 7
scohennm 4:41920817d234 8 */
menchacaeli 5:cba53136f4b9 9 // Library MMA8451Q defines
menchacaeli 5:cba53136f4b9 10 #define REG_WHO_AM_I 0x0D
menchacaeli 5:cba53136f4b9 11 #define REG_CTRL_REG_1 0x2A
menchacaeli 5:cba53136f4b9 12 #define REG_OUT_X_MSB 0x01
menchacaeli 5:cba53136f4b9 13 #define REG_OUT_Y_MSB 0x03
menchacaeli 5:cba53136f4b9 14 #define REG_OUT_Z_MSB 0x05
menchacaeli 5:cba53136f4b9 15 #define XYZ_DATA_CFG 0x0E
menchacaeli 5:cba53136f4b9 16
menchacaeli 5:cba53136f4b9 17 #define UINT14_MAX 16383
menchacaeli 5:cba53136f4b9 18
menchacaeli 5:cba53136f4b9 19 #define MAX_2G 0x00
menchacaeli 5:cba53136f4b9 20 #define MAX_4G 0x01
menchacaeli 5:cba53136f4b9 21 #define MAX_8G 0x02
menchacaeli 5:cba53136f4b9 22
menchacaeli 5:cba53136f4b9 23 #define GSCALING 1024.0
scohennm 4:41920817d234 24
scohennm 4:41920817d234 25 #define BLINKTIME 0.7
scohennm 4:41920817d234 26 #define DATATIME 0.1
scohennm 4:41920817d234 27 #define DATADISPDWELL 0.2
scohennm 4:41920817d234 28 #define NUMLEDS 2
scohennm 4:41920817d234 29 #define LEDON 0
scohennm 4:41920817d234 30 #define LEDOFF 1
scohennm 4:41920817d234 31 #define SCALING 13
scohennm 4:41920817d234 32 #define RSTARTMESS "RSET"
scohennm 4:41920817d234 33 #define MAXVECT "MAXV"
scohennm 4:41920817d234 34 #define XCOMP "XCMP"
scohennm 4:41920817d234 35 #define YCOMP "YCMP"
scohennm 4:41920817d234 36 #define ZCOMP "ZCMP"
menchacaeli 5:cba53136f4b9 37 #define XRAWP "XRAW"
menchacaeli 5:cba53136f4b9 38 #define GLIMP "GLIM"
scohennm 4:41920817d234 39 #define ANALTOVOLTS 3.3
scohennm 4:41920817d234 40 #define LEDDELAY 0.400
scohennm 4:41920817d234 41 //define states
menchacaeli 5:cba53136f4b9 42 #define NUMSTATES 5
scohennm 4:41920817d234 43 #define XCOMPD 0
scohennm 4:41920817d234 44 #define YCOMPD 1
scohennm 4:41920817d234 45 #define ZCOMPD 2
scohennm 4:41920817d234 46 #define VMAXD 3
menchacaeli 5:cba53136f4b9 47 #define XRAWD 4
scohennm 4:41920817d234 48
scohennm 4:41920817d234 49 #define PROGNAME "LCD_ACC_LCDv3 46\r/n"
scohennm 4:41920817d234 50
scohennm 4:41920817d234 51 //#define PRINTDBUG
scohennm 4:41920817d234 52 // Accelerometer SPI pins
scohennm 4:41920817d234 53 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
scohennm 4:41920817d234 54 PinName const SDA = PTE25;
scohennm 4:41920817d234 55 PinName const SCL = PTE24;
scohennm 4:41920817d234 56 #elif defined (TARGET_KL05Z)
scohennm 4:41920817d234 57 PinName const SDA = PTB4;
scohennm 4:41920817d234 58 PinName const SCL = PTB3;
scohennm 4:41920817d234 59 #else
scohennm 4:41920817d234 60 #error TARGET NOT DEFINED
scohennm 4:41920817d234 61 #endif
scohennm 4:41920817d234 62
scohennm 4:41920817d234 63 #define MMA8451_I2C_ADDRESS (0x1d<<1)
scohennm 4:41920817d234 64
scohennm 4:41920817d234 65 SLCD slcd; //define LCD display
scohennm 4:41920817d234 66 Timer blinkTimer;
scohennm 4:41920817d234 67 Timer dataTimer;
scohennm 4:41920817d234 68 Timer displayTimer;
scohennm 4:41920817d234 69 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
scohennm 4:41920817d234 70 AnalogIn TMP01(PTB1);
scohennm 4:41920817d234 71 DigitalOut LEDs[NUMLEDS]={LED_RED, LED_GREEN}; //Indicator LEDs
scohennm 4:41920817d234 72 Serial pc(USBTX, USBRX);
scohennm 4:41920817d234 73
menchacaeli 5:cba53136f4b9 74 int16_t xAccBits; // making local creates a warning
scohennm 4:41920817d234 75
scohennm 4:41920817d234 76 void LCDMess(char *lMess, float dWait){
scohennm 4:41920817d234 77 slcd.Home();
scohennm 4:41920817d234 78 slcd.clear();
scohennm 4:41920817d234 79 slcd.printf(lMess);
scohennm 4:41920817d234 80 wait(dWait);
scohennm 4:41920817d234 81 }
scohennm 4:41920817d234 82 void LCDMessNoDwell(char *lMess){
scohennm 4:41920817d234 83 slcd.Home();
scohennm 4:41920817d234 84 slcd.clear();
scohennm 4:41920817d234 85 slcd.printf(lMess);
scohennm 4:41920817d234 86 }
scohennm 4:41920817d234 87
scohennm 4:41920817d234 88
scohennm 4:41920817d234 89 void allLEDsOFF(int numberOfLEDS) {
scohennm 4:41920817d234 90 int i;
scohennm 4:41920817d234 91 for (i=0;i<numberOfLEDS; i++){
scohennm 4:41920817d234 92 LEDs[i] = LEDOFF;
scohennm 4:41920817d234 93 }
scohennm 4:41920817d234 94 }
scohennm 4:41920817d234 95
scohennm 4:41920817d234 96 int main() {
scohennm 4:41920817d234 97
scohennm 4:41920817d234 98 int RButtonState;
scohennm 4:41920817d234 99 int LButtonState;
scohennm 4:41920817d234 100 DigitalIn RtButton(PTC12);
scohennm 4:41920817d234 101 DigitalIn LftButton(PTC3);
scohennm 4:41920817d234 102 int displayState = XCOMPD;
scohennm 4:41920817d234 103 float xAcc = 0.0;
scohennm 4:41920817d234 104 float yAcc = 0.0;
scohennm 4:41920817d234 105 float zAcc = 0.0;
scohennm 4:41920817d234 106 float vector;
scohennm 4:41920817d234 107 float vMax = 0.0;
scohennm 4:41920817d234 108 float DisplayTime = DATADISPDWELL;
scohennm 4:41920817d234 109 int outState = false;
scohennm 4:41920817d234 110 char lcdData[10]; //buffer needs places for decimal pt and colon
scohennm 4:41920817d234 111
scohennm 4:41920817d234 112
scohennm 4:41920817d234 113 #ifdef PRINTDBUG
scohennm 4:41920817d234 114 pc.printf(PROGNAME);
scohennm 4:41920817d234 115 #endif
scohennm 4:41920817d234 116 LCDMess(RSTARTMESS, BLINKTIME);
scohennm 4:41920817d234 117 // Initialze readings and sequence the LED's for dramtic effect.
scohennm 4:41920817d234 118 allLEDsOFF(NUMLEDS);
scohennm 4:41920817d234 119 blinkTimer.start();
scohennm 4:41920817d234 120 blinkTimer.reset();
scohennm 4:41920817d234 121 displayTimer.start();
scohennm 4:41920817d234 122 displayTimer.reset();
scohennm 4:41920817d234 123 dataTimer.start();
scohennm 4:41920817d234 124 dataTimer.reset();
scohennm 4:41920817d234 125
scohennm 4:41920817d234 126 // main loop forever
scohennm 4:41920817d234 127 while(true) {
scohennm 4:41920817d234 128
scohennm 4:41920817d234 129 // Handle user input for display selections
scohennm 4:41920817d234 130 RButtonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream
scohennm 4:41920817d234 131 if (RButtonState){
scohennm 4:41920817d234 132 vMax = 0.0; // Clear vMax
scohennm 4:41920817d234 133 LCDMess(RSTARTMESS, BLINKTIME);
scohennm 4:41920817d234 134 }
scohennm 4:41920817d234 135 LButtonState = !LftButton;
scohennm 4:41920817d234 136 if (LButtonState) { //Change data that is displayed
scohennm 4:41920817d234 137 displayState = (displayState + 1) % NUMSTATES;
scohennm 4:41920817d234 138 // Change to switch/case soon.
scohennm 4:41920817d234 139 switch (displayState){
scohennm 4:41920817d234 140 case XCOMPD: {
scohennm 4:41920817d234 141 LCDMess(XCOMP,BLINKTIME);
scohennm 4:41920817d234 142 break;
scohennm 4:41920817d234 143 }
scohennm 4:41920817d234 144 case YCOMPD: {
scohennm 4:41920817d234 145 LCDMess(YCOMP,BLINKTIME);
scohennm 4:41920817d234 146 break;
scohennm 4:41920817d234 147 }
scohennm 4:41920817d234 148 case ZCOMPD: {
scohennm 4:41920817d234 149 LCDMess(ZCOMP,BLINKTIME);
scohennm 4:41920817d234 150 break;
scohennm 4:41920817d234 151 }
menchacaeli 5:cba53136f4b9 152 case VMAXD: {
scohennm 4:41920817d234 153 LCDMess(MAXVECT,BLINKTIME);
scohennm 4:41920817d234 154 break;
scohennm 4:41920817d234 155 }
menchacaeli 5:cba53136f4b9 156 case XRAWD: {
menchacaeli 5:cba53136f4b9 157 LCDMess(XRAWP,BLINKTIME);
menchacaeli 5:cba53136f4b9 158 break;
menchacaeli 5:cba53136f4b9 159 }
menchacaeli 5:cba53136f4b9 160 case GLIMD: {
menchacaeli 5:cba53136f4b9 161 LCDMess(GLIMP,BLINKTIME);
menchacaeli 5:cba53136f4b9 162 break;
menchacaeli 5:cba53136f4b9 163 }
scohennm 4:41920817d234 164 }// switch
scohennm 4:41920817d234 165 }
scohennm 4:41920817d234 166
scohennm 4:41920817d234 167 // --------------------------------------------
scohennm 4:41920817d234 168 while (dataTimer.read() > DATATIME){
scohennm 4:41920817d234 169 // No offset
menchacaeli 5:cba53136f4b9 170 xAcc = abs(acc.getAccX());
menchacaeli 5:cba53136f4b9 171 yAcc = abs(acc.getAccY());
scohennm 4:41920817d234 172 zAcc = abs(acc.getAccZ());
menchacaeli 5:cba53136f4b9 173 // test getting raw bits
menchacaeli 5:cba53136f4b9 174 xAccBits = acc.getAccAxis(REG_OUT_X_MSB);
menchacaeli 5:cba53136f4b9 175 // test g limit
menchacaeli 5:cba53136f4b9 176 gLimit = data[2].setGLimit();
scohennm 4:41920817d234 177 // Calulate vector sum of x,y and z reading.
menchacaeli 5:cba53136f4b9 178 // vector = sqrt(pow(xAcc,2) + pow(zAcc,2));
scohennm 4:41920817d234 179 vector = zAcc;
scohennm 4:41920817d234 180 if (vector > vMax) {
scohennm 4:41920817d234 181 vMax = vector;
scohennm 4:41920817d234 182 }
scohennm 4:41920817d234 183 dataTimer.reset();
scohennm 4:41920817d234 184 }
scohennm 4:41920817d234 185
scohennm 4:41920817d234 186 // Display the appropriate data on the LCD based upon what mode was chosen
scohennm 4:41920817d234 187 while (displayTimer.read() > DisplayTime){
scohennm 4:41920817d234 188 switch (displayState) {
scohennm 4:41920817d234 189 case XCOMPD: {
scohennm 4:41920817d234 190 sprintf (lcdData,"%4.3f",xAcc);
scohennm 4:41920817d234 191 break;
scohennm 4:41920817d234 192 }
scohennm 4:41920817d234 193 case YCOMPD: {
scohennm 4:41920817d234 194 sprintf (lcdData,"%4.3f",yAcc);
scohennm 4:41920817d234 195 break;
scohennm 4:41920817d234 196 }
scohennm 4:41920817d234 197 case ZCOMPD: {
scohennm 4:41920817d234 198 sprintf (lcdData,"%4.3f",zAcc);
scohennm 4:41920817d234 199 break;
scohennm 4:41920817d234 200 }
scohennm 4:41920817d234 201 case VMAXD:{
scohennm 4:41920817d234 202 sprintf (lcdData,"%4.3f",vMax);
scohennm 4:41920817d234 203 break;
scohennm 4:41920817d234 204 }
menchacaeli 5:cba53136f4b9 205 case XRAWD:{
menchacaeli 5:cba53136f4b9 206 sprintf (lcdData,"%4d",xAccBits);
menchacaeli 5:cba53136f4b9 207 break;
menchacaeli 5:cba53136f4b9 208 }
menchacaeli 5:cba53136f4b9 209 case GLIMD:{
menchacaeli 5:cba53136f4b9 210 sprintf (lcdData,"%4d",gLimit);
menchacaeli 5:cba53136f4b9 211 break;
menchacaeli 5:cba53136f4b9 212 }
scohennm 4:41920817d234 213 }
scohennm 4:41920817d234 214 LCDMessNoDwell(lcdData);
scohennm 4:41920817d234 215 displayTimer.reset();
scohennm 4:41920817d234 216 } // displaytimer
scohennm 4:41920817d234 217 // Wait then do the whole thing again.
scohennm 4:41920817d234 218 // Alive LEDs
scohennm 4:41920817d234 219 while(blinkTimer.read()> LEDDELAY) {
scohennm 4:41920817d234 220 LEDs[0].write(outState);
scohennm 4:41920817d234 221 LEDs[1].write(!outState);
scohennm 4:41920817d234 222 outState = !outState;
scohennm 4:41920817d234 223 blinkTimer.reset();
scohennm 4:41920817d234 224 }
scohennm 4:41920817d234 225 }//forever loop
scohennm 4:41920817d234 226 }// main