John Halfpenny / Mbed 2 deprecated Accellerometer_project

Dependencies:   USBDevice mbed DipCortex-USB-EEProm

Committer:
johnhalfpenny
Date:
Fri Sep 11 10:37:33 2015 +0000
Revision:
9:f37125673b91
Parent:
8:9a3d2d28e7e5
Child:
10:ba3a28345f76
LED flashes when logging, if BUTTON held at start auto logging starts, EEPROM library loaded

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnhalfpenny 5:e1eb6bdf10f3 1 //
johnhalfpenny 5:e1eb6bdf10f3 2 // Accellerometer project
johnhalfpenny 5:e1eb6bdf10f3 3 //
johnhalfpenny 5:e1eb6bdf10f3 4 // Laurence Halfpenny
johnhalfpenny 5:e1eb6bdf10f3 5 // August 2015
SolderSplashLabs 0:0bce3a738bcb 6
johnhalfpenny 9:f37125673b91 7 #include <stdint.h>
johnhalfpenny 9:f37125673b91 8 #include <string.h>
johnhalfpenny 9:f37125673b91 9 #include "mbed.h" // Needed for MBED library functions
johnhalfpenny 9:f37125673b91 10 #include "main.h" // Needed for definitions of things that can be changed
johnhalfpenny 9:f37125673b91 11 #include "USBSerial.h" // Needed for USB serial port
johnhalfpenny 9:f37125673b91 12 #include "DipCortex-EEprom.h"
johnhalfpenny 9:f37125673b91 13 #include "LPC13Uxx.h"
SolderSplashLabs 0:0bce3a738bcb 14
SolderSplashLabs 0:0bce3a738bcb 15 USBSerial pc; // USB CDC serial port
SolderSplashLabs 0:0bce3a738bcb 16
johnhalfpenny 5:e1eb6bdf10f3 17 // Define the X, Y & Z analogue inputs on ADCs
johnhalfpenny 5:e1eb6bdf10f3 18 AnalogIn Z_in(P0_11); // Z accelleration voltage
johnhalfpenny 5:e1eb6bdf10f3 19 AnalogIn Y_in(P0_12); // Y accelleration voltage
johnhalfpenny 5:e1eb6bdf10f3 20 AnalogIn X_in(P0_13); // X accelleration voltage
SolderSplashLabs 4:ce953c80c5b3 21
johnhalfpenny 9:f37125673b91 22 // On-board LED
johnhalfpenny 9:f37125673b91 23 DigitalOut LED(P0_1);
johnhalfpenny 9:f37125673b91 24
johnhalfpenny 9:f37125673b91 25 // Push button
johnhalfpenny 9:f37125673b91 26 // Logic FALSE when pushed, logic TRUE when no pushed
johnhalfpenny 9:f37125673b91 27 DigitalIn BUTTON(P0_16);
johnhalfpenny 9:f37125673b91 28
johnhalfpenny 5:e1eb6bdf10f3 29 // Acceleration values
johnhalfpenny 5:e1eb6bdf10f3 30 int Z_accelleration=0;
johnhalfpenny 5:e1eb6bdf10f3 31 int Y_accelleration=0;
johnhalfpenny 5:e1eb6bdf10f3 32 int X_accelleration=0;
SolderSplashLabs 0:0bce3a738bcb 33
lh121438 8:9a3d2d28e7e5 34 // Storage arrays for X,Y,Z
johnhalfpenny 9:f37125673b91 35 // Pack them to save RAM space
johnhalfpenny 9:f37125673b91 36 #pragma pack(push,1)
lh121438 8:9a3d2d28e7e5 37 uint16_t Z_store[MAXSAMPLES]={};
lh121438 8:9a3d2d28e7e5 38 uint16_t Y_store[MAXSAMPLES]={};
lh121438 8:9a3d2d28e7e5 39 uint16_t X_store[MAXSAMPLES]={};
johnhalfpenny 9:f37125673b91 40 #pragma pack(pop)
johnhalfpenny 9:f37125673b91 41
lh121438 8:9a3d2d28e7e5 42 int Sample_index=0;
lh121438 8:9a3d2d28e7e5 43 int Last_sample=0;
johnhalfpenny 9:f37125673b91 44 int EEPROM_index=0;
lh121438 8:9a3d2d28e7e5 45
johnhalfpenny 5:e1eb6bdf10f3 46 // Control variables
johnhalfpenny 5:e1eb6bdf10f3 47 bool monitoring=FALSE; // Controls whether monitoring is running or stopped
johnhalfpenny 5:e1eb6bdf10f3 48
johnhalfpenny 5:e1eb6bdf10f3 49 // Ticker for monitoring
johnhalfpenny 5:e1eb6bdf10f3 50 Ticker monitor_tick;
SolderSplashLabs 0:0bce3a738bcb 51
SolderSplashLabs 0:0bce3a738bcb 52 // ------------------------------------------------------------------------------------------------------------
johnhalfpenny 5:e1eb6bdf10f3 53 // Wait for a character to be typed
SolderSplashLabs 0:0bce3a738bcb 54 // ------------------------------------------------------------------------------------------------------------
SolderSplashLabs 0:0bce3a738bcb 55 char WaitForSerialCommand ( void )
SolderSplashLabs 0:0bce3a738bcb 56 {
SolderSplashLabs 0:0bce3a738bcb 57 char charIn = 0;
SolderSplashLabs 0:0bce3a738bcb 58 char prevCharIn;
SolderSplashLabs 0:0bce3a738bcb 59
johnhalfpenny 5:e1eb6bdf10f3 60 pc.printf("Enter command: ");
SolderSplashLabs 0:0bce3a738bcb 61
johnhalfpenny 5:e1eb6bdf10f3 62 while (TRUE)
SolderSplashLabs 0:0bce3a738bcb 63 {
SolderSplashLabs 0:0bce3a738bcb 64 prevCharIn = charIn;
SolderSplashLabs 0:0bce3a738bcb 65 charIn = pc.getc();
SolderSplashLabs 0:0bce3a738bcb 66
SolderSplashLabs 0:0bce3a738bcb 67 pc.printf("%c", charIn);
SolderSplashLabs 0:0bce3a738bcb 68 if ((charIn == '\n') || (charIn == '\r'))
SolderSplashLabs 0:0bce3a738bcb 69 {
SolderSplashLabs 0:0bce3a738bcb 70 break;
SolderSplashLabs 0:0bce3a738bcb 71 }
SolderSplashLabs 0:0bce3a738bcb 72 }
SolderSplashLabs 0:0bce3a738bcb 73 return ( prevCharIn );
SolderSplashLabs 0:0bce3a738bcb 74 }
SolderSplashLabs 0:0bce3a738bcb 75
johnhalfpenny 9:f37125673b91 76 // ------------------------------------------------------------------------------------------------------------
johnhalfpenny 9:f37125673b91 77 // Read and Write the EEPROM
johnhalfpenny 9:f37125673b91 78 // ------------------------------------------------------------------------------------------------------------
johnhalfpenny 9:f37125673b91 79
SolderSplashLabs 0:0bce3a738bcb 80
SolderSplashLabs 0:0bce3a738bcb 81 // ------------------------------------------------------------------------------------------------------------
johnhalfpenny 5:e1eb6bdf10f3 82 // Initialise the LPC1347 processor
SolderSplashLabs 0:0bce3a738bcb 83 // ------------------------------------------------------------------------------------------------------------
SolderSplashLabs 0:0bce3a738bcb 84 void init()
SolderSplashLabs 0:0bce3a738bcb 85 {
SolderSplashLabs 0:0bce3a738bcb 86 NVIC_SetPriority(SSP1_IRQn, 0x0);
SolderSplashLabs 0:0bce3a738bcb 87 NVIC_SetPriority(PIN_INT0_IRQn, 0x1);
SolderSplashLabs 0:0bce3a738bcb 88
SolderSplashLabs 0:0bce3a738bcb 89 // SysTick set to lower priority than Wi-Fi SPI bus interrupt
SolderSplashLabs 0:0bce3a738bcb 90 NVIC_SetPriority(SysTick_IRQn, 0x2);
SolderSplashLabs 3:15828ac052f1 91
SolderSplashLabs 3:15828ac052f1 92
SolderSplashLabs 3:15828ac052f1 93 // Enable RAM1
SolderSplashLabs 3:15828ac052f1 94 LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26);
johnhalfpenny 9:f37125673b91 95
johnhalfpenny 9:f37125673b91 96 // Clear the LED
johnhalfpenny 9:f37125673b91 97 LED=FALSE;
johnhalfpenny 9:f37125673b91 98
johnhalfpenny 9:f37125673b91 99 // Pull the BUTTON pin up to logic high
johnhalfpenny 9:f37125673b91 100 BUTTON.mode(PullUp);
SolderSplashLabs 0:0bce3a738bcb 101 }
SolderSplashLabs 0:0bce3a738bcb 102
SolderSplashLabs 0:0bce3a738bcb 103 // ------------------------------------------------------------------------------------------------------------
johnhalfpenny 5:e1eb6bdf10f3 104 // The monitoring software, triggered by the monitoring ticker
johnhalfpenny 5:e1eb6bdf10f3 105 // ------------------------------------------------------------------------------------------------------------
johnhalfpenny 5:e1eb6bdf10f3 106 void monitor_accelleration(void)
johnhalfpenny 5:e1eb6bdf10f3 107 {
johnhalfpenny 9:f37125673b91 108 LED = !LED; // Toggle the LED each sample
johnhalfpenny 9:f37125673b91 109
johnhalfpenny 5:e1eb6bdf10f3 110 X_accelleration=X_in.read_u16(); // Read and print X accelleration
lh121438 8:9a3d2d28e7e5 111 X_store[Sample_index]=X_accelleration;
johnhalfpenny 5:e1eb6bdf10f3 112 X_accelleration=(X_accelleration-(0xFFFF/2)); // Convert to change in accelleration
johnhalfpenny 5:e1eb6bdf10f3 113 X_accelleration=X_accelleration/16;
johnhalfpenny 9:f37125673b91 114 //pc.printf("X: %d ",X_accelleration);
johnhalfpenny 5:e1eb6bdf10f3 115
johnhalfpenny 5:e1eb6bdf10f3 116 Y_accelleration=Y_in.read_u16(); // Read and print Y accelleration
lh121438 8:9a3d2d28e7e5 117 Y_store[Sample_index]=Y_accelleration;
johnhalfpenny 5:e1eb6bdf10f3 118 Y_accelleration=(Y_accelleration-(0xFFFF/2)); // Convert to change in accelleration
johnhalfpenny 5:e1eb6bdf10f3 119 Y_accelleration=Y_accelleration/16;
johnhalfpenny 9:f37125673b91 120 //pc.printf("Y: %d ",Y_accelleration);
johnhalfpenny 5:e1eb6bdf10f3 121
johnhalfpenny 5:e1eb6bdf10f3 122 Z_accelleration=Z_in.read_u16(); // Read and print Z accelleration
lh121438 8:9a3d2d28e7e5 123 Z_store[Sample_index]=Z_accelleration;
johnhalfpenny 5:e1eb6bdf10f3 124 Z_accelleration=(Z_accelleration-(0xFFFF/2)); // Convert to change in accelleration
johnhalfpenny 5:e1eb6bdf10f3 125 Z_accelleration=Z_accelleration/16;
johnhalfpenny 9:f37125673b91 126 //pc.printf("Z: %d \r\n",Z_accelleration);
lh121438 8:9a3d2d28e7e5 127
johnhalfpenny 9:f37125673b91 128 //pc.printf("Sample index: %d \r\n",Sample_index);
lh121438 8:9a3d2d28e7e5 129 Sample_index++;
lh121438 8:9a3d2d28e7e5 130
lh121438 8:9a3d2d28e7e5 131 if (Sample_index>=MAXSAMPLES)
lh121438 8:9a3d2d28e7e5 132 {
johnhalfpenny 9:f37125673b91 133 //pc.printf("Stopping - full!\r\n");
lh121438 8:9a3d2d28e7e5 134 monitoring=FALSE;
lh121438 8:9a3d2d28e7e5 135 monitor_tick.detach();
lh121438 8:9a3d2d28e7e5 136 Last_sample=Sample_index;
lh121438 8:9a3d2d28e7e5 137 }
johnhalfpenny 5:e1eb6bdf10f3 138 }
johnhalfpenny 5:e1eb6bdf10f3 139
johnhalfpenny 5:e1eb6bdf10f3 140 // ------------------------------------------------------------------------------------------------------------
johnhalfpenny 5:e1eb6bdf10f3 141 // main loop
SolderSplashLabs 0:0bce3a738bcb 142 // ------------------------------------------------------------------------------------------------------------
SolderSplashLabs 0:0bce3a738bcb 143 int main( void )
SolderSplashLabs 0:0bce3a738bcb 144 {
lh121438 8:9a3d2d28e7e5 145 int i;
johnhalfpenny 9:f37125673b91 146 // Initalise the LPC1347 & WiFiDIPCORTEX board
SolderSplashLabs 0:0bce3a738bcb 147 init();
SolderSplashLabs 0:0bce3a738bcb 148
johnhalfpenny 5:e1eb6bdf10f3 149 // Wait for terminal program to start
lh121438 6:c3a33d04d731 150 wait(WAITATSTART);
SolderSplashLabs 0:0bce3a738bcb 151
johnhalfpenny 5:e1eb6bdf10f3 152 // Show the start banner
johnhalfpenny 5:e1eb6bdf10f3 153 pc.printf("\r\n");
johnhalfpenny 5:e1eb6bdf10f3 154 pc.printf("+-------------------------------------------+\r\n");
johnhalfpenny 5:e1eb6bdf10f3 155 pc.printf("| 3-axis accelleration measurer |\r\n");
johnhalfpenny 5:e1eb6bdf10f3 156 pc.printf("| Laurence Halfpenny |\r\n");
lh121438 8:9a3d2d28e7e5 157 pc.printf("| Version 1.2 |\r\n");
johnhalfpenny 5:e1eb6bdf10f3 158 pc.printf("+-------------------------------------------+\r\n");
johnhalfpenny 5:e1eb6bdf10f3 159
johnhalfpenny 9:f37125673b91 160 // If the BUTTON is pushed (logic FALSE) start logging otherwise go into keyboard menu
johnhalfpenny 9:f37125673b91 161 if (BUTTON==FALSE)
johnhalfpenny 5:e1eb6bdf10f3 162 {
johnhalfpenny 9:f37125673b91 163 // Start the monitoring
johnhalfpenny 9:f37125673b91 164 Sample_index=0;
johnhalfpenny 9:f37125673b91 165 monitoring=TRUE;
johnhalfpenny 9:f37125673b91 166 monitor_tick.attach(&monitor_accelleration,MONITORINTERVAL);
johnhalfpenny 9:f37125673b91 167 // Wait for the monitoring to finish and then save it to EEPROM
johnhalfpenny 9:f37125673b91 168 while (monitoring==TRUE)
johnhalfpenny 9:f37125673b91 169 {
johnhalfpenny 9:f37125673b91 170 // Just wait for monitoring to finish
johnhalfpenny 9:f37125673b91 171 }
johnhalfpenny 9:f37125673b91 172 // Now copy the storage arrays to EEPROM
johnhalfpenny 9:f37125673b91 173
johnhalfpenny 9:f37125673b91 174 // Now wait for ever
johnhalfpenny 9:f37125673b91 175 while (TRUE)
johnhalfpenny 5:e1eb6bdf10f3 176 {
johnhalfpenny 9:f37125673b91 177 }
johnhalfpenny 9:f37125673b91 178 }
johnhalfpenny 9:f37125673b91 179 else
johnhalfpenny 9:f37125673b91 180 {
johnhalfpenny 9:f37125673b91 181 // Forever check for commands from the keyboard
johnhalfpenny 9:f37125673b91 182 while (TRUE)
johnhalfpenny 9:f37125673b91 183 {
johnhalfpenny 9:f37125673b91 184 pc.printf("+-------------------------------------------+\r\n");
johnhalfpenny 9:f37125673b91 185 pc.printf("| V 1.2 MENU: |\r\n");
johnhalfpenny 9:f37125673b91 186 pc.printf("| 1: Start monitoring |\r\n");
johnhalfpenny 9:f37125673b91 187 pc.printf("| 2: Stop monitoring |\r\n");
johnhalfpenny 9:f37125673b91 188 pc.printf("| 3: Stop / Dump values |\r\n");
johnhalfpenny 9:f37125673b91 189 pc.printf("+-------------------------------------------+\r\n");
johnhalfpenny 9:f37125673b91 190
johnhalfpenny 9:f37125673b91 191 switch(WaitForSerialCommand())
johnhalfpenny 9:f37125673b91 192 {
johnhalfpenny 9:f37125673b91 193 case '1':
johnhalfpenny 9:f37125673b91 194 pc.printf("Starting monitor\r\n");
johnhalfpenny 9:f37125673b91 195 Sample_index=0;
johnhalfpenny 9:f37125673b91 196 monitoring=TRUE;
johnhalfpenny 9:f37125673b91 197 monitor_tick.attach(&monitor_accelleration,MONITORINTERVAL);
johnhalfpenny 9:f37125673b91 198 break;
johnhalfpenny 9:f37125673b91 199
johnhalfpenny 9:f37125673b91 200 case '2':
lh121438 8:9a3d2d28e7e5 201 pc.printf("Stopping monitoring\r\n");
lh121438 8:9a3d2d28e7e5 202 monitoring=FALSE;
johnhalfpenny 9:f37125673b91 203 LED=FALSE;
lh121438 8:9a3d2d28e7e5 204 monitor_tick.detach();
johnhalfpenny 9:f37125673b91 205 Last_sample=Sample_index;
johnhalfpenny 9:f37125673b91 206 break;
lh121438 8:9a3d2d28e7e5 207
johnhalfpenny 9:f37125673b91 208 case '3':
johnhalfpenny 9:f37125673b91 209 if (monitoring==TRUE)
lh121438 8:9a3d2d28e7e5 210 {
johnhalfpenny 9:f37125673b91 211 pc.printf("Stopping monitoring\r\n");
johnhalfpenny 9:f37125673b91 212 monitoring=FALSE;
johnhalfpenny 9:f37125673b91 213 LED=FALSE;
johnhalfpenny 9:f37125673b91 214 monitor_tick.detach();
johnhalfpenny 9:f37125673b91 215 Last_sample=Sample_index;
lh121438 8:9a3d2d28e7e5 216 }
johnhalfpenny 9:f37125673b91 217
johnhalfpenny 9:f37125673b91 218 if (Last_sample>0)
johnhalfpenny 9:f37125673b91 219 {
johnhalfpenny 9:f37125673b91 220 for (i=0; i<Last_sample; i++)
johnhalfpenny 9:f37125673b91 221 {
johnhalfpenny 9:f37125673b91 222 pc.printf("No.: %d ",i);
johnhalfpenny 9:f37125673b91 223 pc.printf("X: %d ",X_store[i]);
johnhalfpenny 9:f37125673b91 224 pc.printf("Y: %d ",Y_store[i]);
johnhalfpenny 9:f37125673b91 225 pc.printf("Z: %d \r\n",Z_store[i]);
johnhalfpenny 9:f37125673b91 226 }
johnhalfpenny 9:f37125673b91 227 }
johnhalfpenny 9:f37125673b91 228 break;
johnhalfpenny 9:f37125673b91 229
johnhalfpenny 9:f37125673b91 230 default:
johnhalfpenny 9:f37125673b91 231 pc.printf("Invalid command\r\n");
johnhalfpenny 9:f37125673b91 232 break;
johnhalfpenny 9:f37125673b91 233 }
johnhalfpenny 5:e1eb6bdf10f3 234 }
johnhalfpenny 5:e1eb6bdf10f3 235 }
johnhalfpenny 5:e1eb6bdf10f3 236
SolderSplashLabs 0:0bce3a738bcb 237 }