1)比较一个电机的当前位置和目标位置,如果它们相同,则将isMoving标志设置为false并返回。如果它们不同,则将isMoving标志设置为true。
2)如果目标位置大于当前位置,则向前移动一步,然后增加当前位置。
3)如果目标位置小于当前位置,则向后移动一步,然后减少当前位置。
以下是代码:
void _ISR _NOPSV _T4Interrupt(void)
{
static char StepperIndex1 = 'A';
if(Device1.statusStr.CurrentPosition == Device1.statusStr.TargetPosition)
{
Device1.statusStr.IsMoving = 0;
// Do Nothing
}
else if (Device1.statusStr.CurrentPosition > Device1.statusStr.TargetPosition)
{
switch (StepperIndex1) // MOVE OUT
{
case 'A':
SetMotor1PosB();
StepperIndex1 = 'B';
break;
case 'B':
SetMotor1PosC();
StepperIndex1 = 'C';
break;
case 'C':
SetMotor1PosD();
StepperIndex1 = 'D';
break;
case 'D':
default:
SetMotor1PosA();
StepperIndex1 = 'A';
break;
}
Device1.statusStr.CurrentPosition--;
Device1.statusStr.IsMoving = 1;
}
else
{
switch (StepperIndex1) // MOVE IN
{
case 'A':
SetMotor1PosD();
StepperIndex1 = 'D';
break;
case 'B':
SetMotor1PosA();
StepperIndex1 = 'A';
break;
case 'C':
SetMotor1PosB();
StepperIndex1 = 'B';
break;
case 'D':
default:
SetMotor1PosC();
StepperIndex1 = 'C';
break;
}
Device1.statusStr.CurrentPosition++;
Device1.statusStr.IsMoving = 1;
}
_T4IF = 0; // Clear the Timer 4 Interrupt Flag.
}
当接收到移动请求时,目标位置在主程序循环中设置。SetMotorPos行只是打开/关闭特定端口引脚的宏。
我的问题是:有没有办法提高这段代码的效率?如果位置是16位整数,则该代码运行良好,但如果是32位整数,则处理过程太多。该设备必须与PC通信而不会出现迟疑,如此编写会有明显的性能损失。我真的只需要18位数学运算,但我不知道是否有简单的方法!任何建设性的输入/建议都将不胜感激。