Gary Richardson
/
Counter
Use of two timers to implement a counter of an external signal
Revision 0:e619b6823668, committed 2014-01-28
- Comitter:
- garyr
- Date:
- Tue Jan 28 17:01:19 2014 +0000
- Commit message:
- First working version
Changed in this revision
diff -r 000000000000 -r e619b6823668 Timer2.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Timer2.cpp Tue Jan 28 17:01:19 2014 +0000 @@ -0,0 +1,53 @@ + +/* + This counter establishes the measurent interval. + MAT2.0 (P8) is set when the interval has elapsed. + + This code generates timed intervals T = <prescale> * 100 ms. + mbed pin 8 goes high at the beginning of the interval and returns to zero at the end. + The interrupt occurs at the end of the interval. +*/ +#include "TARGET_LPC1768/LPC17xx.h" +#include "mbed.h" +#include "Timer2.h" + +funcptr timer2ISR; + +void Timer2_IRQHandler(void) +{ + if (LPC_TIM2->IR & 1) // MR0 interrupt + { + LPC_TIM2->IR = 1; // clear the interrupt + if (timer2ISR) + timer2ISR(); + } +} + +void Timer2_init(funcptr callback) +{ + timer2ISR = callback; + LPC_SC->PCONP |= (1<<22); // Power on the Timer2 + LPC_SC->PCLKSEL1 &= ~(3<<12); + LPC_SC->PCLKSEL1 |= (1<<12); // Select CCLK for Timer2 + LPC_PINCON->PINSEL0 &=~(3<<12); + LPC_PINCON->PINSEL0 |= (3<<12); // Connect MAT2.0 to mbed P8 + LPC_PINCON->PINMODE0 &=~(3<<12); + LPC_PINCON->PINMODE0 |= (2<<12); // no pull-p or pull-down + if (callback) + { + NVIC_SetPriority(TIMER2_IRQn, 10); + NVIC_SetVector(TIMER2_IRQn, (uint32_t)&Timer2_IRQHandler); + NVIC_EnableIRQ(TIMER2_IRQn); + } +} +void Timer2_start(int prescale) +{ + LPC_TIM2->TCR = 2; // Reset the timer and prescale counters on next PCLK + LPC_TIM2->MR0 = CLOCK/10; // Set the Timer2 match register for 100 ms resolution + LPC_TIM2->CTCR = 0; // Timer2 is incremented when the prescale register == 0 + LPC_TIM2->PR = prescale; // Update prescale register + LPC_TIM2->MCR = 3; // Interrupt on MR0 and reset + LPC_TIM2->EMR = (3<<4)|1; // Toggle MAT2.0 when match occurs. + LPC_TIM2->TCR = 1; // Enable Timer2 + LPC_TIM3->TCR = 1; // Enable Timer3 +}
diff -r 000000000000 -r e619b6823668 Timer2.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Timer2.h Tue Jan 28 17:01:19 2014 +0000 @@ -0,0 +1,12 @@ +#ifndef __TIMER2_H +#define __TIMER2_H + +#define CLOCK 96000000 + +typedef void (*funcptr)(void); +extern void Timer2_init(funcptr callback); +extern void Timer2_start(int prescale); + +#endif + +
diff -r 000000000000 -r e619b6823668 Timer3.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Timer3.cpp Tue Jan 28 17:01:19 2014 +0000 @@ -0,0 +1,41 @@ + +// Timer 3 is used by mbed library functions: Ticker, Timer, Wait + +#include "TARGET_LPC1768/LPC17xx.h" +#include "mbed.h" +#include "Timer3.h" + +funcptr3 timer3ISR; + +void Timer3_IRQHandler(void) +{ +#define ICR0 (1<<5) // CR0 interrupt bit +int count; + if (LPC_TIM3->IR & ICR0) + { + count = LPC_TIM3->TC; + LPC_TIM3->TCR = 2; // Disable + LPC_TIM3->IR = ICR0; // clear the interrupt + timer3ISR(count); // return the count + } +} + +void Timer3_init(funcptr3 callback) +{ + timer3ISR = callback; + LPC_SC->PCONP |= (1<<23); // Power on the Timer3 + LPC_TIM3->TCR = 2; // Reset the timer and prescale counters on next PCLK + LPC_SC->PCLKSEL1 &= ~(3<<14); + LPC_SC->PCLKSEL1 |= (1<<14); // Select CCLK for Timer3 + LPC_PINCON->PINSEL1 |= (3<<16)|(3<<14); // Connect CAP3.0 to P15, CAP3.1 to P16 + LPC_PINCON->PINMODE1 &=~((3<<16)|(3<<14)); + LPC_PINCON->PINMODE1 |= (2<<16)|(2<<14); // no pull-p or pull-down + LPC_TIM3->CTCR = 1; // TC incremented by CAP3.0 (P15) rising edge + LPC_TIM3->PR = 0; // Set prescale register to zero + LPC_TIM3->CCR = 0x30; // Capture on CAP3.1 (P16) falling edge and interrupt + NVIC_SetPriority(TIMER3_IRQn, 9); + NVIC_SetVector(TIMER3_IRQn, (uint32_t)&Timer3_IRQHandler); + NVIC_EnableIRQ(TIMER3_IRQn); + LPC_TIM3->TCR = 2; // Disable +} +
diff -r 000000000000 -r e619b6823668 Timer3.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Timer3.h Tue Jan 28 17:01:19 2014 +0000 @@ -0,0 +1,8 @@ + +#ifndef H_TIMER3 +#define H_TIMER3 + +typedef void (*funcptr3)(int); +extern void Timer3_init(funcptr3); + +#endif
diff -r 000000000000 -r e619b6823668 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Jan 28 17:01:19 2014 +0000 @@ -0,0 +1,48 @@ + +#include "mbed.h" +#include "Timer2.h" +#include "Timer3.h" +/* + Connect P8 (MAT2.0) to P16 (CAP3.1) + Connect external input on P15 (CAP3.0) to PWM output on P26 +*/ +Serial pc(USBTX, USBRX); // tx, rx +DigitalOut out1(p5), out2(p6); +PwmOut pw(p26); + +int done; +void Timer3Isr(int count) +{ + pc.printf("count=%d\n\r", count); + LPC_TIM2->TCR = 2; // Disable Timer2 + done = 1; +} + +void Timer2Isr(void) +{ + out1 = !out1; +} + +int main(void) +{ +int prescale; // Units are 0.1 second. + + out1 = 0; + out2 = 0; + pw.period_us(10); + pw.write(0.5); + Timer3_init(Timer3Isr); + Timer2_init(Timer2Isr); + while(1) + { + pc.printf("PS: "); + pc.scanf("%d", &prescale); // Interval is <prescale>+1 * 0.1 second. + pc.printf("%d\n\r", prescale); + done = 0; + Timer2_start(prescale); + while (!done) + out2 = !out2; + } +} + +
diff -r 000000000000 -r e619b6823668 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Jan 28 17:01:19 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/824293ae5e43 \ No newline at end of file