if(EQep1Regs.QFLG.bit.UTO == 1) // If unit timeout (one 1000Hz period) 1ms
{
// The following lines calculate position = (x2-x1)/4000 (position in 15 revolution)
pos16bval = (Uint16)EQep1Regs.QPOSLAT; // Latched POSCNT value
tmp = (long)((long)pos16bval * (long)sMotorPosSpeed.mech_scaler); // Q0*Q26 = Q26
tmp &= 0x03FFF000;//0x04000000;//
tmp = (int)(tmp >> 11); // Q26 -> Q15
tmp &= 0x7FFF;//0x8000;//
newp = _IQ15toIQ(tmp);
oldp = sMotorPosSpeed.oldpos;
if (sMotorPosSpeed.DirectionQep == 0) // POSCNT is counting down
{
if (newp > oldp)
Tmp1 = -(_IQ(1) – newp + oldp); // x2-x1 should be negative
else
Tmp1 = newp – oldp;
}
else if (sMotorPosSpeed.DirectionQep == 1) // POSCNT is counting up
{
if (newp < oldp)
Tmp1 = _IQ(1) + newp – oldp;
else
Tmp1 = newp – oldp; // x2-x1 should be positive
}
if (Tmp1 > _IQ(1))
sMotorPosSpeed.Speed_fr = _IQ(1);
else if (Tmp1 < _IQ(-1))
sMotorPosSpeed.Speed_fr = _IQ(-1);
else
sMotorPosSpeed.Speed_fr = Tmp1;
// Update the old position
sMotorPosSpeed.oldpos = newp;
// Change motor speed from pu value to rpm value (Q15 -> Q0)
// Q0 = Q0*GLOBAL_Q => _IQXmpy(), X = GLOBAL_Q
sMotorPosSpeed.SpeedRpm_fr = _IQmpy(sMotorPosSpeed.BaseRpm,sMotorPosSpeed.Speed_fr);
EQep1Regs.QCLR.bit.UTO = 1; // Clear interrupt flag
}
//**** Low-speed computation using QEP capture counter ****//
if(EQep1Regs.QEPSTS.bit.UPEVNT == 1) // Unit position event
{
if(EQep1Regs.QEPSTS.bit.COEF == 0) // No Capture overflow 1->捕获定时器溢出
temp1 = (Uint32)EQep1Regs.QCPRDLAT; // temp1 = t2 – t1
else // Capture overflow, saturate the result
temp1 = 0xFFFF;
sMotorPosSpeed.Speed_pr = _IQdiv(sMotorPosSpeed.SpeedScaler,temp1); // p->Speed_pr = p->SpeedScaler/temp1
Tmp1 = sMotorPosSpeed.Speed_pr;
if (Tmp1 > _IQ(1))
sMotorPosSpeed.Speed_pr = _IQ(1);
else
sMotorPosSpeed.Speed_pr = Tmp1;
// Convert p->Speed_pr to RPM
if (sMotorPosSpeed.DirectionQep == 0) // Reverse direction = negative
{
sMotorPosSpeed.Speed_pr = -sMotorPosSpeed.Speed_pr;
sMotorPosSpeed.SpeedRpm_pr = _IQmpy(sMotorPosSpeed.BaseRpm,sMotorPosSpeed.Speed_pr); // Q0 = Q0 * GLOBAL_Q => _IQXmpy(), X = GLOBAL_Q
}
else // Forward direction = positive
{
sMotorPosSpeed.Speed_pr = sMotorPosSpeed.Speed_pr;
sMotorPosSpeed.SpeedRpm_pr = _IQmpy(sMotorPosSpeed.BaseRpm,sMotorPosSpeed.Speed_pr); // Q0 = Q0 * GLOBAL_Q => _IQXmpy(), X = GLOBAL_Q
}
EQep1Regs.QEPSTS.all = 0x88; // Clear Unit position event flag,Clear overflow error flag
}
怎样分辨高速和低速,当UPEVNT时间发生时,代表什么?
Eric Ma:
你高低速的判断范围是多少?直接看速度反馈不可以吗?
ERIC