Content originally posted in LPCWare by emielzij on Wed Dec 17 09:03:21 MST 2014 I have an external clock source that drives the counter0 of the LPC11C24. If the counter value matches the match register 0 (in the code forced to a value 10 for testing purposes) then the counter should reset to 0 and the code should jump to an isr.
Here is my code:
#define timer0match 10//0-65535
#include "LPC11xx.h"
void timer0init(void)
{
LPC_IOCON->PIO0_2 |= (1 << 1);//Selects function CT16B0_CAP0
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 7);//Enable clock for 16-bit counter/timer0
LPC_TMR16B0->CTCR |= (1 << 0);//TC incremented on rising edge CAP input (Selected)
//CAP0 as clock source, default CAP0
//Configure CT16B0_MAT0 (PIO0_8) as match function for external measurements
LPC_GPIO0->DIR |= (1 << 8); //Set PIO0_8 as output
LPC_TMR16B0->EMR|= 0x00000030;//EMC0, Toggle the corresponding External Match bit/output
LPC_IOCON->PIO0_8|= (1 << 1); //Selects function CT16B0_MAT0
LPC_TMR16B0->MR0 = timer0match;//Set match value
LPC_TMR16B0->MCR|= (1 << 0); //Interrupt if TC==MR0
LPC_TMR16B0->MCR|= (1 << 1); //Reset TC to 0 if TC==MR0
LPC_TMR16B0->TCR |= (1 << 0);//Enable timer 0
//Reset timer 0
LPC_TMR16B0->TCR |= (1 << 1);
LPC_TMR16B0->TCR &= ~(1 << 1);
NVIC_EnableIRQ(TIMER_16_0_IRQn);
}
int main(void)
{
int i=0;
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<6);//GPIO CLOCK ENABLE
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<16);//Enable IOCON
LPC_GPIO0->DIR|= (1 << 3);//OUTPUT
LPC_IOCON->PIO0_3&= ~(1 << 3); //Disable pull up
timer0init();
while(1)
{
LPC_GPIO0->DATA|= (1 << 3);
for(i=0; i<100; i++);
LPC_GPIO0->DATA &= ~(1 << 3); //toggle pin
for(i=0; i<100; i++);
}
return 0 ;
}
void TIMER_16_0_IRQHandler(void)
{
LPC_TMR16B0->IR |= (1 << 0);//Clear interrupt flag by writing a 1
}
The PIO0_3 is not toggled in the while(1) loop, the program get stuck in the ISR I think. If I remove NVIC_EnableIRQ(TIMER_16_0_IRQn); from the code, the program runs fine. The hardware interrupt, toggle a pin at a match works fine in both cases.
Am I doing something wrong with the interrupt clearing?