ct_test/Keil_C/Apps/motor.c

1005 lines
22 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "motor.h"
#define STEP_LIN 16 //直行程步长单位数值(脉冲数)
#define STEP_ROT 20 //角行程步长单位数值(脉冲数)
#define DATA_LEN 10 //角行程步长单位数值(脉冲数)
bit Travle_Flag = 0; //0 直 1 角
char xdata Motor_Run = 0; //0 停止 1 运行 2 运行到起始点 3 运行到结束点
char xdata Run_Mode = 0; //0 点动 1 方案一 2 方案二
unsigned int xdata Run_Step = 0; //电机运行步长
unsigned int xdata Run_Inter = 0; //电机运行间隔时长
unsigned int xdata Run_Stop = 0; //到“结束点”后,停止时间
unsigned int xdata Run_mm = 0; //行进长度mm/转动角度(°)
unsigned int xdata Run_num = 0; //角行程电机转动圈数
unsigned int xdata ct_num = 0; //磁条长度
bit mov_flag = 0; //脉冲标志
bit send_flag = 0; //发送标志
bit seat_flag = 0; //位置标志
bit motor_dire = 1; //电机转动方向
bit flag = 0;
//步骤
unsigned char xdata Runmotor_step = 0;
//次数
unsigned int xdata Runmotor_Nums = 0;
//电机启动
void motor_start(void)
{
GPIO_Init(GPIO1, GPIO_PIN_7,GPIO_MODE_OUT_PP);//脉冲引脚设置为输出模式,电机可收到脉冲信号
GPIO_WriteHigh(GPIO1,GPIO_PIN_7);
motor_data[1] = 0x01;//电机启动对应数据01
}
//电机停止
void motor_stop(void)
{
GPIO_WriteHigh(GPIO1,GPIO_PIN_7);
GPIO_Init(GPIO1, GPIO_PIN_7,GPIO_MODE_IN_HI);//脉冲引脚设置为输入模式,电机无法收到脉冲信号
motor_data[1] = 0x00;//电机停止对应数据00
}
//正转
void FWD(void)
{
GPIO_WriteLow(GPIO1,GPIO_PIN_6);//引脚电平置低,电机正转
}
//反转
void REV(void)
{
GPIO_WriteHigh(GPIO1,GPIO_PIN_6);//引脚电平置高,电机反转
}
//清除电机标记
void ClrRunmotorStep(void)
{
//步骤
Runmotor_step = 0;
//次数
Runmotor_Nums = 0;
//脉冲标记清0
mov_flag = 0;
//发送标记清0
send_flag = 0;
//位置标记清0
seat_flag = 0;
}
//处理马达运行
void Deal_Motor(void)
{
//判断直行程还是角行程
if(Travle_Flag == 0)//直行程——电机旋转一圈磁条水平位移5mm
{
motor_data[0] = 0x00;//00 直行程 01 角行程
//判断电机停止还是运行,运行到起始位还是结束位
if( Motor_Run == 0)//停止
{
motor_stop();
}
else if(Motor_Run == 1)//运行
{
if(Motor_Run >= 1 && Runmotor_step == 0)
{
Runmotor_step = 1;
}
//判断电机运行方式是点动还是连续,点动为方案三,方案一和方案二为连续
if(Run_Mode == 0)//点动(方案三)
{
motor_data[2] = 0x00;//发送时反馈的数据
mov_step();//点动
}
else if(Run_Mode == 1)//连续(方案一)“步长过大可能会越过限位开关”
{
motor_data[2] = 0x01;//发送时反馈的数据
mov_loop1();//方案一
}
else if(Run_Mode == 2)//连续(方案二)“步长过大可能会越过限位开关”
{
motor_data[2] = 0x02;//发送时反馈的数据
//磁条循环“起始点-结束点-起始点”,一定次数后停在起始点
mov_loop2();//方案二
}
else//初始化
{
motor_stop();
}
}
else if(Motor_Run == 2)//运行到起始位
{
mov_begin();//回到起始位
}
else if(Motor_Run == 3)//运行到结束位
{
mov_end();//移动至结束位
}
else//数据错误
SC_Init();
}
else //角行程——电机旋转一圈磁条旋转4°
{
motor_data[0] = 0x01;//00 直行程 01 角行程
//判断电机停止还是运行,运行到起始位还是结束位
if( Motor_Run == 0)//停止
{
motor_stop();
}
else if(Motor_Run == 1)//运行
{
if(Motor_Run >= 1 && Runmotor_step == 0)
{
Runmotor_step = 1;
}
//判断电机运行方式是点动还是连续,点动为方案三,方案一和方案二为连续
if(Run_Mode == 0)//点动(方案三)
{
motor_data[2] = 0x00;//发送时反馈的数据
mov_step_ang();//电机点动运行
}
else if(Run_Mode == 1)//连续(方案一)
{
motor_data[2] = 0x01;//发送时反馈的数据
mov_loop1_ang();//磁条旋转一圈
}
else if(Run_Mode == 2)//连续(方案二)
{
motor_data[2] = 0x02;//发送时反馈的数据
if(seat_flag == 0)
{
if(GPIO_ReadPin(GPIO1,GPIO_PIN_4) == 0) //判断是否到达限位处
{
motor_stop(); //电机停止
Run_mm = 0;
seat_flag = 1;//位置标记
}
else
{
REV(); //反转
motor_start(); //运行
motor_mov(1); //提供脉冲信号
}
}
else
{
mov_loop2_ang();//磁条旋转一圈
}
}
else//初始化
{
motor_stop();
}
}
else if(Motor_Run == 2 || Motor_Run == 3)//运行到起始位
{
mov_begin();//回到起始位
}
else//数据错误
SC_Init();
}
}
/*****************************************************
*函数名称: motor_mov
*函数功能: 电机速度控制
*参数说明speed 电机速度设定值1为最快5为最慢
*****************************************************/
void motor_mov(unsigned int speed)
{
switch(speed)//控制电机速度,设定有五档速度
{
//脉冲周期2ms
case 1:
{
if(it_1ms_flag) //检查1ms定时标志
{
it_1ms_flag = 0;//定时标志清零
if(mov_flag)
{
mov_flag = 0;
GPIO_WriteHigh(GPIO1,GPIO_PIN_7);//P1.7引脚拉高
}
else
{
mov_flag = 1;
GPIO_WriteLow(GPIO1,GPIO_PIN_7);//P1.7引脚拉低
}
Runmotor_Nums++; //运行次数
}
}
break;
//脉冲周期4ms
case 2:
{
if(it_2ms_flag)//检查2ms定时标志
{
it_2ms_flag = 0;//定时标志清零
if(mov_flag)
{
mov_flag = 0;
GPIO_WriteHigh(GPIO1,GPIO_PIN_7);//P1.7引脚拉高
}
else
{
mov_flag = 1;
GPIO_WriteLow(GPIO1,GPIO_PIN_7);//P1.7引脚拉低
}
Runmotor_Nums++; //运行次数
}
}
break;
//脉冲周期6ms
case 3:
{
if(it_3ms_flag)//检查3ms定时标志
{
it_3ms_flag = 0;//定时标志清零
if(mov_flag)
{
mov_flag = 0;
GPIO_WriteHigh(GPIO1,GPIO_PIN_7);//P1.7引脚拉高
}
else
{
mov_flag = 1;
GPIO_WriteLow(GPIO1,GPIO_PIN_7);//P1.7引脚拉低
}
Runmotor_Nums++; //运行次数
}
}
break;
//脉冲周期8ms
case 4:
{
if(it_4ms_flag)//检查4ms定时标志
{
it_4ms_flag = 0;//定时标志清零
if(mov_flag)
{
mov_flag = 0;
GPIO_WriteHigh(GPIO1,GPIO_PIN_7);//P1.7引脚拉高
}
else
{
mov_flag = 1;
GPIO_WriteLow(GPIO1,GPIO_PIN_7);//P1.7引脚拉低
}
Runmotor_Nums++; //运行次数
}
}
break;
//脉冲周期10ms
case 5:
{
if(it_5ms_flag)//检查5ms定时标志
{
it_5ms_flag = 0;//定时标志清零
if(mov_flag)
{
mov_flag = 0;
GPIO_WriteHigh(GPIO1,GPIO_PIN_7);//P1.7引脚拉高
}
else
{
mov_flag = 1;
GPIO_WriteLow(GPIO1,GPIO_PIN_7);//P1.7引脚拉低
}
Runmotor_Nums++; //运行次数
}
}
break;
default :
break;
}
}
//电机连续运行,方案一(直行程)
void mov_loop1(void)
{
if(Run_Step == 0) return;//步长不能为0
switch(Runmotor_step)
{
case 1 : //电机运行准备
{
FWD(); //正转
motor_dire = 1;
Run_mm = 0;
Run_num = 0;
motor_data[2] = 0x00; //发送时反馈的数据
Runmotor_Nums = 0; //运行次数
mov_flag = 0;
Runmotor_step++;
}
break;
case 2 : //电机运行过程
{
if(GPIO_ReadPin(GPIO0,GPIO_PIN_5) == 0) //光栅信号判断
{
if(it_1s_flag) //检查1ms定时标志
{
it_1s_flag = 0;//定时标志清零
Runmotor_Nums++;
}
if(Runmotor_Nums >= Run_Stop)//时间
{
Runmotor_step++;
motor_data[8] = ((Run_mm * Run_Step) & 0xff00) >> 8;//磁条长度高8位
motor_data[9] = (Run_mm * Run_Step) & 0x00ff; //磁条长度低8位
motor_seat(); //读取电机当前位置
send_set_resp(0xF001, OBJ_DEVICE_ADDR, DATA_LEN, motor_data);//数据发送
}
}
else
{
if(send_flag == 0)
{
send_flag = 1;
motor_seat();//读取电机当前位置
send_set_resp(0xF001, OBJ_DEVICE_ADDR, DATA_LEN, motor_data);//数据发送
}
motor_start(); //运行
motor_mov(1); //运行1个脉冲
Run_mm = 1; //磁条移动距离清0
Runmotor_Nums = 0; //计数清零
Runmotor_step = 6;
}
}
break;
case 3 : //延时
{
if(motor_dire == 1)
{
motor_start(); //运行
motor_mov(1); //运行1个脉冲
if(Runmotor_Nums >= (Run_Step * STEP_LIN))//运行一个步长,脉冲数由驱动器的细分数决定
{
Runmotor_Nums = 0; //计数清零
motor_stop(); //马达停止
motor_data[1] = 0x01; //电机启动对应数据01
motor_data[3] = 0x03; //电机方向——始终
Run_mm++; //磁条移动距离加1
ct_num = Run_mm; //磁条长度
motor_data[8] = ((Run_mm * Run_Step) & 0xff00) >> 8;//磁条长度高8位
motor_data[9] = (Run_mm * Run_Step) & 0x00ff; //磁条长度低8位
Runmotor_step++;
}
}
else
{
motor_start(); //运行
motor_mov(1); //运行1个脉冲
if(Runmotor_Nums >= (Run_Step * STEP_LIN))//运行一个步长,脉冲数由驱动器的细分数决定
{
Runmotor_Nums = 0; //计数清零
motor_stop(); //马达停止
motor_data[1] = 0x01; //电机启动对应数据01
motor_data[3] = 0x04; //电机方向——终始
ct_num = ct_num - 1; //磁条长度
Run_mm = ct_num; //磁条移动距离减1
motor_data[8] = ((Run_mm * Run_Step) & 0xff00) >> 8;//磁条长度高8位
motor_data[9] = (Run_mm * Run_Step) & 0x00ff; //磁条长度低8位
Runmotor_step++;
}
}
}
break;
case 4 : //延时
{
if(it_1ms_flag) //检查1ms定时标志
{
it_1ms_flag = 0;//定时标志清零
Runmotor_Nums++;
}
if(Runmotor_Nums >= Run_Inter)//时间
{
Runmotor_Nums = 0;
Runmotor_step++;
send_flag = 0;
//motor_seat(); //读取电机当前位置
send_set_resp(0xF001, OBJ_DEVICE_ADDR, DATA_LEN, motor_data);//数据发送
}
}
break;
case 5 :
{
if(GPIO_ReadPin(GPIO0,GPIO_PIN_5) == 1 )//光栅信号判断
{
if(flag == 0)
{
Runmotor_Nums = 0;
Runmotor_step = 3;
if(motor_dire == 1)
{
REV();
flag = 1;
motor_dire = 0;
}
else
{
Motor_Run = 2; //运行标记改变,电机回到初始位
}
}
else
{
Runmotor_step = 3;
}
}
else
{
Runmotor_step = 3;
flag = 0 ;
}
}
break;
case 6 :
{
if(GPIO_ReadPin(GPIO1,GPIO_PIN_4) == 0)//起始点限位
{
FWD();//正转
motor_dire = 1;//电机转动方向
}
if(GPIO_ReadPin(GPIO1,GPIO_PIN_5) == 0)//结束点限位
{
REV();//反转
motor_dire = 0;//电机转动方向
}
Runmotor_step = 2;
}
break;
default :
{
}
break;
}
}
//电机连续运行,方案一(角行程)
void mov_loop1_ang(void)
{
if(Run_Step == 0) return;//步长不能为0
switch(Runmotor_step)
{
case 1 : //电机运行准备
{
FWD();//正转
motor_dire = 1;
motor_data[2] = 0x00;//发送时反馈的数据
Runmotor_Nums = 0; //运行次数
mov_flag = 0;
Run_mm = 0;
Run_num = 0;
Runmotor_step++;
}
break;
case 2 : //电机运行过程
{
if(motor_dire == 1)
{
FWD();//正转
motor_start(); //运行
motor_mov(1); //运行1个脉冲
if(Runmotor_Nums >= (Run_Step * STEP_ROT))//运行一个步长,脉冲数由驱动器的细分数决定
{
Runmotor_Nums = 0; //计数清零
motor_stop(); //马达停止
motor_data[1] = 0x01; //电机启动对应数据01
motor_data[3] = 0x03; //电机方向——始终
Run_mm++; //磁条移动距离加1
Run_num = Run_mm * Run_Step;
motor_data[8] = (Run_num & 0xff00) >> 8;//磁条长度高8位
motor_data[9] = Run_num & 0x00ff; //磁条长度低8位
Runmotor_step++;
}
}
else
{
REV();//反转
motor_start(); //运行
motor_mov(1); //运行1个脉冲
if(Runmotor_Nums >= (Run_Step * STEP_ROT))//运行一个步长,脉冲数由驱动器的细分数决定
{
Runmotor_Nums = 0; //计数清零
motor_stop(); //马达停止
motor_data[1] = 0x01; //电机启动对应数据01
motor_data[3] = 0x04; //电机方向——终始
Run_mm--; //磁条移动距离减1
Run_num = Run_mm * Run_Step;
motor_data[8] = (Run_num & 0xff00) >> 8;//磁条长度高8位
motor_data[9] = Run_num & 0x00ff; //磁条长度低8位
Runmotor_step++;
}
}
}
break;
case 3 : //延时
{
if(it_1ms_flag) //检查1ms定时标志
{
it_1ms_flag = 0;//定时标志清零
Runmotor_Nums++;
}
if(Runmotor_Nums >= Run_Inter)//时间
{
Runmotor_Nums = 0;
Runmotor_step++;
//motor_seat(); //读取电机当前位置
send_set_resp(0xF001, OBJ_DEVICE_ADDR, DATA_LEN, motor_data);//数据发送
}
}
break;
case 4 :
{
if(Run_num >= Run_Stop * 10&& motor_dire == 1)//判断磁条是否转动设定的角度
{
motor_dire = 0;
Runmotor_step = 2;
}
else if(Run_num <= 0 && motor_dire == 0)
{
//motor_dire = 1;
Motor_Run = 1;
}
else
{
Runmotor_step = 2;
}
}
break;
default :
{
}
break;
}
}
//电机连续运行,方案二(直行程)
void mov_loop2(void)
{
if(Run_Step == 0) return;//步长不能为0
switch(Runmotor_step)
{
case 1 : //电机运行准备
{
FWD();//正转
motor_dire = 1;
motor_data[2] = 0x00;//发送时反馈的数据
Runmotor_Nums = 0; //运行次数
mov_flag = 0; //
Runmotor_step++;
}
break;
case 2 : //电机运行过程
{
if(GPIO_ReadPin(GPIO0,GPIO_PIN_5) == 0)//光栅信号判断
{
if(it_1s_flag) //检查1ms定时标志
{
it_1s_flag = 0;//定时标志清零
Runmotor_Nums++;
}
if(Runmotor_Nums >= Run_Stop)//时间
{
Runmotor_step++;
motor_data[8] = ((Run_mm * Run_Step) & 0xff00) >> 8;//磁条长度高8位
motor_data[9] = (Run_mm * Run_Step) & 0x00ff; //磁条长度低8位
motor_seat(); //读取电机当前位置
send_set_resp(0xF001, OBJ_DEVICE_ADDR, DATA_LEN, motor_data);//数据发送
}//Run_mm = 1; //磁条移动距离置1
}
else
{
if(send_flag == 0)
{
send_flag = 1;
motor_seat();//读取电机当前位置
send_set_resp(0xF001, OBJ_DEVICE_ADDR, DATA_LEN, motor_data);//数据发送
}
motor_start(); //运行
motor_mov(1); //运行1个脉冲
Run_mm = 1; //磁条移动距离清0
Runmotor_Nums = 0; //计数清零
Runmotor_step = 6;
}
}
break;
case 3 : //延时
{
if(motor_dire == 1)
{
motor_start(); //运行
motor_mov(1); //运行1个脉冲
if(Runmotor_Nums >= (Run_Step * STEP_LIN))//运行一个步长,脉冲数由驱动器的细分数决定
{
Runmotor_Nums = 0; //计数清零
motor_stop(); //马达停止
motor_data[1] = 0x01; //电机启动对应数据01
motor_data[3] = 0x03; //电机方向——始终
Run_mm++; //磁条移动距离加1
ct_num = Run_mm; //磁条长度
motor_data[8] = ((Run_mm * Run_Step) & 0xff00) >> 8;//磁条长度高8位
motor_data[9] = (Run_mm * Run_Step) & 0x00ff; //磁条长度低8位
Runmotor_step++;
}
}
else
{
motor_start(); //运行
motor_mov(1); //运行1个脉冲
if(Runmotor_Nums >= (Run_Step * STEP_LIN))//运行一个步长,脉冲数由驱动器的细分数决定
{
Runmotor_Nums = 0; //计数清零
motor_stop(); //马达停止
motor_data[1] = 0x01; //电机启动对应数据01
motor_data[3] = 0x04; //电机方向——终始
ct_num = ct_num - 1; //磁条长度
Run_mm = ct_num; //磁条移动距离减1
motor_data[8] = ((Run_mm * Run_Step) & 0xff00) >> 8;//磁条长度高8位
motor_data[9] = (Run_mm * Run_Step) & 0x00ff; //磁条长度低8位
Runmotor_step++;
}
}
}
break;
case 4 : //延时
{
if(it_1ms_flag) //检查1ms定时标志
{
it_1ms_flag = 0;//定时标志清零
Runmotor_Nums++;
}
if(Runmotor_Nums >= Run_Inter)//时间
{
Runmotor_Nums = 0;
Runmotor_step++;
send_flag = 0;
//motor_seat(); //读取电机当前位置
send_set_resp(0xF001, OBJ_DEVICE_ADDR, DATA_LEN, motor_data);//数据发送
}
}
break;
case 5 :
{
if(GPIO_ReadPin(GPIO0,GPIO_PIN_5) == 1 )//光栅信号判断
{
if(flag == 0)
{
Runmotor_Nums = 0;
Runmotor_step = 3;
if(motor_dire == 1)
{
REV();
flag = 1;
motor_dire = 0;
}
else
{
FWD();
flag = 1;
motor_dire = 1;
}
}
else
{
Runmotor_step = 3;
}
}
else
{
Runmotor_step = 3;
flag = 0 ;
}
}
break;
case 6 :
{
if(GPIO_ReadPin(GPIO1,GPIO_PIN_4) == 0)//起始点限位
{
FWD();//正转
motor_dire = 1;//电机转动方向
}
if(GPIO_ReadPin(GPIO1,GPIO_PIN_5) == 0)//结束点限位
{
REV();//反转
motor_dire = 0;//电机转动方向
}
Runmotor_step = 2;
}
break;
default :
{
}
break;
}
}
//电机连续运行,方案二(角行程)
void mov_loop2_ang(void)
{
if(Run_Step == 0) return;//步长不能为0
switch(Runmotor_step)
{
case 1 : //电机运行准备
{
FWD();//正转
motor_dire = 1;
motor_data[2] = 0x00;//发送时反馈的数据
Runmotor_Nums = 0; //运行次数
mov_flag = 0;
Runmotor_step++;
}
break;
case 2 : //电机运行过程
{
if(motor_dire == 1)
{
FWD();//正转
motor_start(); //运行
motor_mov(1); //运行1个脉冲
if(Runmotor_Nums >= (Run_Step * STEP_ROT))//运行一个步长,脉冲数由驱动器的细分数决定
{
Runmotor_Nums = 0; //计数清零
motor_stop(); //马达停止
motor_data[1] = 0x01; //电机启动对应数据01
motor_data[3] = 0x03; //电机方向——始终
Run_mm++; //磁条移动距离加1
Run_num = Run_mm * Run_Step;
motor_data[8] = (Run_num & 0xff00) >> 8;//磁条长度高8位
motor_data[9] = Run_num & 0x00ff; //磁条长度低8位
Runmotor_step++;
}
}
else
{
REV();//反转
motor_start(); //运行
motor_mov(1); //运行1个脉冲
if(Runmotor_Nums >= (Run_Step * STEP_ROT))//运行一个步长,脉冲数由驱动器的细分数决定
{
Runmotor_Nums = 0; //计数清零
motor_stop(); //马达停止
motor_data[1] = 0x01; //电机启动对应数据01
motor_data[3] = 0x04; //电机方向——终始
Run_mm--; //磁条移动距离减1
Run_num = Run_mm * Run_Step;
motor_data[8] = (Run_num & 0xff00) >> 8;//磁条长度高8位
motor_data[9] = Run_num & 0x00ff; //磁条长度低8位
Runmotor_step++;
}
}
}
break;
case 3 : //延时
{
if(it_1ms_flag) //检查1ms定时标志
{
it_1ms_flag = 0;//定时标志清零
Runmotor_Nums++;
}
if(Runmotor_Nums >= Run_Inter)//时间
{
Runmotor_Nums = 0;
Runmotor_step++;
//motor_seat(); //读取电机当前位置
send_set_resp(0xF001, OBJ_DEVICE_ADDR, DATA_LEN, motor_data);//数据发送
}
}
break;
case 4 :
{
if(Run_num >= Run_Stop * 10)//判断磁条是否转动设定的角度
{
motor_dire = 0;
Runmotor_step = 2;
}
else if(Run_num <= 0)
{
motor_dire = 1;
Runmotor_step = 2;
}
else
{
Runmotor_step = 2;
}
}
break;
default :
{
}
break;
}
}
//电机点动运行,方案三(直行程)
void mov_step(void)
{
if(Run_Step == 0) return;//步长不能为0
switch(Runmotor_step)
{
case 1 : //电机运行准备
{
motor_start(); //运行
if(GPIO_ReadPin(GPIO1,GPIO_PIN_4) == 0)//判断是否到达始限位
FWD(); //正转
if(GPIO_ReadPin(GPIO1,GPIO_PIN_5) == 0)//判断是否到达终限位
REV(); //反转
Runmotor_Nums = 0; //运行次数
mov_flag = 0;
Runmotor_step++;
}
break;
case 2 : //电机运行过程
{
motor_mov(1);//运行1个脉冲
if(Runmotor_Nums >= (Run_Step * STEP_LIN))//运行一个步长,脉冲数由驱动器的细分数决定
{
Runmotor_Nums = 0;
motor_stop(); //马达停止
Runmotor_step++;
Run_mm++; //磁条移动距离加1
Run_num = Run_mm * Run_Step;
motor_data[8] = (Run_num & 0xff00) >> 8;//磁条长度高8位
motor_data[9] = Run_num & 0x00ff; //磁条长度低8位
// motor_seat(); //读取电机当前位置
// send_set_resp(0xF001, OBJ_DEVICE_ADDR, DATA_LEN, motor_data);//数据发送
}
}
break;
case 3 :
{
if(it_1ms_flag) //检查1ms定时标志
{
it_1ms_flag = 0;//定时标志清零
Runmotor_Nums++;
}
if(Runmotor_Nums >= 50)//时间
{
Runmotor_Nums = 0;
Runmotor_step++;
motor_seat(); //读取电机当前位置
send_set_resp(0xF001, OBJ_DEVICE_ADDR, DATA_LEN, motor_data);//数据发送
}
}
break;
case 4 :
{
Runmotor_step = 0;
Motor_Run = 0; //运行标记清除
}
break;
default :
{
}
break;
}
}
//电机点动运行,方案三(角行程)
void mov_step_ang(void)
{
if(Run_Step == 0) return;//步长不能为0
switch(Runmotor_step)
{
case 1 : //电机运行准备
{
motor_start(); //运行
FWD(); //正转
Runmotor_Nums = 0; //运行次数
mov_flag = 0;
Runmotor_step++;
}
break;
case 2 : //电机运行过程
{
motor_mov(1);//运行1个脉冲
if(Runmotor_Nums >= (Run_Step * STEP_ROT))//运行一个步长,脉冲数由驱动器的细分数决定
{
Runmotor_Nums = 0;
motor_stop(); //马达停止
Run_mm++; //磁条移动距离加1
Run_num = Run_mm * Run_Step;
motor_data[8] = (Run_num & 0xff00) >> 8;//磁条长度高8位
motor_data[9] = Run_num & 0x00ff; //磁条长度低8位
motor_seat(); //读取电机当前位置
send_set_resp(0xF001, OBJ_DEVICE_ADDR, DATA_LEN, motor_data);//数据发送
Runmotor_step++;
}
}
break;
case 3 :
{
Runmotor_step = 0;
Motor_Run = 0; //运行标记清除
if(Run_num >= 3600) //判断磁条是否转动一圈
Run_mm = 0;
}
break;
default :
{
}
break;
}
}
//电机回到起始位
void mov_begin(void)
{
motor_data[1] = 0x02; //电机运行到起始位对应数据02
motor_data[8] = 0x00; //磁条长度高8位
motor_data[9] = 0x00; //磁条长度低8位
REV(); //电机反转
motor_start(); //电机启动
if(GPIO_ReadPin(GPIO1,GPIO_PIN_4) == 0) //判断是否到达限位处
{
motor_stop(); //电机停止
Run_mm = 0;
Run_num = 0;
if(send_flag == 1)
{
send_flag = 0;
motor_seat(); //读取电机当前位置
send_set_resp(0xF001, OBJ_DEVICE_ADDR, DATA_LEN, motor_data);//数据发送
}
}
else
motor_mov(1); //提供脉冲
}
//电机回到结束位
void mov_end(void)
{
motor_data[1] = 0x03; //电机运行到结束位对应数据03
FWD(); //电机反转
motor_start(); //电机启动
//Run_mm = 0;
if(GPIO_ReadPin(GPIO1,GPIO_PIN_5) == 0)//判断是否到达限位处
motor_stop(); //电机停止
else
motor_mov(1); //提供脉冲
}
//电机位置判断
void motor_seat(void)
{
if(GPIO_ReadPin(GPIO1,GPIO_PIN_4) == 0) //磁条处于起始点限位开关处
motor_data[3] = 0x01; //发送时反馈的数据
else if(GPIO_ReadPin(GPIO1,GPIO_PIN_5) == 0)//磁条处于结束点限位开关处
motor_data[3] = 0x02; //发送时反馈的数据
else if(GPIO_ReadPin(GPIO0,GPIO_PIN_5) == 0)//磁条正通过光栅
motor_data[3] = 0x03; //发送时反馈的数据
else //无状态
motor_data[3] = 0x00; //发送时反馈的数据
}