Gary Richardson
/
Counter
Use of two timers to implement a counter of an external signal
Timer2.cpp@0:e619b6823668, 2014-01-28 (annotated)
- Committer:
- garyr
- Date:
- Tue Jan 28 17:01:19 2014 +0000
- Revision:
- 0:e619b6823668
First working version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
garyr | 0:e619b6823668 | 1 | |
garyr | 0:e619b6823668 | 2 | /* |
garyr | 0:e619b6823668 | 3 | This counter establishes the measurent interval. |
garyr | 0:e619b6823668 | 4 | MAT2.0 (P8) is set when the interval has elapsed. |
garyr | 0:e619b6823668 | 5 | |
garyr | 0:e619b6823668 | 6 | This code generates timed intervals T = <prescale> * 100 ms. |
garyr | 0:e619b6823668 | 7 | mbed pin 8 goes high at the beginning of the interval and returns to zero at the end. |
garyr | 0:e619b6823668 | 8 | The interrupt occurs at the end of the interval. |
garyr | 0:e619b6823668 | 9 | */ |
garyr | 0:e619b6823668 | 10 | #include "TARGET_LPC1768/LPC17xx.h" |
garyr | 0:e619b6823668 | 11 | #include "mbed.h" |
garyr | 0:e619b6823668 | 12 | #include "Timer2.h" |
garyr | 0:e619b6823668 | 13 | |
garyr | 0:e619b6823668 | 14 | funcptr timer2ISR; |
garyr | 0:e619b6823668 | 15 | |
garyr | 0:e619b6823668 | 16 | void Timer2_IRQHandler(void) |
garyr | 0:e619b6823668 | 17 | { |
garyr | 0:e619b6823668 | 18 | if (LPC_TIM2->IR & 1) // MR0 interrupt |
garyr | 0:e619b6823668 | 19 | { |
garyr | 0:e619b6823668 | 20 | LPC_TIM2->IR = 1; // clear the interrupt |
garyr | 0:e619b6823668 | 21 | if (timer2ISR) |
garyr | 0:e619b6823668 | 22 | timer2ISR(); |
garyr | 0:e619b6823668 | 23 | } |
garyr | 0:e619b6823668 | 24 | } |
garyr | 0:e619b6823668 | 25 | |
garyr | 0:e619b6823668 | 26 | void Timer2_init(funcptr callback) |
garyr | 0:e619b6823668 | 27 | { |
garyr | 0:e619b6823668 | 28 | timer2ISR = callback; |
garyr | 0:e619b6823668 | 29 | LPC_SC->PCONP |= (1<<22); // Power on the Timer2 |
garyr | 0:e619b6823668 | 30 | LPC_SC->PCLKSEL1 &= ~(3<<12); |
garyr | 0:e619b6823668 | 31 | LPC_SC->PCLKSEL1 |= (1<<12); // Select CCLK for Timer2 |
garyr | 0:e619b6823668 | 32 | LPC_PINCON->PINSEL0 &=~(3<<12); |
garyr | 0:e619b6823668 | 33 | LPC_PINCON->PINSEL0 |= (3<<12); // Connect MAT2.0 to mbed P8 |
garyr | 0:e619b6823668 | 34 | LPC_PINCON->PINMODE0 &=~(3<<12); |
garyr | 0:e619b6823668 | 35 | LPC_PINCON->PINMODE0 |= (2<<12); // no pull-p or pull-down |
garyr | 0:e619b6823668 | 36 | if (callback) |
garyr | 0:e619b6823668 | 37 | { |
garyr | 0:e619b6823668 | 38 | NVIC_SetPriority(TIMER2_IRQn, 10); |
garyr | 0:e619b6823668 | 39 | NVIC_SetVector(TIMER2_IRQn, (uint32_t)&Timer2_IRQHandler); |
garyr | 0:e619b6823668 | 40 | NVIC_EnableIRQ(TIMER2_IRQn); |
garyr | 0:e619b6823668 | 41 | } |
garyr | 0:e619b6823668 | 42 | } |
garyr | 0:e619b6823668 | 43 | void Timer2_start(int prescale) |
garyr | 0:e619b6823668 | 44 | { |
garyr | 0:e619b6823668 | 45 | LPC_TIM2->TCR = 2; // Reset the timer and prescale counters on next PCLK |
garyr | 0:e619b6823668 | 46 | LPC_TIM2->MR0 = CLOCK/10; // Set the Timer2 match register for 100 ms resolution |
garyr | 0:e619b6823668 | 47 | LPC_TIM2->CTCR = 0; // Timer2 is incremented when the prescale register == 0 |
garyr | 0:e619b6823668 | 48 | LPC_TIM2->PR = prescale; // Update prescale register |
garyr | 0:e619b6823668 | 49 | LPC_TIM2->MCR = 3; // Interrupt on MR0 and reset |
garyr | 0:e619b6823668 | 50 | LPC_TIM2->EMR = (3<<4)|1; // Toggle MAT2.0 when match occurs. |
garyr | 0:e619b6823668 | 51 | LPC_TIM2->TCR = 1; // Enable Timer2 |
garyr | 0:e619b6823668 | 52 | LPC_TIM3->TCR = 1; // Enable Timer3 |
garyr | 0:e619b6823668 | 53 | } |