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?