📄 motor.c
字号:
else {
data = MotorFinalStep[channel]-MotorCurStep[channel];
MotorSpeedIndex[channel] = abs(data)>>10;
}
MotorStatus[channel] = ON;
TC0 = TCNT - 20;
TIE |= 1;
}
else if(flag == STEPLOW)
{
MotorFinalStep[channel] = MotorMinStep[channel];
MotorMoveStatus[channel] = CONST;
if(resetback0) {
MotorSpeedIndex[channel] = 0;
}
else {
data = MotorFinalStep[channel]-MotorCurStep[channel];
MotorSpeedIndex[channel] = abs(data)>>10;
}
MotorStatus[channel] = ON;
TC0 = TCNT - 20;
TIE |= 1;
}
else if(flag == STEPNOT)
{
MotorStatus[channel] = OFF;
}
else
{
}
}
}
else if(channel == TEMP)
{
if(MotorStatus[channel] == OFF)
{
flag = check_pos(channel,position);
if(flag == OK)
{
MotorFinalStep[channel] = position;
MotorMoveStatus[channel] = CONST;
if(resetback0) {
MotorSpeedIndex[channel] = 0;
}
else {
data = MotorFinalStep[channel]-MotorCurStep[channel];
MotorSpeedIndex[channel] = abs(data)>>10;
}
MotorStatus[channel] = ON;
TC1 = TCNT - 20;
TIE |= 2;
}
else if(flag == STEPHIG)
{
MotorFinalStep[channel] = MotorMaxStep[channel];
MotorMoveStatus[channel] = CONST;
if(resetback0) {
MotorSpeedIndex[channel] = 0;
}
else {
data = MotorFinalStep[channel]-MotorCurStep[channel];
MotorSpeedIndex[channel] = abs(data)>>10;
}
MotorStatus[channel] = ON;
TC1 = TCNT - 20;
TIE |= 2;
}
else if(flag == STEPLOW)
{
MotorFinalStep[channel] = MotorMinStep[channel];
MotorMoveStatus[channel] = CONST;
if(resetback0) {
MotorSpeedIndex[channel] = 0;
}
else {
data = MotorFinalStep[channel]-MotorCurStep[channel];
MotorSpeedIndex[channel] = abs(data)>>10;
}
MotorStatus[channel] = ON;
TC1 = TCNT - 20;
TIE |= 2;
}
else if(flag == STEPNOT)
{
MotorStatus[channel] = OFF;
}
else
{
}
}
}
else
{}
if((flag==OK) || (flag==STEPHIG) || (flag==STEPLOW))
{
return OK;
}
else
{
return NOTOK;
}
}
/****************************************************************************************
@函数名称 : void motor_update(char channel, char dir);
@参数 : char channel, char dir
@返回值 : NONE
@描述 : 更新步数
@最后编辑时间 : 2006-02-23
@版本 : V1.0
@修改
*****************************************************************************************/
void motor_update(char channel, char dir)
{
volatile unsigned char ctemp,*motor_channel;
int temp,itemp;
itemp=MotorFinalStep[channel]-MotorCurStep[channel];
if (itemp!=0)
{
if (itemp<0) MotorCurStep[channel]--;
else if (itemp>0) MotorCurStep[channel]++;
motor_channel = &MCDC0 + channel*2;
temp = MotorCurStep[channel];
asm {
ldd temp;
ldx #6
idiv
stab ctemp
}
if(dir == CLOCKWISE)
{
if(ctemp==0)
{
MCCTL1_RECIRC = 0;
*motor_channel &= ~S0_DTC; //;Duty cycle channel 0
*(motor_channel+1) = 64;
MCCTL1_RECIRC = 0;
*(motor_channel+2) &= ~S1_DTC; //;Duty cycle channel 1
*(motor_channel+3) =0;
}
else if(ctemp==1)
{
MCCTL1_RECIRC = 0;
*motor_channel &= ~S0_DTC; //;Duty cycle channel 0
*(motor_channel+1) = 64;
*(motor_channel+2) &= ~S1_DTC; //;Duty cycle channel 1
*(motor_channel+3) = 64;
}
else if(ctemp==2)
{
MCCTL1_RECIRC = 0;
*motor_channel &= ~S0_DTC; //;Duty cycle channel 0
*(motor_channel+1) = 0;
*(motor_channel+2) &= ~S1_DTC; //;Duty cycle channel 1
*(motor_channel+3) = 64;
}
else if(ctemp==3)
{
MCCTL1_RECIRC = 1;
*motor_channel |= S0_DTC; //;Duty cycle channel 0
*(motor_channel+1) = 64;
MCCTL1_RECIRC = 1;
*(motor_channel+2) |= S1_DTC; //;Duty cycle channel 1
*(motor_channel+3) = 0;
}
else if(ctemp==4)
{
MCCTL1_RECIRC = 1;
*motor_channel |= S0_DTC; //;Duty cycle channel 0
*(motor_channel+1) = 64;
*(motor_channel+2) |= S1_DTC; //;Duty cycle channel 1
*(motor_channel+3) = 64;
}
else if(ctemp==5)
{
MCCTL1_RECIRC = 1;
*motor_channel |= S0_DTC; //;Duty cycle channel 0
*(motor_channel+1) = 0;
*(motor_channel+2) |= S1_DTC; //;Duty cycle channel 1
*(motor_channel+3) = 64;
}
}
}
else
{
MotorStatus[channel]=OFF;
}
}
/****************************************************************************
* motor_micro_update(char channel, char dir) -
*
* Description : Control motor drivers PWM value and polarity
* :
* Example : N/A
* Input : motor channel, direction
* Output : MotorCurStyep[?]
* Modify : Motor driver PWM value and polarity
* Return : N/A
****************************************************************************/
void motor_micro_update(char channel, char dir)
{
volatile unsigned char ctemp,*motor_channel;
int temp,itemp;
itemp = MotorFinalStep[channel] - MotorCurStep[channel];
if (itemp!=0)
{
if (itemp<0) MotorCurStep[channel]--;
else if (itemp>0) MotorCurStep[channel]++;
motor_channel= &MCDC0 + channel*2;
temp = MotorCurStep[channel];
asm
{
ldd temp;
ldx #STEP6
idiv
stab ctemp
} //取余数运算
if(dir == CLOCKWISE)
{
if((ctemp<STEP01))
{
MCCTL1_RECIRC = 0;
*motor_channel &= ~S0_DTC; //;Duty cycle channel 0
MCCTL1_RECIRC = 1;
*(motor_channel+2) |= S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP01)&&(ctemp<STEP1))
{
MCCTL1_RECIRC = 0;
*motor_channel &= ~S0_DTC; //;Duty cycle channel 0
*(motor_channel+2) &= ~S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP1)&&(ctemp<STEP2))
{
MCCTL1_RECIRC = 0;
*motor_channel &= ~S0_DTC; //;Duty cycle channel 0
*(motor_channel+2) &= ~S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP2)&&(ctemp<STEP23))
{
MCCTL1_RECIRC = 0;
*motor_channel &= ~S0_DTC; //;Duty cycle channel 0
*(motor_channel+2) &= ~S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP23)&&(ctemp<STEP3))
{
MCCTL1_RECIRC = 1;
*motor_channel |= S0_DTC; //;Duty cycle channel 0
MCCTL1_RECIRC = 0;
*(motor_channel+2) &= ~S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP3)&&(ctemp<STEP34))
{
MCCTL1_RECIRC = 1;
*motor_channel |= S0_DTC; //;Duty cycle channel 0
MCCTL1_RECIRC = 0;
*(motor_channel+2) &= ~S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP34)&&(ctemp<STEP4))
{
MCCTL1_RECIRC = 1;
*motor_channel |= S0_DTC; //;Duty cycle channel 0
*(motor_channel+2) |= S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP4)&&(ctemp<STEP5))
{
MCCTL1_RECIRC = 1;
*motor_channel |= S0_DTC; //;Duty cycle channel 0
*(motor_channel+2) |= S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP5)&&(ctemp<STEP56))
{
MCCTL1_RECIRC = 1;
*motor_channel |= S0_DTC; //;Duty cycle channel 0
*(motor_channel+2) |= S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP56)&&(ctemp<STEP6))
{
MCCTL1_RECIRC = 0;
*motor_channel &= ~S0_DTC; //;Duty cycle channel 0
MCCTL1_RECIRC = 1;
*(motor_channel+2) |= S1_DTC; //;Duty cycle channel 1
}
}
else
{
if((ctemp<STEP01))
{
MCCTL1_RECIRC = 0;
*(motor_channel+2) &= ~S0_DTC; //;Duty cycle channel 0
MCCTL1_RECIRC = 1;
*motor_channel |= S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP01)&&(ctemp<STEP1))
{
MCCTL1_RECIRC = 0;
*(motor_channel+2) &= ~S0_DTC; //;Duty cycle channel 0
*motor_channel &= ~S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP1)&&(ctemp<STEP2))
{
MCCTL1_RECIRC = 0;
*(motor_channel+2) &= ~S0_DTC; //;Duty cycle channel 0
*motor_channel &= ~S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP2)&&(ctemp<STEP23))
{
MCCTL1_RECIRC = 0;
*(motor_channel+2) &= ~S0_DTC; //;Duty cycle channel 0
*motor_channel &= ~S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP23)&&(ctemp<STEP3))
{
MCCTL1_RECIRC = 1;
*(motor_channel+2) |= S0_DTC; //;Duty cycle channel 0
MCCTL1_RECIRC = 0;
*motor_channel &= ~S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP3)&&(ctemp<STEP34))
{
MCCTL1_RECIRC = 1;
*(motor_channel+2) |= S0_DTC; //;Duty cycle channel 0
MCCTL1_RECIRC = 0;
*motor_channel &= ~S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP34)&&(ctemp<STEP4))
{
MCCTL1_RECIRC = 1;
*(motor_channel+2) |= S0_DTC; //;Duty cycle channel 0
*motor_channel |= S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP4)&&(ctemp<STEP5))
{
MCCTL1_RECIRC = 1;
*(motor_channel+2) |= S0_DTC; //;Duty cycle channel 0
*motor_channel |= S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP5)&&(ctemp<STEP56))
{
MCCTL1_RECIRC = 1;
*(motor_channel+2) |= S0_DTC; //;Duty cycle channel 0
*motor_channel |= S1_DTC; //;Duty cycle channel 1
}
else if((ctemp>=STEP56)&&(ctemp<STEP6))
{
MCCTL1_RECIRC = 0;
*(motor_channel+2) &= ~S0_DTC; //;Duty cycle channel 0
MCCTL1_RECIRC = 1;
*motor_channel |= S1_DTC; //;Duty cycle channel 1
}
}
*(motor_channel+1) = SinTbl4[ctemp];
*(motor_channel+3) = CosTbl4[ctemp];
}
else
{
MotorStatus[channel]=OFF;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -