近期在做一个交流电机的控制程序,参考TI公司提供的代码库,发现了在f281xpwm.c文件中的一个费解语句,如下:
// 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)
EvaRegs.T1PR = MPeriod;
// Compute the compare 1 (Q0) from the PWM 1&2 duty cycle ratio (Q15)
Tmp = (int32)MPeriod*(int32)p->MfuncC1; // Q15 = Q0*Q15
EvaRegs.CMPR1 = (int16)(Tmp>>16) + (int16)(MPeriod>>1); // Q0 = (Q15->Q0)/2 + (Q0/2)
// Compute the compare 2 (Q0) from the PWM 3&4 duty cycle ratio (Q15)
Tmp = (int32)MPeriod*(int32)p->MfuncC2; // Q15 = Q0*Q15
EvaRegs.CMPR2 = (int16)(Tmp>>16) + (int16)(MPeriod>>1); // Q0 = (Q15->Q0)/2 + (Q0/2)
// Compute the compare 3 (Q0) from the PWM 5&6 duty cycle ratio (Q15)
Tmp = (int32)MPeriod*(int32)p->MfuncC3; // Q15 = Q0*Q15
EvaRegs.CMPR3 = (int16)(Tmp>>16) + (int16)(MPeriod>>1); // Q0 = (Q15->Q0)/2 + (Q0/2)
蓝色部分的语句让我很费解,经过查找资料和自己的理解,总算看明白了。现解释如下:
这条语句就是实现上述功能的。Tmp首先除于2,范围变为-1500~1500,然后在偏移1500(+PeriodMax>>1),此时范围是0~3000,实现了上述转换。
v->Ta = _IQmpy(_IQ(2.0),(v->Ta-_IQ(0.5)));
v->Tb = _IQmpy(_IQ(2.0),(v->Tb-_IQ(0.5)));
v->Tc = _IQmpy(_IQ(2.0),(v->Tc-_IQ(0.5)));
该条语句的作用,PWM周期PeriodMax乘以其占空比(低电平占周期的百分数),得到比较寄存器的值,但因为此时的占空比经过范围变换处理,所以还不是真正的比较寄存器的值