#include "driver_config.h" #include "target_config.h" #include "gpio.h" #include "timer16.h" #include "timer32.h" int main (void) { /* Init the system */ SystemInit(); /* Init the GPIO */ GPIOInit(); //initialize 16-bit COUNTER/TIMER 0 init_timer16(1, TIME_INTERVAL); enable_timer16(1); while (1) { } return 0; } |
void TIMER16_1_IRQHandler(void) { if ( LPC_TMR16B1->IR & 0x1 ) { LPC_TMR16B1->IR = 1; // clear interrupt flag timer16_1_counter++; } if ( LPC_TMR16B1->IR & (0x1<<4) ) { LPC_TMR16B1->TC = 0;//reset timer timer16_1_capture = LPC_TMR16B1->CR0;//read period LPC_TMR16B1->IR = 0x1<<4;/* clear interrupt flag */ } return; } void init_timer16(uint8_t timer_num, uint16_t TimerInterval) { if ( timer_num == 0 ) { /* Some of the I/O pins need to be clearfully planned if you use below module because JTAG and TIMER CAP/MAT pins are muxed. */ LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7); LPC_IOCON->PIO0_2 &= ~0x07;/* Timer0_16 I/O config */ LPC_IOCON->PIO0_2 |= 0x02;/* Timer0_16 CAP0 */ LPC_IOCON->PIO0_8 &= ~0x07; LPC_IOCON->PIO0_8 |= 0x02;/* Timer0_16 MAT0 */ LPC_IOCON->PIO0_9 &= ~0x07; LPC_IOCON->PIO0_9 |= 0x02;/* Timer0_16 MAT1 */ #ifdef __JTAG_DISABLED LPC_IOCON->JTAG_TCK_PIO0_10 &= ~0x07; LPC_IOCON->JTAG_TCK_PIO0_10 |= 0x03;/* Timer0_16 MAT2 */ #endif timer16_0_counter = 0; timer16_0_capture = 0; LPC_TMR16B0->PR = MHZ_PRESCALE; /* set prescaler to get 1 M counts/sec */ LPC_TMR16B0->MR0 = TIME_INTERVALmS * 10; /* Set up 10 mS interval */ #if TIMER_MATCH LPC_TMR16B0->EMR &= ~(0xFF<<4); LPC_TMR16B0->EMR |= ((0x3<<4)|(0x3<<6)); #else /* Capture 0 on rising edge, interrupt enable. */ LPC_TMR16B0->CCR = (0x1<<0)|(0x1<<2); #endif LPC_TMR16B0->MCR = 3;/* Interrupt and Reset on MR0 and MR1 */ /* Enable the TIMER0 Interrupt */ NVIC_EnableIRQ(TIMER_16_0_IRQn); } else if ( timer_num == 1 ) { /* Some of the I/O pins need to be clearfully planned if you use below module because JTAG and TIMER CAP/MAT pins are muxed. */ LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8); LPC_IOCON->PIO1_8 &= ~0x2F;/* Timer1_16 I/O config */ LPC_IOCON->PIO1_8 |= 0x01;/* Timer1_16 CAP0 */ LPC_IOCON->PIO1_9 &= ~0x07; LPC_IOCON->PIO1_9 |= 0x01;/* Timer1_16 MAT0 */ LPC_IOCON->PIO1_10 &= ~0x07; LPC_IOCON->PIO1_10 |= 0x02;/* Timer1_16 MAT1 */ timer16_1_counter = 0; timer16_1_capture = 0; LPC_TMR16B1->PR = MHZ_PRESCALE; /* set prescaler to get 1 M counts/sec */ LPC_TMR16B1->MR0 = TIME_INTERVALmS * 10; /* Set up 10 mS interval */ #if TIMER_MATCH LPC_TMR16B1->EMR &= ~(0xFF<<4); LPC_TMR16B1->EMR |= ((0x3<<4)|(0x3<<6)); #else LPC_TMR16B1->CTCR = 0; //use timer mode /* Capture 0 on rising edge, interrupt enable. */ LPC_TMR16B1->CCR = (0x1<<0)|(0x1<<2); #endif LPC_TMR16B1->MCR = 3;/* Interrupt and Reset on MR0 and MR1 */ /* Enable the TIMER1 Interrupt */ NVIC_EnableIRQ(TIMER_16_1_IRQn); } return; } |
0x184 0x2288 0xc7 0xe04 0x17bc |
#define MHZ_PRESCALE (TIMER_CLOCKFREQ/1000000)-1 // = 48-1 |
(TIMER_CLOCKFREQ/100000)-1 =479 |
(TIMER_CLOCKFREQ/10000)-1 =4799 |
(TIMER_CLOCKFREQ/100000)-1 =479 |
(TIMER_CLOCKFREQ/10000)-1 =4799 |
#define MHZ_PRESCALE (TIMER_CLOCKFREQ/1000000) |
#define MHZ_PRESCALE (TIMER_CLOCKFREQ/1000000)-1 |
#define MHZ_PRESCALE (TIMER_CLOCKFREQ/1000000) |
#define MHZ_PRESCALE (TIMER_CLOCKFREQ/1000000)-1 |