It’s mostly doing what I want, but either I’m using the interrupt wrong or I’m having some analog issues with the signal (RC radio Receiver output, PWM @ ~50Hz). See unwanted spikes in plot which correspond to PWM frame rate, so they are a measure between a falling and a rising edge, instead of a rising and a falling edge.
What do think?
main, followed by timer/capture interrupt:
int main(void)
{
// hack NVIC_SetPriorityGrouping(0x05);
// This is for running out of ROM
NVIC_SetVTOR(0x00000000);
TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL;
TIM_ConfigStruct.PrescaleValue = 1;
// use channel 0, CAPn.0
TIM_CaptureConfigStruct.CaptureChannel = 0;
// Enable capture on CAPn.0 rising edge
TIM_CaptureConfigStruct.RisingEdge = ENABLE;
// Enable capture on CAPn.0 falling edge
TIM_CaptureConfigStruct.FallingEdge = ENABLE;
// Generate capture interrupt
TIM_CaptureConfigStruct.IntOnCaption = ENABLE;
// Set configuration for Tim_config and Tim_MatchConfig
TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct);
TIM_ConfigCapture(LPC_TIM0, &TIM_CaptureConfigStruct);
TIM_ResetCounter(LPC_TIM0);
/* preemption = 1, sub-priority = 1 */
NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01));
init_in_main();
LED1_on();
comm_init();
SysTick_Config(SystemCoreClock/1000 - 1); /* Generate interrupt each 1 ms */
//KB_DEBUG_FUNC();
/* Loop forever.and_forever->andforever */
while (1)
{
/* Enable interrupt for timer 0 */
NVIC_EnableIRQ(TIMER0_IRQn);
first_capture = TRUE;done=FALSE;capture=0;
// To start timer 0
TIM_Cmd(LPC_TIM0,ENABLE);
counter++;
if (counter == 1000)
{
PWM_debug = (int) TIM_GetCaptureValue(LPC_TIM0,0);
itoa(PWM_debug, PWM_char);
counter = 0;
comm_puts("from TIM_GetCaptureValue() \r\n");
comm_puts(PWM_char);
comm_puts(" \r\n");
PWM_debug2 = (int) capture;
itoa(PWM_debug2, PWM_char);
comm_puts("from 'capture' \r\n");
comm_puts(PWM_char);
comm_puts(" \r\n");
}
//KB_DEBUG = 2;
}
return 1;
}
void TIMER0_IRQHandler(void)
{
// hack
KB_DEBUG = 4;
if (TIM_GetIntCaptureStatus(LPC_TIM0,0))
{
TIM_ClearIntCapturePending(LPC_TIM0,0);
if(first_capture==TRUE)
{
TIM_Cmd(LPC_TIM0,DISABLE);
TIM_ResetCounter(LPC_TIM0);
TIM_Cmd(LPC_TIM0,ENABLE);
count++;
if(count==5)first_capture=FALSE; //stable
}
else
{
count=0; //reset count for next use
done=TRUE;
capture = TIM_GetCaptureValue(LPC_TIM0,0);
}
}
}