I´m trying to measure the frequency and duty cycle from a PWM signal, my code works OK but when the PWM signal is not active (0Hz 0%) the values of Freq_C4 and DutyCycle_PC4 still reminder the values of the last PWM signal, How can I reset these values and detect when a signal is no longer received?
Code:
volatile uint32_t Freq_PC4;
volatile uint32_t DutyCycle_PC4;
void PWM1_DutyCycle()
{
TimerIntClear(WTIMER0_BASE, TIMER_CAPA_EVENT);
if(Count_PC4==0)//coming raising edge
{
LastStart_PC4 = Start_PC4;
Start_PC4= TimerValueGet(WTIMER0_BASE, TIMER_A);
TimerControlEvent(WTIMER0_BASE, TIMER_A, TIMER_EVENT_NEG_EDGE);//the next time capture falling edge
if(Start_PC4 > LastStart_PC4){CycleTime_PC4 = (Start_PC4 - LastStart_PC4);}//convert to micro seconds
else{CycleTime_PC4 = (TimerLoadGet((WTIMER0_BASE), TIMER_A) - LastStart_PC4 + Start_PC4);}
//4M = 0x3D0900, 40M = 0x2625A00. Original calculation: 0x3D0900/(Start_PC4 - LastStart_PC4) substituted to be able to round the number before truncating it to be stored in an integer
Freq_PC4 = (0x3D0900 + (CycleTime_PC4/ 2))/CycleTime_PC4;
Count_PC4= 1;
return;
}
if(Count_PC4==1)//coming falling edge
{
End_PC4= TimerValueGet(WTIMER0_BASE, TIMER_A);//the next time capture raising edge
TimerControlEvent(WTIMER0_BASE, TIMER_A, TIMER_EVENT_POS_EDGE);
if(End_PC4 > Start_PC4){OnTime_PC4 = (End_PC4 - Start_PC4);}//convert to micro seconds
else{OnTime_PC4 = (TimerLoadGet((WTIMER0_BASE), TIMER_A) - Start_PC4 + End_PC4);}
DutyCycle_PC4 = (OnTime_PC4*100 + (CycleTime_PC4/ 2))/CycleTime_PC4;
Count_PC4= 0;
}
}