AnsweredAssumed Answered

How do I detect both rising and falling edge interrupt with input capture?

Question asked by exgreyfox on Oct 7, 2015
Latest reply on Dec 16, 2016 by Mark Butcher

I am trying to measure the pulse width of an input signal at TPM1_CH0 but I am having no luck. I am configuring TPM1_C0SC register to interrupt on both rising and falling edges as such:

 

/* Input Capture mode rising and falling edge interrupt */
  TPM1_C0SC |= TPM_CnSC_ELSA_MASK; 
  TPM1_C0SC |= TPM_CnSC_ELSB_MASK; 
  TPM1_C0SC &= ~(TPM_CnSC_MSA_MASK);
  TPM1_C0SC &= ~(TPM_CnSC_MSB_MASK); 
  TPM1_C0SC |= TPM_CnSC_CHIE_MASK;

 

And then inside main.c I execute the following code:

 

enable_irq (INT_TPM1-16);     /* Enable Timer1 interrupt in NVIC */
TPM1_SC |= TPM_SC_CMOD(0);    /* Enable the TPM1_CH0 Timer */
TPM1_CNT = 0;                 /* Reset TPM1 */
DelayMs_NoIRQ(19);            /* delay 19ms */              
TPM1_SC |= TPM_SC_CMOD        /* Disable the TPM1_CH0 Timer */
disable_irq (INT_TPM1-16);    /* Disable Timer1 interrupt in NVIC */
val = falling_val - rising_val; /* calculate pulse width of input signal */

 

And inside the ISR i attempt to detect the logic level of the input signal to and decide whether it is falling or rising edge:

 

void FTM1_IRQHandler(void)
{
  if(TPM1_C0SC & TPM_CnSC_CHF_MASK)
  {  
       TPM1_C0SC |= TPM_CnSC_CHF_MASK;

       if(GPIOA_PDIR & GPIO_PTA12_LOC)
       {
            rising_val = TPM1_C0V;
       }
       if(!(GPIOA_PDIR & GPIO_PTA12_LOC))
       {
            falling_val = TPM1_C0V;
       }
  }
  return;
}

 

But I am not having any luck as "val" returns wrong value. I am using KL25Z freedom board.

Outcomes