Final_Project

Dependencies:   MMA8451Q8a SLCD mbed

Fork of SP_LCD_punch_mtr_8_v5_class by Siphamandla Simelane

Committer:
scohennm
Date:
Sat Jan 10 16:48:10 2015 +0000
Revision:
5:6ec1dac45861
Parent:
4:89ae38dc05a9
Child:
6:c2f31918cf77
Added more comments and reference to data sheet.

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 4:89ae38dc05a9 33 #define MAXGS 8.0
scohennm 4:89ae38dc05a9 34
scohennm 4:89ae38dc05a9 35 #define REG_WHO_AM_I 0x0D
scohennm 4:89ae38dc05a9 36 #define XYZ_DATA_CFG 0x0E
scohennm 4:89ae38dc05a9 37
scohennm 4:89ae38dc05a9 38 #define MAX_2G 0x00
scohennm 4:89ae38dc05a9 39 #define MAX_4G 0x01
scohennm 4:89ae38dc05a9 40 #define MAX_8G 0x02
scohennm 4:89ae38dc05a9 41
scohennm 4:89ae38dc05a9 42
scohennm 4:89ae38dc05a9 43 #define PROGNAME "LCD_punch_mtr 8 v5\r/n"
scohennm 4:89ae38dc05a9 44 #define LCDNAME "PC.V5"
scohennm 4:89ae38dc05a9 45
scohennm 5:6ec1dac45861 46
scohennm 4:89ae38dc05a9 47 //#define PRINTDBUG
scohennm 4:89ae38dc05a9 48 // Accelerometer SPI pins
scohennm 4:89ae38dc05a9 49 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
scohennm 4:89ae38dc05a9 50 PinName const SDA = PTE25;
scohennm 4:89ae38dc05a9 51 PinName const SCL = PTE24;
scohennm 4:89ae38dc05a9 52 #elif defined (TARGET_KL05Z)
scohennm 4:89ae38dc05a9 53 PinName const SDA = PTB4;
scohennm 4:89ae38dc05a9 54 PinName const SCL = PTB3;
scohennm 4:89ae38dc05a9 55 #else
scohennm 4:89ae38dc05a9 56 #error TARGET NOT DEFINED
scohennm 4:89ae38dc05a9 57 #endif
scohennm 4:89ae38dc05a9 58
scohennm 4:89ae38dc05a9 59 #define MMA8451_I2C_ADDRESS (0x1d<<1)
scohennm 4:89ae38dc05a9 60
scohennm 4:89ae38dc05a9 61 SLCD slcd; //define LCD display
scohennm 4:89ae38dc05a9 62 Timer blinkTimer;
scohennm 4:89ae38dc05a9 63 Timer dataTimer;
scohennm 4:89ae38dc05a9 64 Timer displayTimer;
scohennm 4:89ae38dc05a9 65 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
scohennm 4:89ae38dc05a9 66 AnalogIn TMP01(PTB1);
scohennm 4:89ae38dc05a9 67 DigitalOut LEDs[NUMLEDS]={LED_RED, LED_GREEN}; //Indicator LEDs
scohennm 4:89ae38dc05a9 68 Serial pc(USBTX, USBRX);
scohennm 4:89ae38dc05a9 69 char displayTitles[NUMSTATES][LCDLEN] = {XCOMP,YCOMP,ZCOMP,MAXVECT};
scohennm 4:89ae38dc05a9 70 float accaxisdata[NUMSTATES];
scohennm 4:89ae38dc05a9 71 char displayformats[NUMSTATES][LCDLEN] = {"x.%3.2f","y.%3.2f","z.%3.2f","v.%3.2f"};
scohennm 4:89ae38dc05a9 72 //char displayformats[NUMSTATES][LCDLEN] = {"%4.3f","%4.3f","%4.3f","%4.3f"};
scohennm 4:89ae38dc05a9 73
scohennm 4:89ae38dc05a9 74 void LCDMess(char *lMess, float dWait){
scohennm 4:89ae38dc05a9 75 slcd.Home();
scohennm 4:89ae38dc05a9 76 slcd.clear();
scohennm 4:89ae38dc05a9 77 slcd.printf(lMess);
scohennm 4:89ae38dc05a9 78 wait(dWait);
scohennm 4:89ae38dc05a9 79 }
scohennm 4:89ae38dc05a9 80 void LCDMessNoDwell(char *lMess){
scohennm 4:89ae38dc05a9 81 slcd.Home();
scohennm 4:89ae38dc05a9 82 slcd.clear();
scohennm 4:89ae38dc05a9 83 slcd.printf(lMess);
scohennm 4:89ae38dc05a9 84 }
scohennm 4:89ae38dc05a9 85
scohennm 4:89ae38dc05a9 86
scohennm 4:89ae38dc05a9 87 void allLEDsOFF(int numberOfLEDS) {
scohennm 4:89ae38dc05a9 88 int i;
scohennm 4:89ae38dc05a9 89 for (i=0;i<numberOfLEDS; i++){
scohennm 4:89ae38dc05a9 90 LEDs[i] = LEDOFF;
scohennm 4:89ae38dc05a9 91 }
scohennm 4:89ae38dc05a9 92
scohennm 4:89ae38dc05a9 93 }
scohennm 4:89ae38dc05a9 94
scohennm 4:89ae38dc05a9 95 void runLEDs(int numberOfLEDS) {
scohennm 4:89ae38dc05a9 96 int i;
scohennm 4:89ae38dc05a9 97 for (i=0;i<numberOfLEDS; i++){
scohennm 4:89ae38dc05a9 98 LEDs[i] = LEDON;
scohennm 4:89ae38dc05a9 99 wait(0.2);
scohennm 4:89ae38dc05a9 100 }
scohennm 4:89ae38dc05a9 101 allLEDsOFF(numberOfLEDS);
scohennm 4:89ae38dc05a9 102 }
scohennm 4:89ae38dc05a9 103
scohennm 4:89ae38dc05a9 104
scohennm 4:89ae38dc05a9 105
scohennm 4:89ae38dc05a9 106 int main() {
scohennm 4:89ae38dc05a9 107
scohennm 4:89ae38dc05a9 108 int RButtonState;
scohennm 4:89ae38dc05a9 109 int LButtonState;
scohennm 4:89ae38dc05a9 110 DigitalIn RtButton(PTC12);
scohennm 4:89ae38dc05a9 111 DigitalIn LftButton(PTC3);
scohennm 4:89ae38dc05a9 112 int displayState = XCOMPD;
scohennm 4:89ae38dc05a9 113 float xAcc = 0.0;
scohennm 4:89ae38dc05a9 114 float yAcc = 0.0;
scohennm 4:89ae38dc05a9 115 float zAcc = 0.0;
scohennm 4:89ae38dc05a9 116 float vector;
scohennm 4:89ae38dc05a9 117 float vMax = 0.0;
scohennm 4:89ae38dc05a9 118 float DisplayTime = DATADISPDWELL;
scohennm 4:89ae38dc05a9 119 float LEDDwell = BLINKTIME;
scohennm 4:89ae38dc05a9 120 int outState = false;
scohennm 4:89ae38dc05a9 121 char lcdData[LCDLEN]; //buffer needs places dor decimal pt and colon
scohennm 4:89ae38dc05a9 122 uint8_t regData = MAX_4G; // test value must change after G setting
scohennm 4:89ae38dc05a9 123
scohennm 4:89ae38dc05a9 124 #ifdef PRINTDBUG
scohennm 4:89ae38dc05a9 125 pc.printf(PROGNAME);
scohennm 4:89ae38dc05a9 126 #endif
scohennm 4:89ae38dc05a9 127 LCDMess(LCDNAME, BLINKTIME);
scohennm 4:89ae38dc05a9 128 // Initialze readings and sequence the LED's for dramtic effect.
scohennm 4:89ae38dc05a9 129 allLEDsOFF(NUMLEDS);
scohennm 4:89ae38dc05a9 130 blinkTimer.start();
scohennm 4:89ae38dc05a9 131 blinkTimer.reset();
scohennm 4:89ae38dc05a9 132 displayTimer.start();
scohennm 4:89ae38dc05a9 133 displayTimer.reset();
scohennm 4:89ae38dc05a9 134 dataTimer.start();
scohennm 4:89ae38dc05a9 135 dataTimer.reset();
scohennm 4:89ae38dc05a9 136 LCDMess(RSTARTMESS, BLINKTIME);
scohennm 5:6ec1dac45861 137 // Go into registers ofn Accelerometer, change sensitivity then test.
scohennm 5:6ec1dac45861 138 // http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8451Q.pdf
scohennm 5:6ec1dac45861 139 // See page 26
scohennm 5:6ec1dac45861 140 // Set 8 g max limit
scohennm 4:89ae38dc05a9 141 acc.setGLimit(MAX_8G); // For now set to 8g
scohennm 4:89ae38dc05a9 142 acc.readRegs(XYZ_DATA_CFG, &regData, 1);
scohennm 4:89ae38dc05a9 143 sprintf (lcdData,"%d",regData);
scohennm 4:89ae38dc05a9 144 LCDMess(lcdData,BLINKTIME);
scohennm 4:89ae38dc05a9 145 acc.readRegs(REG_WHO_AM_I, &regData, 1);
scohennm 4:89ae38dc05a9 146 sprintf (lcdData,"%d",regData);
scohennm 4:89ae38dc05a9 147 LCDMess(lcdData,BLINKTIME);
scohennm 4:89ae38dc05a9 148 // main loop forever
scohennm 4:89ae38dc05a9 149 while(true) {
scohennm 4:89ae38dc05a9 150
scohennm 4:89ae38dc05a9 151 // Handle user input for display selections
scohennm 4:89ae38dc05a9 152 RButtonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream
scohennm 4:89ae38dc05a9 153 if (RButtonState){
scohennm 4:89ae38dc05a9 154 vMax = 0.0; // Clear vMax
scohennm 4:89ae38dc05a9 155 LCDMess(RSTARTMESS, BLINKTIME);
scohennm 4:89ae38dc05a9 156 }
scohennm 4:89ae38dc05a9 157 LButtonState = !LftButton;
scohennm 4:89ae38dc05a9 158 if (LButtonState) { //Change data that is displayed
scohennm 4:89ae38dc05a9 159 displayState = (displayState + 1) % NUMSTATES;
scohennm 4:89ae38dc05a9 160 // Change to switch/case soon.
scohennm 4:89ae38dc05a9 161 LCDMess(displayTitles[displayState],BLINKTIME);
scohennm 4:89ae38dc05a9 162 }
scohennm 4:89ae38dc05a9 163
scohennm 4:89ae38dc05a9 164 // --------------------------------------------
scohennm 4:89ae38dc05a9 165 while (dataTimer.read() > DATATIME){
scohennm 4:89ae38dc05a9 166 //Get accelerometer data - tilt angles minus offset for zero mark.
scohennm 4:89ae38dc05a9 167 // No offset
scohennm 4:89ae38dc05a9 168 xAcc = abs(acc.getAccX());
scohennm 4:89ae38dc05a9 169 yAcc = abs(acc.getAccY());
scohennm 4:89ae38dc05a9 170 zAcc = abs(acc.getAccZ());
scohennm 4:89ae38dc05a9 171 // Calulate vector sum of x,y and z reading.
scohennm 4:89ae38dc05a9 172 vector = sqrt(pow(xAcc,2) + pow(zAcc,2));
scohennm 4:89ae38dc05a9 173 vector = zAcc;
scohennm 4:89ae38dc05a9 174 if (vector > vMax) {
scohennm 4:89ae38dc05a9 175 vMax = vector;
scohennm 4:89ae38dc05a9 176 }
scohennm 4:89ae38dc05a9 177 accaxisdata[XCOMPD] = xAcc;
scohennm 4:89ae38dc05a9 178 accaxisdata[YCOMPD] = yAcc;
scohennm 4:89ae38dc05a9 179 accaxisdata[ZCOMPD] = zAcc;
scohennm 4:89ae38dc05a9 180 accaxisdata[VMAXD] = vMax;
scohennm 4:89ae38dc05a9 181 dataTimer.reset();
scohennm 4:89ae38dc05a9 182 LEDDwell = 1.1 - vMax/MAXGS;
scohennm 4:89ae38dc05a9 183 }
scohennm 4:89ae38dc05a9 184 #ifdef PRINTDBUG
scohennm 4:89ae38dc05a9 185 pc.printf("vector = %f\r\n", xAcc);
scohennm 4:89ae38dc05a9 186 pc.printf("scaling = %f\r\n", scaleExpansion);
scohennm 4:89ae38dc05a9 187 pc.printf("RawTemp = %f\r\n", FDeg);
scohennm 4:89ae38dc05a9 188 #endif
scohennm 4:89ae38dc05a9 189 // Display the appropriate data on the LCD based upon what mode was chosen
scohennm 4:89ae38dc05a9 190 while (displayTimer.read() > DisplayTime){
scohennm 4:89ae38dc05a9 191 sprintf (lcdData,displayformats[displayState],accaxisdata[displayState]);
scohennm 4:89ae38dc05a9 192 LCDMessNoDwell(lcdData);
scohennm 4:89ae38dc05a9 193 displayTimer.reset();
scohennm 4:89ae38dc05a9 194 } // displaytimer
scohennm 4:89ae38dc05a9 195 // Wait then do the whole thing again.
scohennm 4:89ae38dc05a9 196 // Alive LEDs
scohennm 4:89ae38dc05a9 197 while(blinkTimer.read()> LEDDwell) {
scohennm 4:89ae38dc05a9 198 LEDs[0].write(outState);
scohennm 4:89ae38dc05a9 199 LEDs[1].write(!outState);
scohennm 4:89ae38dc05a9 200 outState = !outState;
scohennm 4:89ae38dc05a9 201 blinkTimer.reset();
scohennm 4:89ae38dc05a9 202 }
scohennm 4:89ae38dc05a9 203 }//forever loop
scohennm 4:89ae38dc05a9 204 }// main