How to calculate Pulse width using xmega256 TIMER in Capture Mode

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am trying to implement Atmel example (related to avr1306 - using the XMEGA timers counters) about using TIMER0 Capture Mode to measure duty and frequency, you can find the full example at Github.

 

At (CCA ISR) we have the following code:

 

uint16_t thisCapture = TC_GetCaptureA( &TCC0 );//read CCA value

/*  Save total period based on rising edge and reset counter. */
if ( thisCapture & 0x8000 ) {
    totalPeriod = thisCapture & 0x7FFF;
    TC_Restart( &TCC0 );
}
 /* Calculate duty cycle based on time from reset and falling edge. */
else {
    highPeriod = thisCapture;
}

dutyCycle = ( ( ( highPeriod * 100 ) / totalPeriod ) + dutyCycle ) / 2;
frequency = ( ( ( F_CPU / CPU_PRESCALER ) / totalPeriod ) + frequency ) / 2;

}

Does the if condition exactly determine the rising and falling edge? please if any one explain the code, and how does the "dutyCycle" been calculated?

is it logical to do the calculation in ISR routine?

Last Edited: Sat. Sep 21, 2019 - 08:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, not a good idea to do such calculation within an ISR. And, especially not in a function called from an ISR.

 

Jim

 

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you Jim, i have been confused, especially because it is from Atmel itself, so i have to set a flags in ISR and in the while loop i do the calculation , isn't it?

what is your thought about the calculation of duty cycle?