Gary Richardson / Mbed 2 deprecated Counter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Timer2.cpp Source File

Timer2.cpp

00001 
00002 /*
00003     This counter establishes the measurent interval.
00004     MAT2.0 (P8) is set when the interval has elapsed.
00005 
00006     This code generates timed intervals T = <prescale> * 100 ms.
00007     mbed pin 8 goes high at the beginning of the interval and returns to zero at the end.
00008     The interrupt occurs at the end of the interval.
00009 */
00010 #include "TARGET_LPC1768/LPC17xx.h"
00011 #include "mbed.h"
00012 #include "Timer2.h"
00013 
00014 funcptr timer2ISR;
00015 
00016 void Timer2_IRQHandler(void)
00017 {
00018     if (LPC_TIM2->IR & 1)           // MR0 interrupt
00019     {
00020         LPC_TIM2->IR = 1;           // clear the interrupt
00021         if (timer2ISR)
00022             timer2ISR();
00023     }
00024 }
00025 
00026 void Timer2_init(funcptr callback)
00027 {
00028     timer2ISR = callback;
00029     LPC_SC->PCONP |= (1<<22);           // Power on the Timer2
00030     LPC_SC->PCLKSEL1 &= ~(3<<12);
00031     LPC_SC->PCLKSEL1 |= (1<<12);        // Select  CCLK for Timer2
00032     LPC_PINCON->PINSEL0 &=~(3<<12);
00033     LPC_PINCON->PINSEL0 |= (3<<12);     // Connect MAT2.0 to mbed P8
00034     LPC_PINCON->PINMODE0 &=~(3<<12);
00035     LPC_PINCON->PINMODE0 |= (2<<12);    // no pull-p or pull-down
00036     if (callback)
00037     {
00038         NVIC_SetPriority(TIMER2_IRQn, 10);
00039         NVIC_SetVector(TIMER2_IRQn, (uint32_t)&Timer2_IRQHandler);
00040         NVIC_EnableIRQ(TIMER2_IRQn);
00041     }
00042 }
00043 void Timer2_start(int prescale)
00044 {
00045     LPC_TIM2->TCR = 2;                  // Reset the timer and prescale counters on next PCLK
00046     LPC_TIM2->MR0 = CLOCK/10;           // Set the Timer2 match register for 100 ms resolution
00047     LPC_TIM2->CTCR = 0;                 // Timer2 is incremented when the prescale register == 0
00048     LPC_TIM2->PR = prescale;            // Update prescale register
00049     LPC_TIM2->MCR = 3;                  // Interrupt on MR0 and reset
00050     LPC_TIM2->EMR = (3<<4)|1;           // Toggle MAT2.0 when match occurs.
00051     LPC_TIM2->TCR = 1;                  // Enable Timer2
00052     LPC_TIM3->TCR = 1;                  // Enable Timer3
00053 }