APP 4
Dependencies: mbed CRC16 mbed-rtos
APP.cpp@2:1250280a511b, 2016-02-20 (annotated)
- Committer:
- manl2003
- Date:
- Sat Feb 20 19:25:57 2016 +0000
- Revision:
- 2:1250280a511b
- Parent:
- 1:f212b6676849
- Child:
- 3:3ffa14e75b8a
Allo vincent
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
manl2003 | 2:1250280a511b | 1 | #include "APP.h" |
vinbel93 | 0:ac5e42371639 | 2 | |
vinbel93 | 0:ac5e42371639 | 3 | Serial pc(USBTX, USBRX); |
vinbel93 | 0:ac5e42371639 | 4 | |
vinbel93 | 0:ac5e42371639 | 5 | int state = 0; |
vinbel93 | 0:ac5e42371639 | 6 | float tempsHaut = 0.4; |
vinbel93 | 0:ac5e42371639 | 7 | float tempsBas = 0.1; |
vinbel93 | 0:ac5e42371639 | 8 | |
vinbel93 | 1:f212b6676849 | 9 | int benchmark(void (*function) (void)) |
vinbel93 | 0:ac5e42371639 | 10 | { |
vinbel93 | 0:ac5e42371639 | 11 | int counter = LPC_TIM2->TC; |
vinbel93 | 0:ac5e42371639 | 12 | function(); |
vinbel93 | 0:ac5e42371639 | 13 | return LPC_TIM2->TC - counter; |
vinbel93 | 0:ac5e42371639 | 14 | } |
vinbel93 | 0:ac5e42371639 | 15 | |
vinbel93 | 1:f212b6676849 | 16 | extern "C" void TIMER2_IRQHandler() |
vinbel93 | 0:ac5e42371639 | 17 | { |
vinbel93 | 0:ac5e42371639 | 18 | if ((LPC_TIM2->IR & 0x01) == 0x01) // if MR0 interrupt, proceed |
vinbel93 | 0:ac5e42371639 | 19 | { |
vinbel93 | 0:ac5e42371639 | 20 | if (state == 0) |
vinbel93 | 0:ac5e42371639 | 21 | { |
vinbel93 | 0:ac5e42371639 | 22 | state = 1; |
vinbel93 | 0:ac5e42371639 | 23 | LPC_TIM2->MR0 = (int) CLOCKS_TO_SECOND * tempsHaut; |
vinbel93 | 0:ac5e42371639 | 24 | } |
vinbel93 | 0:ac5e42371639 | 25 | else if (state == 1) |
vinbel93 | 0:ac5e42371639 | 26 | { |
vinbel93 | 0:ac5e42371639 | 27 | state = 0; |
vinbel93 | 0:ac5e42371639 | 28 | LPC_TIM2->MR0 = (int) CLOCKS_TO_SECOND * tempsBas; |
vinbel93 | 0:ac5e42371639 | 29 | } |
vinbel93 | 0:ac5e42371639 | 30 | |
vinbel93 | 0:ac5e42371639 | 31 | LPC_TIM2->IR |= 1 << 0; // Clear MR0 interrupt flag |
vinbel93 | 0:ac5e42371639 | 32 | } |
vinbel93 | 0:ac5e42371639 | 33 | } |
vinbel93 | 0:ac5e42371639 | 34 | |
vinbel93 | 1:f212b6676849 | 35 | void initTimer() |
vinbel93 | 0:ac5e42371639 | 36 | { |
vinbel93 | 0:ac5e42371639 | 37 | LPC_SC->PCLKSEL1 |= (1 << 12); // pclk = cclk timer2 |
vinbel93 | 0:ac5e42371639 | 38 | LPC_SC->PCONP |= (1 << 22); // timer2 power on |
vinbel93 | 0:ac5e42371639 | 39 | LPC_TIM2->MR0 = 9600000; // 100 msec |
vinbel93 | 0:ac5e42371639 | 40 | LPC_TIM2->MCR = 1; // interrupt and reset control |
vinbel93 | 0:ac5e42371639 | 41 | // 3 = Interrupt & reset timer2 on match |
vinbel93 | 0:ac5e42371639 | 42 | // 1 = Interrupt only, no reset of timer0 |
vinbel93 | 0:ac5e42371639 | 43 | LPC_TIM2->EMR = (3 << 4); // EMC0 = 11 (Toogle) |
vinbel93 | 0:ac5e42371639 | 44 | NVIC_EnableIRQ(TIMER2_IRQn); // enable timer2 interrupt |
vinbel93 | 0:ac5e42371639 | 45 | LPC_TIM2->TCR = 1; // enable Timer2 |
vinbel93 | 0:ac5e42371639 | 46 | } |
vinbel93 | 0:ac5e42371639 | 47 | |
vinbel93 | 1:f212b6676849 | 48 | int main() |
vinbel93 | 0:ac5e42371639 | 49 | { |
vinbel93 | 0:ac5e42371639 | 50 | LPC_PINCON->PINSEL0 |= (3 << 12); // P0.6 = MAT2.0 |
vinbel93 | 0:ac5e42371639 | 51 | |
vinbel93 | 1:f212b6676849 | 52 | initTimer(); |
vinbel93 | 0:ac5e42371639 | 53 | |
vinbel93 | 0:ac5e42371639 | 54 | while(1) |
vinbel93 | 0:ac5e42371639 | 55 | { |
vinbel93 | 0:ac5e42371639 | 56 | wait(1.0); |
vinbel93 | 0:ac5e42371639 | 57 | } |
vinbel93 | 0:ac5e42371639 | 58 | } |