Changed to access xAxis counts.

Dependencies:   MMA8451Q8a SLCD mbed

Fork of LCD_punch_mtr_8_v5 by Stanley Cohen

Committer:
scohennm
Date:
Fri Jan 30 22:26:06 2015 +0000
Revision:
7:6aa16a6fde70
Parent:
6:c2f31918cf77
Added correct storage classes and other details

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 //char displayformats[NUMSTATES][LCDLEN] = {"%4.3f","%4.3f","%4.3f","%4.3f"};
scohennm 4:89ae38dc05a9 77
scohennm 4:89ae38dc05a9 78 void LCDMess(char *lMess, float dWait){
scohennm 4:89ae38dc05a9 79 slcd.Home();
scohennm 4:89ae38dc05a9 80 slcd.clear();
scohennm 4:89ae38dc05a9 81 slcd.printf(lMess);
scohennm 4:89ae38dc05a9 82 wait(dWait);
scohennm 4:89ae38dc05a9 83 }
scohennm 4:89ae38dc05a9 84 void LCDMessNoDwell(char *lMess){
scohennm 4:89ae38dc05a9 85 slcd.Home();
scohennm 4:89ae38dc05a9 86 slcd.clear();
scohennm 4:89ae38dc05a9 87 slcd.printf(lMess);
scohennm 4:89ae38dc05a9 88 }
scohennm 4:89ae38dc05a9 89
scohennm 4:89ae38dc05a9 90
scohennm 4:89ae38dc05a9 91 void allLEDsOFF(int numberOfLEDS) {
scohennm 4:89ae38dc05a9 92 int i;
scohennm 4:89ae38dc05a9 93 for (i=0;i<numberOfLEDS; i++){
scohennm 4:89ae38dc05a9 94 LEDs[i] = LEDOFF;
scohennm 4:89ae38dc05a9 95 }
scohennm 4:89ae38dc05a9 96
scohennm 4:89ae38dc05a9 97 }
scohennm 4:89ae38dc05a9 98
scohennm 4:89ae38dc05a9 99 void runLEDs(int numberOfLEDS) {
scohennm 4:89ae38dc05a9 100 int i;
scohennm 4:89ae38dc05a9 101 for (i=0;i<numberOfLEDS; i++){
scohennm 4:89ae38dc05a9 102 LEDs[i] = LEDON;
scohennm 4:89ae38dc05a9 103 wait(0.2);
scohennm 4:89ae38dc05a9 104 }
scohennm 4:89ae38dc05a9 105 allLEDsOFF(numberOfLEDS);
scohennm 4:89ae38dc05a9 106 }
scohennm 4:89ae38dc05a9 107
scohennm 4:89ae38dc05a9 108
scohennm 4:89ae38dc05a9 109
scohennm 4:89ae38dc05a9 110 int main() {
scohennm 4:89ae38dc05a9 111
scohennm 4:89ae38dc05a9 112 int RButtonState;
scohennm 4:89ae38dc05a9 113 int LButtonState;
scohennm 4:89ae38dc05a9 114 DigitalIn RtButton(PTC12);
scohennm 4:89ae38dc05a9 115 DigitalIn LftButton(PTC3);
scohennm 4:89ae38dc05a9 116 int displayState = XCOMPD;
scohennm 4:89ae38dc05a9 117 float xAcc = 0.0;
scohennm 4:89ae38dc05a9 118 float yAcc = 0.0;
scohennm 4:89ae38dc05a9 119 float zAcc = 0.0;
scohennm 4:89ae38dc05a9 120 float vector;
scohennm 4:89ae38dc05a9 121 float vMax = 0.0;
scohennm 4:89ae38dc05a9 122 float DisplayTime = DATADISPDWELL;
scohennm 4:89ae38dc05a9 123 float LEDDwell = BLINKTIME;
scohennm 4:89ae38dc05a9 124 int outState = false;
scohennm 4:89ae38dc05a9 125 char lcdData[LCDLEN]; //buffer needs places dor decimal pt and colon
scohennm 4:89ae38dc05a9 126 uint8_t regData = MAX_4G; // test value must change after G setting
scohennm 7:6aa16a6fde70 127 int16_t xCounts;
scohennm 4:89ae38dc05a9 128
scohennm 4:89ae38dc05a9 129 #ifdef PRINTDBUG
scohennm 4:89ae38dc05a9 130 pc.printf(PROGNAME);
scohennm 4:89ae38dc05a9 131 #endif
scohennm 4:89ae38dc05a9 132 LCDMess(LCDNAME, BLINKTIME);
scohennm 4:89ae38dc05a9 133 // Initialze readings and sequence the LED's for dramtic effect.
scohennm 4:89ae38dc05a9 134 allLEDsOFF(NUMLEDS);
scohennm 4:89ae38dc05a9 135 blinkTimer.start();
scohennm 4:89ae38dc05a9 136 blinkTimer.reset();
scohennm 4:89ae38dc05a9 137 displayTimer.start();
scohennm 4:89ae38dc05a9 138 displayTimer.reset();
scohennm 4:89ae38dc05a9 139 dataTimer.start();
scohennm 4:89ae38dc05a9 140 dataTimer.reset();
scohennm 4:89ae38dc05a9 141 LCDMess(RSTARTMESS, BLINKTIME);
scohennm 5:6ec1dac45861 142 // Go into registers ofn Accelerometer, change sensitivity then test.
scohennm 5:6ec1dac45861 143 // http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8451Q.pdf
scohennm 5:6ec1dac45861 144 // See page 26
scohennm 7:6aa16a6fde70 145 // Set 2 g max limit ****************** Note limits
scohennm 7:6aa16a6fde70 146 acc.setGLimit(MAX_2G); // For now set to 2g
scohennm 4:89ae38dc05a9 147 acc.readRegs(XYZ_DATA_CFG, &regData, 1);
scohennm 4:89ae38dc05a9 148 sprintf (lcdData,"%d",regData);
scohennm 4:89ae38dc05a9 149 LCDMess(lcdData,BLINKTIME);
scohennm 4:89ae38dc05a9 150 acc.readRegs(REG_WHO_AM_I, &regData, 1);
scohennm 4:89ae38dc05a9 151 sprintf (lcdData,"%d",regData);
scohennm 4:89ae38dc05a9 152 LCDMess(lcdData,BLINKTIME);
scohennm 4:89ae38dc05a9 153 // main loop forever
scohennm 4:89ae38dc05a9 154 while(true) {
scohennm 4:89ae38dc05a9 155
scohennm 4:89ae38dc05a9 156 // Handle user input for display selections
scohennm 4:89ae38dc05a9 157 RButtonState = !RtButton; // button is pulled up so false is when button is pushed it's inverted to avoid confusion downstream
scohennm 4:89ae38dc05a9 158 if (RButtonState){
scohennm 4:89ae38dc05a9 159 vMax = 0.0; // Clear vMax
scohennm 4:89ae38dc05a9 160 LCDMess(RSTARTMESS, BLINKTIME);
scohennm 4:89ae38dc05a9 161 }
scohennm 4:89ae38dc05a9 162 LButtonState = !LftButton;
scohennm 4:89ae38dc05a9 163 if (LButtonState) { //Change data that is displayed
scohennm 4:89ae38dc05a9 164 displayState = (displayState + 1) % NUMSTATES;
scohennm 4:89ae38dc05a9 165 // Change to switch/case soon.
scohennm 4:89ae38dc05a9 166 LCDMess(displayTitles[displayState],BLINKTIME);
scohennm 4:89ae38dc05a9 167 }
scohennm 4:89ae38dc05a9 168
scohennm 4:89ae38dc05a9 169 // --------------------------------------------
scohennm 4:89ae38dc05a9 170 while (dataTimer.read() > DATATIME){
scohennm 7:6aa16a6fde70 171
scohennm 4:89ae38dc05a9 172 // No offset
scohennm 4:89ae38dc05a9 173 xAcc = abs(acc.getAccX());
scohennm 6:c2f31918cf77 174 xCounts = acc.getAccAxis(REG_OUT_X_MSB);
scohennm 4:89ae38dc05a9 175 yAcc = abs(acc.getAccY());
scohennm 4:89ae38dc05a9 176 zAcc = abs(acc.getAccZ());
scohennm 4:89ae38dc05a9 177 // Calulate vector sum of x,y and z reading.
scohennm 4:89ae38dc05a9 178 vector = sqrt(pow(xAcc,2) + pow(zAcc,2));
scohennm 4:89ae38dc05a9 179 vector = zAcc;
scohennm 4:89ae38dc05a9 180 if (vector > vMax) {
scohennm 4:89ae38dc05a9 181 vMax = vector;
scohennm 4:89ae38dc05a9 182 }
scohennm 7:6aa16a6fde70 183 //Prepare data for LCD display
scohennm 7:6aa16a6fde70 184 accaxisdata[XCOMPD] = abs((float)xCounts/COUNTSCALE); // scalling is set to 1 at this point
scohennm 4:89ae38dc05a9 185 accaxisdata[YCOMPD] = yAcc;
scohennm 4:89ae38dc05a9 186 accaxisdata[ZCOMPD] = zAcc;
scohennm 4:89ae38dc05a9 187 accaxisdata[VMAXD] = vMax;
scohennm 4:89ae38dc05a9 188 dataTimer.reset();
scohennm 4:89ae38dc05a9 189 LEDDwell = 1.1 - vMax/MAXGS;
scohennm 4:89ae38dc05a9 190 }
scohennm 7:6aa16a6fde70 191
scohennm 4:89ae38dc05a9 192 // Display the appropriate data on the LCD based upon what mode was chosen
scohennm 4:89ae38dc05a9 193 while (displayTimer.read() > DisplayTime){
scohennm 4:89ae38dc05a9 194 sprintf (lcdData,displayformats[displayState],accaxisdata[displayState]);
scohennm 4:89ae38dc05a9 195 LCDMessNoDwell(lcdData);
scohennm 4:89ae38dc05a9 196 displayTimer.reset();
scohennm 4:89ae38dc05a9 197 } // displaytimer
scohennm 4:89ae38dc05a9 198 // Wait then do the whole thing again.
scohennm 4:89ae38dc05a9 199 // Alive LEDs
scohennm 4:89ae38dc05a9 200 while(blinkTimer.read()> LEDDwell) {
scohennm 4:89ae38dc05a9 201 LEDs[0].write(outState);
scohennm 4:89ae38dc05a9 202 LEDs[1].write(!outState);
scohennm 4:89ae38dc05a9 203 outState = !outState;
scohennm 4:89ae38dc05a9 204 blinkTimer.reset();
scohennm 4:89ae38dc05a9 205 }
scohennm 4:89ae38dc05a9 206 }//forever loop
scohennm 4:89ae38dc05a9 207 }// main