Gary Richardson / Mbed 2 deprecated Counter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Timer3.cpp Source File

Timer3.cpp

00001 
00002 // Timer 3 is used by mbed library functions: Ticker, Timer, Wait
00003 
00004 #include "TARGET_LPC1768/LPC17xx.h"
00005 #include "mbed.h"
00006 #include "Timer3.h"
00007 
00008 funcptr3 timer3ISR;
00009 
00010 void Timer3_IRQHandler(void)
00011 {
00012 #define ICR0    (1<<5)              // CR0 interrupt bit
00013 int count; 
00014     if (LPC_TIM3->IR & ICR0)
00015     {
00016         count = LPC_TIM3->TC;
00017         LPC_TIM3->TCR = 2;         // Disable
00018         LPC_TIM3->IR = ICR0;       // clear the interrupt
00019         timer3ISR(count);          // return the count
00020     }
00021 }
00022 
00023 void Timer3_init(funcptr3 callback)
00024 {
00025     timer3ISR = callback;
00026     LPC_SC->PCONP |= (1<<23);           // Power on the Timer3
00027     LPC_TIM3->TCR = 2;                  // Reset the timer and prescale counters on next PCLK
00028     LPC_SC->PCLKSEL1 &= ~(3<<14);
00029     LPC_SC->PCLKSEL1 |= (1<<14);        // Select  CCLK for Timer3
00030     LPC_PINCON->PINSEL1 |= (3<<16)|(3<<14);     // Connect CAP3.0 to P15, CAP3.1 to P16
00031     LPC_PINCON->PINMODE1 &=~((3<<16)|(3<<14));
00032     LPC_PINCON->PINMODE1 |= (2<<16)|(2<<14);    // no pull-p or pull-down
00033     LPC_TIM3->CTCR = 1;                 // TC incremented by CAP3.0 (P15) rising edge
00034     LPC_TIM3->PR = 0;                   // Set prescale register to zero
00035     LPC_TIM3->CCR = 0x30;              // Capture on CAP3.1 (P16) falling edge and interrupt
00036     NVIC_SetPriority(TIMER3_IRQn, 9);
00037     NVIC_SetVector(TIMER3_IRQn, (uint32_t)&Timer3_IRQHandler);
00038     NVIC_EnableIRQ(TIMER3_IRQn);
00039     LPC_TIM3->TCR = 2;                  // Disable
00040 }
00041