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
- Committer:
- lh121438
- Date:
- 2015-09-03
- Revision:
- 7:930020a7dc9e
- Parent:
- 6:c3a33d04d731
- Child:
- 8:9a3d2d28e7e5
File content as of revision 7:930020a7dc9e:
// // Accellerometer project // // Laurence Halfpenny // August 2015 #include "mbed.h" #include "main.h" #include "USBSerial.h" USBSerial pc; // USB CDC serial port // Define the X, Y & Z analogue inputs on ADCs AnalogIn Z_in(P0_11); // Z accelleration voltage AnalogIn Y_in(P0_12); // Y accelleration voltage AnalogIn X_in(P0_13); // X accelleration voltage // Acceleration values int Z_accelleration=0; int Y_accelleration=0; int X_accelleration=0; // Control variables bool monitoring=FALSE; // Controls whether monitoring is running or stopped // Ticker for monitoring Ticker monitor_tick; // ------------------------------------------------------------------------------------------------------------ // Wait for a character to be typed // ------------------------------------------------------------------------------------------------------------ char WaitForSerialCommand ( void ) { char charIn = 0; char prevCharIn; pc.printf("Enter command: "); while (TRUE) { prevCharIn = charIn; charIn = pc.getc(); pc.printf("%c", charIn); if ((charIn == '\n') || (charIn == '\r')) { break; } } return ( prevCharIn ); } // ------------------------------------------------------------------------------------------------------------ // Initialise the LPC1347 processor // ------------------------------------------------------------------------------------------------------------ void init() { NVIC_SetPriority(SSP1_IRQn, 0x0); NVIC_SetPriority(PIN_INT0_IRQn, 0x1); // SysTick set to lower priority than Wi-Fi SPI bus interrupt NVIC_SetPriority(SysTick_IRQn, 0x2); // Enable RAM1 LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26); } // ------------------------------------------------------------------------------------------------------------ // The monitoring software, triggered by the monitoring ticker // ------------------------------------------------------------------------------------------------------------ void monitor_accelleration(void) { X_accelleration=X_in.read_u16(); // Read and print X accelleration X_accelleration=(X_accelleration-(0xFFFF/2)); // Convert to change in accelleration X_accelleration=X_accelleration/16; pc.printf("X: %d ",X_accelleration); Y_accelleration=Y_in.read_u16(); // Read and print Y accelleration Y_accelleration=(Y_accelleration-(0xFFFF/2)); // Convert to change in accelleration Y_accelleration=Y_accelleration/16; pc.printf("Y: %d ",Y_accelleration); Z_accelleration=Z_in.read_u16(); // Read and print Z accelleration Z_accelleration=(Z_accelleration-(0xFFFF/2)); // Convert to change in accelleration Z_accelleration=Z_accelleration/16; pc.printf("Z: %d \r\n",Z_accelleration); } // ------------------------------------------------------------------------------------------------------------ // main loop // ------------------------------------------------------------------------------------------------------------ int main( void ) { // Initalise the LPC1347 init(); // Wait for terminal program to start wait(WAITATSTART); // Show the start banner pc.printf("\r\n"); pc.printf("+-------------------------------------------+\r\n"); pc.printf("| 3-axis accelleration measurer |\r\n"); pc.printf("| Laurence Halfpenny |\r\n"); pc.printf("| Version 1.0 |\r\n"); pc.printf("+-------------------------------------------+\r\n"); // Forever check for commands from the keyboard while (TRUE) { pc.printf("+-------------------------------------------+\r\n"); pc.printf("| V 1.0 MENU: |\r\n"); pc.printf("| 1: Start monitoring |\r\n"); pc.printf("| 2: Stop monitoring |\r\n"); pc.printf("+-------------------------------------------+\r\n"); switch(WaitForSerialCommand()) { case '1': pc.printf("Starting monitor\r\n"); monitoring=TRUE; monitor_tick.attach(&monitor_accelleration,MONITORINTERVAL); break; case '2': pc.printf("Stopping monitoring\r\n"); monitoring=FALSE; monitor_tick.detach(); break; default: pc.printf("Invalid command\r\n"); break; } } }