I want to incorporate SVGEN_DQ and FC_PWM_DRV modules into my custom code for 2xpmMotor control application and I was examining f2803xpwm.c and f2803xpwm.h found under TI_F28xxx_SysSW\~SupportFiles\lib\IQmath\doc.
Knowing that FC_PWM_DRV module produces compare values for CMP registers by receiving duty cycles produced by svgen_dq module, I don't quite understand how the following piece of code, taken from f2803xpwm,c works:
void F280X_PWM_Update(PWMGEN *p)
{
/*... some code ... */
// Compute the timer period (Q0) from the period modulation input (Q15)
Tmp = (int32)p->PeriodMax*(int32)p->MfuncPeriod; // Q15 = Q0*Q15
MPeriod = (int16)(Tmp>>16) + (int16)(p->PeriodMax>>1); // Q0 = (Q15->Q0)/2 + (Q0/2)
/* ... some code ... */
}
what I understand is that PWM half period is originally in Q0 format, and by multiplying it by the period scaler we turn the product into a Q15 format value. in the second line, we change this Q15 value to Q0 again and divide it by two ( >> 16 operand) and add it by PeriodMax/2. What is the purpose of transforming from Q0 to Q15 and then back to Q0? and why do we add PeriodMax/2 in the second line to the Tmp? I don't understand the logic of the code.