Hw

Dependencies:   MMA8451Q SLCD mbed

Fork of LCD_ACC_46_v4-2g by Stanley Cohen

Committer:
kennylujan42
Date:
Wed Feb 22 09:50:25 2017 +0000
Revision:
5:98f02fe1d957
Parent:
4:41920817d234
HW;

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