Use of two timers to implement a counter of an external signal

Dependencies:   mbed

Committer:
garyr
Date:
Tue Jan 28 17:01:19 2014 +0000
Revision:
0:e619b6823668
First working version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
garyr 0:e619b6823668 1
garyr 0:e619b6823668 2 // Timer 3 is used by mbed library functions: Ticker, Timer, Wait
garyr 0:e619b6823668 3
garyr 0:e619b6823668 4 #include "TARGET_LPC1768/LPC17xx.h"
garyr 0:e619b6823668 5 #include "mbed.h"
garyr 0:e619b6823668 6 #include "Timer3.h"
garyr 0:e619b6823668 7
garyr 0:e619b6823668 8 funcptr3 timer3ISR;
garyr 0:e619b6823668 9
garyr 0:e619b6823668 10 void Timer3_IRQHandler(void)
garyr 0:e619b6823668 11 {
garyr 0:e619b6823668 12 #define ICR0 (1<<5) // CR0 interrupt bit
garyr 0:e619b6823668 13 int count;
garyr 0:e619b6823668 14 if (LPC_TIM3->IR & ICR0)
garyr 0:e619b6823668 15 {
garyr 0:e619b6823668 16 count = LPC_TIM3->TC;
garyr 0:e619b6823668 17 LPC_TIM3->TCR = 2; // Disable
garyr 0:e619b6823668 18 LPC_TIM3->IR = ICR0; // clear the interrupt
garyr 0:e619b6823668 19 timer3ISR(count); // return the count
garyr 0:e619b6823668 20 }
garyr 0:e619b6823668 21 }
garyr 0:e619b6823668 22
garyr 0:e619b6823668 23 void Timer3_init(funcptr3 callback)
garyr 0:e619b6823668 24 {
garyr 0:e619b6823668 25 timer3ISR = callback;
garyr 0:e619b6823668 26 LPC_SC->PCONP |= (1<<23); // Power on the Timer3
garyr 0:e619b6823668 27 LPC_TIM3->TCR = 2; // Reset the timer and prescale counters on next PCLK
garyr 0:e619b6823668 28 LPC_SC->PCLKSEL1 &= ~(3<<14);
garyr 0:e619b6823668 29 LPC_SC->PCLKSEL1 |= (1<<14); // Select CCLK for Timer3
garyr 0:e619b6823668 30 LPC_PINCON->PINSEL1 |= (3<<16)|(3<<14); // Connect CAP3.0 to P15, CAP3.1 to P16
garyr 0:e619b6823668 31 LPC_PINCON->PINMODE1 &=~((3<<16)|(3<<14));
garyr 0:e619b6823668 32 LPC_PINCON->PINMODE1 |= (2<<16)|(2<<14); // no pull-p or pull-down
garyr 0:e619b6823668 33 LPC_TIM3->CTCR = 1; // TC incremented by CAP3.0 (P15) rising edge
garyr 0:e619b6823668 34 LPC_TIM3->PR = 0; // Set prescale register to zero
garyr 0:e619b6823668 35 LPC_TIM3->CCR = 0x30; // Capture on CAP3.1 (P16) falling edge and interrupt
garyr 0:e619b6823668 36 NVIC_SetPriority(TIMER3_IRQn, 9);
garyr 0:e619b6823668 37 NVIC_SetVector(TIMER3_IRQn, (uint32_t)&Timer3_IRQHandler);
garyr 0:e619b6823668 38 NVIC_EnableIRQ(TIMER3_IRQn);
garyr 0:e619b6823668 39 LPC_TIM3->TCR = 2; // Disable
garyr 0:e619b6823668 40 }
garyr 0:e619b6823668 41