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: mbed PowerControl
Fork of Projet_S5 by
main.cpp@7:89be89aeed5a, 2014-04-05 (annotated)
- Committer:
- joGenie
- Date:
- Sat Apr 05 18:07:45 2014 +0000
- Revision:
- 7:89be89aeed5a
- Parent:
- 6:ef8bfca9e69b
- Child:
- 11:9c0786fc06b4
Analyzer initial
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| joGenie | 0:d80295a0bcc2 | 1 | #include "accelerometer.h" |
| joGenie | 3:17a4ceb30535 | 2 | #include "analyzer.h" |
| joGenie | 0:d80295a0bcc2 | 3 | #include "interrupt.h" |
| joGenie | 0:d80295a0bcc2 | 4 | |
| joGenie | 0:d80295a0bcc2 | 5 | Serial pc(USBTX, USBRX); |
| joGenie | 1:feae56685ab1 | 6 | DigitalOut led1(LED1); |
| joGenie | 4:f72c51a2108c | 7 | DigitalOut led2(LED2); |
| joGenie | 4:f72c51a2108c | 8 | DigitalOut led4(LED4); |
| joGenie | 4:f72c51a2108c | 9 | DigitalIn button(p30); |
| joGenie | 1:feae56685ab1 | 10 | |
| joGenie | 0:d80295a0bcc2 | 11 | void *accelerometer = Accelerometer_C_new(); |
| joGenie | 4:f72c51a2108c | 12 | void *analyzer = Analyzer_C_new(); |
| joGenie | 4:f72c51a2108c | 13 | bool rebound = true; |
| joGenie | 4:f72c51a2108c | 14 | bool appuyer = false; |
| joGenie | 0:d80295a0bcc2 | 15 | |
| joGenie | 1:feae56685ab1 | 16 | extern "C" void interruptCapture(void) |
| joGenie | 0:d80295a0bcc2 | 17 | { |
| joGenie | 4:f72c51a2108c | 18 | if(rebound && !appuyer) |
| joGenie | 1:feae56685ab1 | 19 | { |
| joGenie | 4:f72c51a2108c | 20 | LPC_TIM2->TC = 0; // clear timer counter |
| joGenie | 4:f72c51a2108c | 21 | LPC_TIM2->PC = 0; // clear prescale counter |
| joGenie | 4:f72c51a2108c | 22 | LPC_TIM2->PR = 0; // clear prescale register |
| joGenie | 4:f72c51a2108c | 23 | LPC_TIM2->MCR |= 0x03; // interrupt and reset control |
| joGenie | 4:f72c51a2108c | 24 | LPC_TIM2->CCR = 0; //no capture interupt |
| joGenie | 4:f72c51a2108c | 25 | rebound = false; |
| joGenie | 4:f72c51a2108c | 26 | } |
| joGenie | 4:f72c51a2108c | 27 | else if(!rebound && !appuyer) |
| joGenie | 0:d80295a0bcc2 | 28 | { |
| joGenie | 4:f72c51a2108c | 29 | led1 = 1; |
| joGenie | 4:f72c51a2108c | 30 | LPC_TIM2->MCR = 0; |
| joGenie | 4:f72c51a2108c | 31 | LPC_TIM2->CCR = (0x06 << 0); //falling edge |
| joGenie | 4:f72c51a2108c | 32 | |
| joGenie | 4:f72c51a2108c | 33 | LPC_TIM3->TC = 0; // clear timer counter |
| joGenie | 4:f72c51a2108c | 34 | LPC_TIM3->PC = 0; // clear prescale counter |
| joGenie | 4:f72c51a2108c | 35 | LPC_TIM3->PR = 0; // clear prescale register |
| joGenie | 4:f72c51a2108c | 36 | LPC_TIM3->IR |= 0xFF; |
| joGenie | 4:f72c51a2108c | 37 | LPC_TIM3->TCR = 0x01; //enable timer 3 |
| joGenie | 4:f72c51a2108c | 38 | |
| joGenie | 4:f72c51a2108c | 39 | appuyer = true; |
| joGenie | 4:f72c51a2108c | 40 | NVIC_EnableIRQ(TIMER3_IRQn); // Enable timer3 interrupt |
| joGenie | 7:89be89aeed5a | 41 | |
| joGenie | 7:89be89aeed5a | 42 | signed char* values = Accelerometer_C_getAccelValue(accelerometer); |
| joGenie | 7:89be89aeed5a | 43 | |
| joGenie | 7:89be89aeed5a | 44 | // Analyze data |
| joGenie | 7:89be89aeed5a | 45 | Analyzer_C_setInitial(values, analyzer); |
| joGenie | 0:d80295a0bcc2 | 46 | } |
| joGenie | 4:f72c51a2108c | 47 | else |
| joGenie | 4:f72c51a2108c | 48 | { |
| joGenie | 4:f72c51a2108c | 49 | led1 = 0; |
| joGenie | 7:89be89aeed5a | 50 | LPC_TIM2->CCR = (5 << 0); //rising edge |
| joGenie | 4:f72c51a2108c | 51 | |
| joGenie | 4:f72c51a2108c | 52 | LPC_TIM3->TC = 0; // clear timer counter |
| joGenie | 4:f72c51a2108c | 53 | LPC_TIM3->PC = 0; // clear prescale counter |
| joGenie | 4:f72c51a2108c | 54 | LPC_TIM3->PR = 0; // clear prescale register |
| joGenie | 4:f72c51a2108c | 55 | LPC_TIM3->IR |= 0xFF; |
| joGenie | 4:f72c51a2108c | 56 | LPC_TIM3->TCR = 0; //disable timer 3 |
| joGenie | 4:f72c51a2108c | 57 | |
| joGenie | 4:f72c51a2108c | 58 | appuyer = false; |
| joGenie | 4:f72c51a2108c | 59 | rebound = true; |
| joGenie | 4:f72c51a2108c | 60 | NVIC_DisableIRQ(TIMER3_IRQn); // Disable timer3 interrupt |
| joGenie | 7:89be89aeed5a | 61 | |
| joGenie | 7:89be89aeed5a | 62 | Analyzer_C_checkMouvement(analyzer); |
| joGenie | 4:f72c51a2108c | 63 | } |
| joGenie | 4:f72c51a2108c | 64 | |
| joGenie | 1:feae56685ab1 | 65 | |
| joGenie | 1:feae56685ab1 | 66 | LPC_TIM2->IR |= 0xFF; |
| joGenie | 0:d80295a0bcc2 | 67 | } |
| joGenie | 0:d80295a0bcc2 | 68 | |
| joGenie | 1:feae56685ab1 | 69 | extern "C" void interruptMatch(void) |
| joGenie | 0:d80295a0bcc2 | 70 | { |
| joGenie | 4:f72c51a2108c | 71 | if(button) |
| joGenie | 4:f72c51a2108c | 72 | { |
| joGenie | 4:f72c51a2108c | 73 | led4 = !led4; |
| joGenie | 6:ef8bfca9e69b | 74 | signed char* values; |
| joGenie | 0:d80295a0bcc2 | 75 | |
| joGenie | 4:f72c51a2108c | 76 | values = Accelerometer_C_getAccelValue(accelerometer); |
| joGenie | 0:d80295a0bcc2 | 77 | |
| joGenie | 6:ef8bfca9e69b | 78 | pc.printf("\n\r x: %i, y: %i, z: %i; ", values[0],values[1],values[2]); |
| joGenie | 4:f72c51a2108c | 79 | |
| joGenie | 4:f72c51a2108c | 80 | // Analyze data |
| joGenie | 4:f72c51a2108c | 81 | Analyzer_C_setMinMax(values, analyzer); |
| joGenie | 2:d7784cc39c81 | 82 | |
| joGenie | 4:f72c51a2108c | 83 | delete values; |
| joGenie | 4:f72c51a2108c | 84 | } |
| joGenie | 4:f72c51a2108c | 85 | |
| joGenie | 4:f72c51a2108c | 86 | LPC_TIM3->IR |= 0xFF; // Reset timer |
| joGenie | 0:d80295a0bcc2 | 87 | } |
| joGenie | 0:d80295a0bcc2 | 88 | |
| joGenie | 0:d80295a0bcc2 | 89 | void initialize() |
| joGenie | 0:d80295a0bcc2 | 90 | { |
| joGenie | 4:f72c51a2108c | 91 | |
| joGenie | 4:f72c51a2108c | 92 | // CAPTURE |
| joGenie | 4:f72c51a2108c | 93 | // Enable the pins on the device to use TIMER CAP2.0 on pin 30 |
| joGenie | 5:6313ddd0dfdd | 94 | LPC_PINCON->PINSEL0 |= 3 << 8; //set pin 30 capture 2.0 |
| joGenie | 5:6313ddd0dfdd | 95 | LPC_SC->PCONP |= (1 << 22); // Timer2 power on |
| joGenie | 0:d80295a0bcc2 | 96 | |
| joGenie | 5:6313ddd0dfdd | 97 | //LPC_TIM2->MCR |= 3; //interrupt and reset control |
| joGenie | 4:f72c51a2108c | 98 | LPC_TIM2->TC = 0; // clear timer counter |
| joGenie | 4:f72c51a2108c | 99 | LPC_TIM2->PC = 0; // clear prescale counter |
| joGenie | 4:f72c51a2108c | 100 | LPC_TIM2->PR = 0; // clear prescale register |
| joGenie | 4:f72c51a2108c | 101 | LPC_TIM2->MR0 = SystemCoreClock / 100; //match pour rebound |
| joGenie | 4:f72c51a2108c | 102 | |
| joGenie | 5:6313ddd0dfdd | 103 | LPC_TIM2->IR |= 0xFF; // Clear CR0 interrupt flag |
| joGenie | 5:6313ddd0dfdd | 104 | LPC_TIM2->CCR = (5 << 0); // enable cap2.0 rising-edge capture; interrupt on cap2.0 p.496 |
| joGenie | 5:6313ddd0dfdd | 105 | LPC_TIM2->TCR = (1 << 1); // reset timer |
| joGenie | 5:6313ddd0dfdd | 106 | LPC_TIM2->TCR = 1; // enable timer |
| joGenie | 0:d80295a0bcc2 | 107 | |
| joGenie | 5:6313ddd0dfdd | 108 | NVIC_EnableIRQ(TIMER2_IRQn); // Enable timer2 interrupt |
| joGenie | 4:f72c51a2108c | 109 | NVIC_SetVector(TIMER2_IRQn, uint32_t(interruptCapture)); |
| joGenie | 0:d80295a0bcc2 | 110 | |
| joGenie | 4:f72c51a2108c | 111 | // TIMER 3 |
| joGenie | 5:6313ddd0dfdd | 112 | LPC_SC->PCONP |= 1 << 23; // Timer3 power on |
| joGenie | 4:f72c51a2108c | 113 | |
| joGenie | 5:6313ddd0dfdd | 114 | LPC_TIM3->MCR |= 3; // Interrupt and reset control |
| joGenie | 5:6313ddd0dfdd | 115 | LPC_TIM3->TC = 0; // clear timer counter |
| joGenie | 5:6313ddd0dfdd | 116 | LPC_TIM3->PC = 0; // clear prescale counter |
| joGenie | 5:6313ddd0dfdd | 117 | LPC_TIM3->PR = 0; // clear prescale register |
| joGenie | 5:6313ddd0dfdd | 118 | LPC_TIM3->MR0 = SystemCoreClock / 20; |
| joGenie | 4:f72c51a2108c | 119 | |
| joGenie | 5:6313ddd0dfdd | 120 | LPC_TIM3->IR |= 0xFF; // Clear MR0 interrupt flag |
| joGenie | 5:6313ddd0dfdd | 121 | LPC_TIM3->TCR = (1 << 1); // Reset Timer3 |
| joGenie | 5:6313ddd0dfdd | 122 | LPC_TIM3->TCR = 0; // Disable timer |
| joGenie | 0:d80295a0bcc2 | 123 | |
| joGenie | 5:6313ddd0dfdd | 124 | //NVIC_EnableIRQ(TIMER3_IRQn); // Enable timer3 interrupt |
| joGenie | 1:feae56685ab1 | 125 | NVIC_SetVector(TIMER3_IRQn, uint32_t(interruptMatch)); |
| joGenie | 0:d80295a0bcc2 | 126 | } |
| joGenie | 0:d80295a0bcc2 | 127 | |
| joGenie | 0:d80295a0bcc2 | 128 | int main() |
| joGenie | 0:d80295a0bcc2 | 129 | { |
| joGenie | 1:feae56685ab1 | 130 | initialize(); |
| joGenie | 1:feae56685ab1 | 131 | |
| joGenie | 1:feae56685ab1 | 132 | while(true) |
| joGenie | 1:feae56685ab1 | 133 | { |
| joGenie | 0:d80295a0bcc2 | 134 | } |
| joGenie | 0:d80295a0bcc2 | 135 | } |
