Final_Project

Dependencies:   MMA8451Q8a SLCD mbed

Fork of SP_LCD_punch_mtr_8_v5_class by Siphamandla Simelane

Committer:
sphasim
Date:
Sun Apr 26 18:48:00 2015 +0000
Revision:
10:2197af0686f0
Parent:
9:795a998ebb21
just started

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 4:89ae38dc05a9 1 #include "mbed.h"
scohennm 4:89ae38dc05a9 2 #include "MMA8451Q8.h"
scohennm 4:89ae38dc05a9 3 #include "SLCD.h"
scohennm 4:89ae38dc05a9 4 /*
scohennm 4:89ae38dc05a9 5 Test of the accelerometer, digital I/O, on-board LCD screen, and 16-bit ADC.
scohennm 4:89ae38dc05a9 6 Looing at vector product of the x-y components of the accelerometer.
scohennm 4:89ae38dc05a9 7 Works pretty well. Still rough, program wise - sc 140710
scohennm 4:89ae38dc05a9 8 Addiing touch sensor to replace potentiometer.
scohennm 4:89ae38dc05a9 9 Show x and y components
scohennm 4:89ae38dc05a9 10 */
scohennm 4:89ae38dc05a9 11
scohennm 4:89ae38dc05a9 12 #define BLINKTIME 0.7
scohennm 4:89ae38dc05a9 13 #define DATATIME 0.1
scohennm 4:89ae38dc05a9 14 #define DATADISPDWELL 0.2
scohennm 4:89ae38dc05a9 15 #define NUMLEDS 2
scohennm 4:89ae38dc05a9 16 #define LEDON 0
scohennm 4:89ae38dc05a9 17 #define LEDOFF 1
scohennm 4:89ae38dc05a9 18 #define SCALING 13
scohennm 4:89ae38dc05a9 19 #define LCDLEN 10
scohennm 4:89ae38dc05a9 20 #define RSTARTMESS "PNCH"
scohennm 4:89ae38dc05a9 21 #define MAXVECT "MAXV"
scohennm 4:89ae38dc05a9 22 #define XCOMP "XCMP"
scohennm 4:89ae38dc05a9 23 #define YCOMP "YCMP"
scohennm 4:89ae38dc05a9 24 #define ZCOMP "ZCMP"
scohennm 4:89ae38dc05a9 25 #define ANALTOVOLTS 3.3
scohennm 4:89ae38dc05a9 26 #define LEDDELAY 0.400
scohennm 4:89ae38dc05a9 27 //define states
scohennm 4:89ae38dc05a9 28 #define NUMSTATES 4
scohennm 4:89ae38dc05a9 29 #define XCOMPD 0
scohennm 4:89ae38dc05a9 30 #define YCOMPD 1
scohennm 4:89ae38dc05a9 31 #define ZCOMPD 2
scohennm 4:89ae38dc05a9 32 #define VMAXD 3
scohennm 7:6aa16a6fde70 33 #define MAXGS 8
scohennm 7:6aa16a6fde70 34 #define COUNTSCALE 1
scohennm 4:89ae38dc05a9 35 #define REG_WHO_AM_I 0x0D
scohennm 4:89ae38dc05a9 36 #define XYZ_DATA_CFG 0x0E
scohennm 4:89ae38dc05a9 37
scohennm 6:c2f31918cf77 38 #define REG_OUT_X_MSB 0x01
scohennm 6:c2f31918cf77 39 #define REG_OUT_Y_MSB 0x03
scohennm 6:c2f31918cf77 40 #define REG_OUT_Z_MSB 0x05
scohennm 6:c2f31918cf77 41
scohennm 4:89ae38dc05a9 42 #define MAX_2G 0x00
scohennm 4:89ae38dc05a9 43 #define MAX_4G 0x01
scohennm 4:89ae38dc05a9 44 #define MAX_8G 0x02
scohennm 4:89ae38dc05a9 45
scohennm 4:89ae38dc05a9 46
scohennm 4:89ae38dc05a9 47 #define PROGNAME "LCD_punch_mtr 8 v5\r/n"
scohennm 4:89ae38dc05a9 48 #define LCDNAME "PC.V5"
scohennm 4:89ae38dc05a9 49
scohennm 5:6ec1dac45861 50
scohennm 4:89ae38dc05a9 51 //#define PRINTDBUG
scohennm 4:89ae38dc05a9 52 // Accelerometer SPI pins
scohennm 4:89ae38dc05a9 53 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
scohennm 4:89ae38dc05a9 54 PinName const SDA = PTE25;
scohennm 4:89ae38dc05a9 55 PinName const SCL = PTE24;
scohennm 4:89ae38dc05a9 56 #elif defined (TARGET_KL05Z)
scohennm 4:89ae38dc05a9 57 PinName const SDA = PTB4;
scohennm 4:89ae38dc05a9 58 PinName const SCL = PTB3;
scohennm 4:89ae38dc05a9 59 #else
scohennm 4:89ae38dc05a9 60 #error TARGET NOT DEFINED
scohennm 4:89ae38dc05a9 61 #endif
scohennm 4:89ae38dc05a9 62
scohennm 4:89ae38dc05a9 63 #define MMA8451_I2C_ADDRESS (0x1d<<1)
scohennm 4:89ae38dc05a9 64
scohennm 4:89ae38dc05a9 65 SLCD slcd; //define LCD display
scohennm 4:89ae38dc05a9 66 Timer blinkTimer;
scohennm 4:89ae38dc05a9 67 Timer dataTimer;
scohennm 4:89ae38dc05a9 68 Timer displayTimer;
scohennm 4:89ae38dc05a9 69 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
scohennm 4:89ae38dc05a9 70 AnalogIn TMP01(PTB1);
scohennm 4:89ae38dc05a9 71 DigitalOut LEDs[NUMLEDS]={LED_RED, LED_GREEN}; //Indicator LEDs
scohennm 4:89ae38dc05a9 72 Serial pc(USBTX, USBRX);
scohennm 4:89ae38dc05a9 73 char displayTitles[NUMSTATES][LCDLEN] = {XCOMP,YCOMP,ZCOMP,MAXVECT};
scohennm 4:89ae38dc05a9 74 float accaxisdata[NUMSTATES];
scohennm 6:c2f31918cf77 75 char displayformats[NUMSTATES][LCDLEN] = {"%4.0f","y.%3.2f","z.%3.2f","v.%3.2f"};
scohennm 4:89ae38dc05a9 76
scohennm 4:89ae38dc05a9 77 void LCDMess(char *lMess, float dWait){
scohennm 4:89ae38dc05a9 78 slcd.Home();
scohennm 4:89ae38dc05a9 79 slcd.clear();
scohennm 4:89ae38dc05a9 80 slcd.printf(lMess);
scohennm 4:89ae38dc05a9 81 wait(dWait);
scohennm 4:89ae38dc05a9 82 }
scohennm 4:89ae38dc05a9 83 void LCDMessNoDwell(char *lMess){
scohennm 4:89ae38dc05a9 84 slcd.Home();
scohennm 4:89ae38dc05a9 85 slcd.clear();
scohennm 4:89ae38dc05a9 86 slcd.printf(lMess);
scohennm 4:89ae38dc05a9 87 }
scohennm 4:89ae38dc05a9 88
scohennm 4:89ae38dc05a9 89
scohennm 4:89ae38dc05a9 90 void allLEDsOFF(int numberOfLEDS) {
scohennm 4:89ae38dc05a9 91 int i;
scohennm 4:89ae38dc05a9 92 for (i=0;i<numberOfLEDS; i++){
scohennm 4:89ae38dc05a9 93 LEDs[i] = LEDOFF;
scohennm 4:89ae38dc05a9 94 }
scohennm 4:89ae38dc05a9 95
scohennm 4:89ae38dc05a9 96 }
scohennm 4:89ae38dc05a9 97
scohennm 4:89ae38dc05a9 98 void runLEDs(int numberOfLEDS) {
scohennm 4:89ae38dc05a9 99 int i;
scohennm 4:89ae38dc05a9 100 for (i=0;i<numberOfLEDS; i++){
scohennm 4:89ae38dc05a9 101 LEDs[i] = LEDON;
scohennm 4:89ae38dc05a9 102 wait(0.2);
scohennm 4:89ae38dc05a9 103 }
scohennm 4:89ae38dc05a9 104 allLEDsOFF(numberOfLEDS);
scohennm 4:89ae38dc05a9 105 }
scohennm 4:89ae38dc05a9 106
scohennm 4:89ae38dc05a9 107
scohennm 4:89ae38dc05a9 108
scohennm 4:89ae38dc05a9 109 int main() {
scohennm 4:89ae38dc05a9 110
scohennm 4:89ae38dc05a9 111 int RButtonState;
scohennm 4:89ae38dc05a9 112 int LButtonState;
scohennm 4:89ae38dc05a9 113 DigitalIn RtButton(PTC12);
scohennm 4:89ae38dc05a9 114 DigitalIn LftButton(PTC3);
scohennm 4:89ae38dc05a9 115 int displayState = XCOMPD;
scohennm 4:89ae38dc05a9 116 float xAcc = 0.0;
scohennm 4:89ae38dc05a9 117 float yAcc = 0.0;
scohennm 4:89ae38dc05a9 118 float zAcc = 0.0;
scohennm 4:89ae38dc05a9 119 float vector;
scohennm 4:89ae38dc05a9 120 float vMax = 0.0;
scohennm 4:89ae38dc05a9 121 float DisplayTime = DATADISPDWELL;
scohennm 4:89ae38dc05a9 122 float LEDDwell = BLINKTIME;
scohennm 4:89ae38dc05a9 123 int outState = false;
scohennm 4:89ae38dc05a9 124 char lcdData[LCDLEN]; //buffer needs places dor decimal pt and colon
scohennm 4:89ae38dc05a9 125 uint8_t regData = MAX_4G; // test value must change after G setting
scohennm 7:6aa16a6fde70 126 int16_t xCounts;
vbharam 8:ce65e42b08c9 127 int16_t yCounts;
vbharam 8:ce65e42b08c9 128 int16_t zCounts;
scohennm 4:89ae38dc05a9 129
scohennm 4:89ae38dc05a9 130 #ifdef PRINTDBUG
scohennm 4:89ae38dc05a9 131 pc.printf(PROGNAME);
scohennm 4:89ae38dc05a9 132 #endif
scohennm 4:89ae38dc05a9 133 LCDMess(LCDNAME, BLINKTIME);
scohennm 4:89ae38dc05a9 134 // Initialze readings and sequence the LED's for dramtic effect.
scohennm 4:89ae38dc05a9 135 allLEDsOFF(NUMLEDS);
scohennm 4:89ae38dc05a9 136 blinkTimer.start();
scohennm 4:89ae38dc05a9 137 blinkTimer.reset();
scohennm 4:89ae38dc05a9 138 displayTimer.start();
scohennm 4:89ae38dc05a9 139 displayTimer.reset();
scohennm 4:89ae38dc05a9 140 dataTimer.start();
scohennm 4:89ae38dc05a9 141 dataTimer.reset();
scohennm 4:89ae38dc05a9 142 LCDMess(RSTARTMESS, BLINKTIME);
scohennm 5:6ec1dac45861 143 // Go into registers ofn Accelerometer, change sensitivity then test.
scohennm 5:6ec1dac45861 144 // http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8451Q.pdf
scohennm 5:6ec1dac45861 145 // See page 26
scohennm 7:6aa16a6fde70 146 // Set 2 g max limit ****************** Note limits
scohennm 7:6aa16a6fde70 147 acc.setGLimit(MAX_2G); // For now set to 2g
scohennm 4:89ae38dc05a9 148 acc.readRegs(XYZ_DATA_CFG, &regData, 1);
scohennm 4:89ae38dc05a9 149 sprintf (lcdData,"%d",regData);
scohennm 4:89ae38dc05a9 150 LCDMess(lcdData,BLINKTIME);
scohennm 4:89ae38dc05a9 151 acc.readRegs(REG_WHO_AM_I, &regData, 1);
scohennm 4:89ae38dc05a9 152 sprintf (lcdData,"%d",regData);
scohennm 4:89ae38dc05a9 153 LCDMess(lcdData,BLINKTIME);
scohennm 4:89ae38dc05a9 154 // main loop forever
scohennm 4:89ae38dc05a9 155 while(true) {
scohennm 4:89ae38dc05a9 156
scohennm 4:89ae38dc05a9 157 // Handle user input for display selections
scohennm 4:89ae38dc05a9 158 RButtonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream
scohennm 4:89ae38dc05a9 159 if (RButtonState){
scohennm 4:89ae38dc05a9 160 vMax = 0.0; // Clear vMax
scohennm 4:89ae38dc05a9 161 LCDMess(RSTARTMESS, BLINKTIME);
scohennm 4:89ae38dc05a9 162 }
scohennm 4:89ae38dc05a9 163 LButtonState = !LftButton;
scohennm 4:89ae38dc05a9 164 if (LButtonState) { //Change data that is displayed
scohennm 4:89ae38dc05a9 165 displayState = (displayState + 1) % NUMSTATES;
scohennm 4:89ae38dc05a9 166 // Change to switch/case soon.
scohennm 4:89ae38dc05a9 167 LCDMess(displayTitles[displayState],BLINKTIME);
scohennm 4:89ae38dc05a9 168 }
sphasim 10:2197af0686f0 169
sphasim 10:2197af0686f0 170 ///STEPS PARAMETER
sphasim 10:2197af0686f0 171 //Set up a digital filter using four registers and a summing unit
sphasim 10:2197af0686f0 172
sphasim 10:2197af0686f0 173
sphasim 10:2197af0686f0 174 //Set up the dynamic threshold and dynamic precision
sphasim 10:2197af0686f0 175 //continuously updates the maximum and minimumvalues of the 3-axis acceleration every 50samples
sphasim 10:2197af0686f0 176 //the average value, (Max+min)/2 is the dynamic threshold level
sphasim 10:2197af0686f0 177
sphasim 10:2197af0686f0 178
sphasim 10:2197af0686f0 179 //set up the peak Detection
sphasim 10:2197af0686f0 180
sphasim 10:2197af0686f0 181 //set up time window
sphasim 10:2197af0686f0 182
sphasim 10:2197af0686f0 183
sphasim 10:2197af0686f0 184 ///DISTANCE PARAMETERS
sphasim 10:2197af0686f0 185 //s=number of steps*distance per step
sphasim 10:2197af0686f0 186
sphasim 10:2197af0686f0 187
sphasim 10:2197af0686f0 188 ///SPEED PARAMETER
sphasim 10:2197af0686f0 189 //d=s/t
scohennm 4:89ae38dc05a9 190 // --------------------------------------------
scohennm 4:89ae38dc05a9 191 while (dataTimer.read() > DATATIME){
scohennm 7:6aa16a6fde70 192
scohennm 4:89ae38dc05a9 193 // No offset
scohennm 4:89ae38dc05a9 194 xAcc = abs(acc.getAccX());
scohennm 6:c2f31918cf77 195 xCounts = acc.getAccAxis(REG_OUT_X_MSB);
scohennm 4:89ae38dc05a9 196 yAcc = abs(acc.getAccY());
vbharam 8:ce65e42b08c9 197 yCounts = acc.getAccAxis(REG_OUT_Y_MSB);
scohennm 4:89ae38dc05a9 198 zAcc = abs(acc.getAccZ());
vbharam 8:ce65e42b08c9 199 zCounts = acc.getAccAxis(REG_OUT_Z_MSB);
scohennm 4:89ae38dc05a9 200 // Calulate vector sum of x,y and z reading.
scohennm 4:89ae38dc05a9 201 vector = sqrt(pow(xAcc,2) + pow(zAcc,2));
scohennm 4:89ae38dc05a9 202 vector = zAcc;
scohennm 4:89ae38dc05a9 203 if (vector > vMax) {
scohennm 4:89ae38dc05a9 204 vMax = vector;
scohennm 4:89ae38dc05a9 205 }
sphasim 10:2197af0686f0 206
scohennm 7:6aa16a6fde70 207 //Prepare data for LCD display
scohennm 7:6aa16a6fde70 208 accaxisdata[XCOMPD] = abs((float)xCounts/COUNTSCALE); // scalling is set to 1 at this point
vbharam 8:ce65e42b08c9 209 accaxisdata[YCOMPD] = abs((float)yCounts/COUNTSCALE);
vbharam 8:ce65e42b08c9 210 accaxisdata[ZCOMPD] = abs((float)zCounts/COUNTSCALE);
scohennm 4:89ae38dc05a9 211 accaxisdata[VMAXD] = vMax;
scohennm 4:89ae38dc05a9 212 dataTimer.reset();
scohennm 4:89ae38dc05a9 213 LEDDwell = 1.1 - vMax/MAXGS;
scohennm 4:89ae38dc05a9 214 }
scohennm 7:6aa16a6fde70 215
scohennm 4:89ae38dc05a9 216 // Display the appropriate data on the LCD based upon what mode was chosen
scohennm 4:89ae38dc05a9 217 while (displayTimer.read() > DisplayTime){
scohennm 4:89ae38dc05a9 218 sprintf (lcdData,displayformats[displayState],accaxisdata[displayState]);
scohennm 4:89ae38dc05a9 219 LCDMessNoDwell(lcdData);
scohennm 4:89ae38dc05a9 220 displayTimer.reset();
scohennm 4:89ae38dc05a9 221 } // displaytimer
scohennm 4:89ae38dc05a9 222 // Wait then do the whole thing again.
scohennm 4:89ae38dc05a9 223 // Alive LEDs
scohennm 4:89ae38dc05a9 224 while(blinkTimer.read()> LEDDwell) {
scohennm 4:89ae38dc05a9 225 LEDs[0].write(outState);
scohennm 4:89ae38dc05a9 226 LEDs[1].write(!outState);
scohennm 4:89ae38dc05a9 227 outState = !outState;
scohennm 4:89ae38dc05a9 228 blinkTimer.reset();
scohennm 4:89ae38dc05a9 229 }
scohennm 4:89ae38dc05a9 230 }//forever loop
scohennm 4:89ae38dc05a9 231 }// main