Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: USBDevice mbed DipCortex-USB-EEProm
main.cpp@7:930020a7dc9e, 2015-09-03 (annotated)
- Committer:
- lh121438
- Date:
- Thu Sep 03 16:03:07 2015 +0000
- Revision:
- 7:930020a7dc9e
- Parent:
- 6:c3a33d04d731
- Child:
- 8:9a3d2d28e7e5
Added version numbers
Who changed what in which revision?
User | Revision | Line number | New 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 | |
SolderSplashLabs | 0:0bce3a738bcb | 7 | #include "mbed.h" |
SolderSplashLabs | 0:0bce3a738bcb | 8 | #include "main.h" |
SolderSplashLabs | 0:0bce3a738bcb | 9 | #include "USBSerial.h" |
SolderSplashLabs | 0:0bce3a738bcb | 10 | |
SolderSplashLabs | 0:0bce3a738bcb | 11 | USBSerial pc; // USB CDC serial port |
SolderSplashLabs | 0:0bce3a738bcb | 12 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 13 | // Define the X, Y & Z analogue inputs on ADCs |
johnhalfpenny | 5:e1eb6bdf10f3 | 14 | AnalogIn Z_in(P0_11); // Z accelleration voltage |
johnhalfpenny | 5:e1eb6bdf10f3 | 15 | AnalogIn Y_in(P0_12); // Y accelleration voltage |
johnhalfpenny | 5:e1eb6bdf10f3 | 16 | AnalogIn X_in(P0_13); // X accelleration voltage |
SolderSplashLabs | 4:ce953c80c5b3 | 17 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 18 | // Acceleration values |
johnhalfpenny | 5:e1eb6bdf10f3 | 19 | int Z_accelleration=0; |
johnhalfpenny | 5:e1eb6bdf10f3 | 20 | int Y_accelleration=0; |
johnhalfpenny | 5:e1eb6bdf10f3 | 21 | int X_accelleration=0; |
SolderSplashLabs | 0:0bce3a738bcb | 22 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 23 | // Control variables |
johnhalfpenny | 5:e1eb6bdf10f3 | 24 | bool monitoring=FALSE; // Controls whether monitoring is running or stopped |
johnhalfpenny | 5:e1eb6bdf10f3 | 25 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 26 | // Ticker for monitoring |
johnhalfpenny | 5:e1eb6bdf10f3 | 27 | Ticker monitor_tick; |
SolderSplashLabs | 0:0bce3a738bcb | 28 | |
SolderSplashLabs | 0:0bce3a738bcb | 29 | // ------------------------------------------------------------------------------------------------------------ |
johnhalfpenny | 5:e1eb6bdf10f3 | 30 | // Wait for a character to be typed |
SolderSplashLabs | 0:0bce3a738bcb | 31 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 0:0bce3a738bcb | 32 | char WaitForSerialCommand ( void ) |
SolderSplashLabs | 0:0bce3a738bcb | 33 | { |
SolderSplashLabs | 0:0bce3a738bcb | 34 | char charIn = 0; |
SolderSplashLabs | 0:0bce3a738bcb | 35 | char prevCharIn; |
SolderSplashLabs | 0:0bce3a738bcb | 36 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 37 | pc.printf("Enter command: "); |
SolderSplashLabs | 0:0bce3a738bcb | 38 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 39 | while (TRUE) |
SolderSplashLabs | 0:0bce3a738bcb | 40 | { |
SolderSplashLabs | 0:0bce3a738bcb | 41 | prevCharIn = charIn; |
SolderSplashLabs | 0:0bce3a738bcb | 42 | charIn = pc.getc(); |
SolderSplashLabs | 0:0bce3a738bcb | 43 | |
SolderSplashLabs | 0:0bce3a738bcb | 44 | pc.printf("%c", charIn); |
SolderSplashLabs | 0:0bce3a738bcb | 45 | if ((charIn == '\n') || (charIn == '\r')) |
SolderSplashLabs | 0:0bce3a738bcb | 46 | { |
SolderSplashLabs | 0:0bce3a738bcb | 47 | break; |
SolderSplashLabs | 0:0bce3a738bcb | 48 | } |
SolderSplashLabs | 0:0bce3a738bcb | 49 | } |
SolderSplashLabs | 0:0bce3a738bcb | 50 | return ( prevCharIn ); |
SolderSplashLabs | 0:0bce3a738bcb | 51 | } |
SolderSplashLabs | 0:0bce3a738bcb | 52 | |
SolderSplashLabs | 0:0bce3a738bcb | 53 | |
SolderSplashLabs | 0:0bce3a738bcb | 54 | // ------------------------------------------------------------------------------------------------------------ |
johnhalfpenny | 5:e1eb6bdf10f3 | 55 | // Initialise the LPC1347 processor |
SolderSplashLabs | 0:0bce3a738bcb | 56 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 0:0bce3a738bcb | 57 | void init() |
SolderSplashLabs | 0:0bce3a738bcb | 58 | { |
SolderSplashLabs | 0:0bce3a738bcb | 59 | NVIC_SetPriority(SSP1_IRQn, 0x0); |
SolderSplashLabs | 0:0bce3a738bcb | 60 | NVIC_SetPriority(PIN_INT0_IRQn, 0x1); |
SolderSplashLabs | 0:0bce3a738bcb | 61 | |
SolderSplashLabs | 0:0bce3a738bcb | 62 | // SysTick set to lower priority than Wi-Fi SPI bus interrupt |
SolderSplashLabs | 0:0bce3a738bcb | 63 | NVIC_SetPriority(SysTick_IRQn, 0x2); |
SolderSplashLabs | 3:15828ac052f1 | 64 | |
SolderSplashLabs | 3:15828ac052f1 | 65 | |
SolderSplashLabs | 3:15828ac052f1 | 66 | // Enable RAM1 |
SolderSplashLabs | 3:15828ac052f1 | 67 | LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26); |
SolderSplashLabs | 0:0bce3a738bcb | 68 | } |
SolderSplashLabs | 0:0bce3a738bcb | 69 | |
SolderSplashLabs | 0:0bce3a738bcb | 70 | // ------------------------------------------------------------------------------------------------------------ |
johnhalfpenny | 5:e1eb6bdf10f3 | 71 | // The monitoring software, triggered by the monitoring ticker |
johnhalfpenny | 5:e1eb6bdf10f3 | 72 | // ------------------------------------------------------------------------------------------------------------ |
johnhalfpenny | 5:e1eb6bdf10f3 | 73 | void monitor_accelleration(void) |
johnhalfpenny | 5:e1eb6bdf10f3 | 74 | { |
johnhalfpenny | 5:e1eb6bdf10f3 | 75 | X_accelleration=X_in.read_u16(); // Read and print X accelleration |
johnhalfpenny | 5:e1eb6bdf10f3 | 76 | X_accelleration=(X_accelleration-(0xFFFF/2)); // Convert to change in accelleration |
johnhalfpenny | 5:e1eb6bdf10f3 | 77 | X_accelleration=X_accelleration/16; |
johnhalfpenny | 5:e1eb6bdf10f3 | 78 | pc.printf("X: %d ",X_accelleration); |
johnhalfpenny | 5:e1eb6bdf10f3 | 79 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 80 | Y_accelleration=Y_in.read_u16(); // Read and print Y accelleration |
johnhalfpenny | 5:e1eb6bdf10f3 | 81 | Y_accelleration=(Y_accelleration-(0xFFFF/2)); // Convert to change in accelleration |
johnhalfpenny | 5:e1eb6bdf10f3 | 82 | Y_accelleration=Y_accelleration/16; |
johnhalfpenny | 5:e1eb6bdf10f3 | 83 | pc.printf("Y: %d ",Y_accelleration); |
johnhalfpenny | 5:e1eb6bdf10f3 | 84 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 85 | Z_accelleration=Z_in.read_u16(); // Read and print Z accelleration |
johnhalfpenny | 5:e1eb6bdf10f3 | 86 | Z_accelleration=(Z_accelleration-(0xFFFF/2)); // Convert to change in accelleration |
johnhalfpenny | 5:e1eb6bdf10f3 | 87 | Z_accelleration=Z_accelleration/16; |
johnhalfpenny | 5:e1eb6bdf10f3 | 88 | pc.printf("Z: %d \r\n",Z_accelleration); |
johnhalfpenny | 5:e1eb6bdf10f3 | 89 | } |
johnhalfpenny | 5:e1eb6bdf10f3 | 90 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 91 | // ------------------------------------------------------------------------------------------------------------ |
johnhalfpenny | 5:e1eb6bdf10f3 | 92 | // main loop |
SolderSplashLabs | 0:0bce3a738bcb | 93 | // ------------------------------------------------------------------------------------------------------------ |
SolderSplashLabs | 0:0bce3a738bcb | 94 | int main( void ) |
SolderSplashLabs | 0:0bce3a738bcb | 95 | { |
johnhalfpenny | 5:e1eb6bdf10f3 | 96 | // Initalise the LPC1347 |
SolderSplashLabs | 0:0bce3a738bcb | 97 | init(); |
SolderSplashLabs | 0:0bce3a738bcb | 98 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 99 | // Wait for terminal program to start |
lh121438 | 6:c3a33d04d731 | 100 | wait(WAITATSTART); |
SolderSplashLabs | 0:0bce3a738bcb | 101 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 102 | // Show the start banner |
johnhalfpenny | 5:e1eb6bdf10f3 | 103 | pc.printf("\r\n"); |
johnhalfpenny | 5:e1eb6bdf10f3 | 104 | pc.printf("+-------------------------------------------+\r\n"); |
johnhalfpenny | 5:e1eb6bdf10f3 | 105 | pc.printf("| 3-axis accelleration measurer |\r\n"); |
johnhalfpenny | 5:e1eb6bdf10f3 | 106 | pc.printf("| Laurence Halfpenny |\r\n"); |
lh121438 | 7:930020a7dc9e | 107 | pc.printf("| Version 1.0 |\r\n"); |
johnhalfpenny | 5:e1eb6bdf10f3 | 108 | pc.printf("+-------------------------------------------+\r\n"); |
johnhalfpenny | 5:e1eb6bdf10f3 | 109 | |
SolderSplashLabs | 0:0bce3a738bcb | 110 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 111 | // Forever check for commands from the keyboard |
johnhalfpenny | 5:e1eb6bdf10f3 | 112 | while (TRUE) |
johnhalfpenny | 5:e1eb6bdf10f3 | 113 | { |
johnhalfpenny | 5:e1eb6bdf10f3 | 114 | pc.printf("+-------------------------------------------+\r\n"); |
lh121438 | 7:930020a7dc9e | 115 | pc.printf("| V 1.0 MENU: |\r\n"); |
johnhalfpenny | 5:e1eb6bdf10f3 | 116 | pc.printf("| 1: Start monitoring |\r\n"); |
johnhalfpenny | 5:e1eb6bdf10f3 | 117 | pc.printf("| 2: Stop monitoring |\r\n"); |
johnhalfpenny | 5:e1eb6bdf10f3 | 118 | pc.printf("+-------------------------------------------+\r\n"); |
johnhalfpenny | 5:e1eb6bdf10f3 | 119 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 120 | switch(WaitForSerialCommand()) |
johnhalfpenny | 5:e1eb6bdf10f3 | 121 | { |
johnhalfpenny | 5:e1eb6bdf10f3 | 122 | case '1': |
johnhalfpenny | 5:e1eb6bdf10f3 | 123 | pc.printf("Starting monitor\r\n"); |
johnhalfpenny | 5:e1eb6bdf10f3 | 124 | monitoring=TRUE; |
johnhalfpenny | 5:e1eb6bdf10f3 | 125 | monitor_tick.attach(&monitor_accelleration,MONITORINTERVAL); |
johnhalfpenny | 5:e1eb6bdf10f3 | 126 | break; |
johnhalfpenny | 5:e1eb6bdf10f3 | 127 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 128 | case '2': |
johnhalfpenny | 5:e1eb6bdf10f3 | 129 | pc.printf("Stopping monitoring\r\n"); |
johnhalfpenny | 5:e1eb6bdf10f3 | 130 | monitoring=FALSE; |
johnhalfpenny | 5:e1eb6bdf10f3 | 131 | monitor_tick.detach(); |
johnhalfpenny | 5:e1eb6bdf10f3 | 132 | break; |
johnhalfpenny | 5:e1eb6bdf10f3 | 133 | |
johnhalfpenny | 5:e1eb6bdf10f3 | 134 | default: |
johnhalfpenny | 5:e1eb6bdf10f3 | 135 | pc.printf("Invalid command\r\n"); |
johnhalfpenny | 5:e1eb6bdf10f3 | 136 | break; |
johnhalfpenny | 5:e1eb6bdf10f3 | 137 | } |
johnhalfpenny | 5:e1eb6bdf10f3 | 138 | } |
johnhalfpenny | 5:e1eb6bdf10f3 | 139 | |
SolderSplashLabs | 0:0bce3a738bcb | 140 | } |