高级会员
- 积分
- 715
- 威望
- 395
- 贡献
- 200
- 兑换币
- 220
- 注册时间
- 2014-2-20
- 在线时间
- 60 小时
- 毕业学校
- 湖北工程学院
|
之前是用的延时驱动步进电机,但是要求我的电机转的慢,所以整个延时导致对我的程序影响大,现在想换TIM4的pwm驱动步进电机,用以前驱动直流电机的改,但是不成功,请大神帮忙看看,或者分享一个DEMO,谢谢啦!!
==================================这是以前控制双电机的驱动=======================================
/**************************************************************
* Function : 控制双电机的方向,占空比,速度
* Description : 电机驱动控制程序(new)
* Input : motorL, motorR, left_motor1,left_motor2,right_motor1,right_motor2 (控制前进后退与方向,占空比)
* Output : None
* Return : None
* Others : left_motor1,left_motor2,right_motor1,right_motor2
代表控制电机的对应的pwm值(0~1000,也可以调档)
* Author : Draway
****************************************************************/
void CtrlMotor(Direction motorL,u16 left_motor1,u16 left_motor2,Direction motorR,u16 right_motor1,u16 right_motor2)
{
if(motorL==RUN_FORWARD)//左轮前进
{
GPIO_SetBits(GPIOB,GPIO_Pin_6);
GPIO_ResetBits(GPIOB,GPIO_Pin_7);
}
else if(motorL==RUN_BACKWARD)//左轮后退
{
GPIO_SetBits(GPIOB,GPIO_Pin_7);
GPIO_ResetBits(GPIOB,GPIO_Pin_6);
}
else if(motorL==STOP)//左轮停转
{
GPIO_SetBits(GPIOB,GPIO_Pin_7);
GPIO_SetBits(GPIOB,GPIO_Pin_6);
}
if(motorR==RUN_FORWARD)//右轮前进
{
GPIO_SetBits(GPIOB,GPIO_Pin_8);
GPIO_ResetBits(GPIOB,GPIO_Pin_9);
}
else if(motorR==RUN_BACKWARD)//右轮后退
{
GPIO_SetBits(GPIOB,GPIO_Pin_9);
GPIO_ResetBits(GPIOB,GPIO_Pin_8);
}
else if(motorR==STOP) //右轮后退
{
GPIO_SetBits(GPIOB,GPIO_Pin_9);
GPIO_SetBits(GPIOB,GPIO_Pin_8);
}
TIM_SetCompare1(TIM4,left_motor1);
TIM_SetCompare2(TIM4,left_motor2);
TIM_SetCompare3(TIM4,right_motor1);
TIM_SetCompare4(TIM4,right_motor2);
}
================================这是我改的驱动步进电机驱动================================================
/**************************************************************
* Function : 控制步进电机的方向,占空比,速度
* Description : 电机驱动控制程序(new)
* Input : motorL, motorR, left_motor1,left_motor2,right_motor1,right_motor2 (控制前进后退与方向,占空比)
* Output : None
* Return : None
* Others : left_motor1,left_motor2,right_motor1,right_motor2
代表控制电机的对应的pwm值(0~1000,也可以调档)
* Author : Draway
* Date : 2016年1月21日
****************************************************************/
void CtrlMotor_BJ(Direction motor)
{
if(motor==RUN_FORWARD)//前进
{
Coil_A1
TIM_SetCompare1(TIM4,FAST_F);
TIM_SetCompare2(TIM4,END_F);
TIM_SetCompare3(TIM4,END_F);
TIM_SetCompare4(TIM4,END_F);
Coil_AB1
TIM_SetCompare1(TIM4,FAST_F);
TIM_SetCompare2(TIM4,FAST_F);
TIM_SetCompare3(TIM4,END_F);
TIM_SetCompare4(TIM4,END_F);
Coil_B1
TIM_SetCompare1(TIM4,END_F);
TIM_SetCompare2(TIM4,FAST_F);
TIM_SetCompare3(TIM4,END_F);
TIM_SetCompare4(TIM4,END_F);
Coil_BC1
TIM_SetCompare1(TIM4,END_F);
TIM_SetCompare2(TIM4,FAST_F);
TIM_SetCompare3(TIM4,FAST_F);
TIM_SetCompare4(TIM4,END_F);
Coil_C1
TIM_SetCompare1(TIM4,END_F);
TIM_SetCompare2(TIM4,END_F);
TIM_SetCompare3(TIM4,FAST_F);
TIM_SetCompare4(TIM4,END_F);
Coil_CD1
TIM_SetCompare1(TIM4,END_F);
TIM_SetCompare2(TIM4,END_F);
TIM_SetCompare3(TIM4,FAST_F);
TIM_SetCompare4(TIM4,FAST_F);
Coil_D1
TIM_SetCompare1(TIM4,END_F);
TIM_SetCompare2(TIM4,END_F);
TIM_SetCompare3(TIM4,END_F);
TIM_SetCompare4(TIM4,FAST_F);
Coil_DA1
TIM_SetCompare1(TIM4,FAST_F);
TIM_SetCompare2(TIM4,END_F);
TIM_SetCompare3(TIM4,END_F);
TIM_SetCompare4(TIM4,FAST_F);
// GPIO_SetBits(GPIOB,GPIO_Pin_6);
// GPIO_ResetBits(GPIOB,GPIO_Pin_7);
}
else if(motor==RUN_BACKWARD)//后退
{
Coil_A1
Coil_DA1 //遇到Coil_AB1 用{A1=1;B1=1;C1=0;D1=0;}代替
Coil_D1
Coil_CD1
Coil_C1
Coil_BC1
Coil_B1
Coil_AB1
// GPIO_SetBits(GPIOB,GPIO_Pin_7);
// GPIO_ResetBits(GPIOB,GPIO_Pin_6);
}
else if(motor==STOP)//电机停转
{
GPIO_SetBits(GPIOB,GPIO_Pin_9);
GPIO_SetBits(GPIOB,GPIO_Pin_8);
GPIO_SetBits(GPIOB,GPIO_Pin_7);
GPIO_SetBits(GPIOB,GPIO_Pin_6);
TIM_SetCompare1(TIM4,END_F);
TIM_SetCompare2(TIM4,END_F);
TIM_SetCompare3(TIM4,END_F);
TIM_SetCompare4(TIM4,END_F);
}
// //尽量统一用库函数操作
// TIM_SetCompare1(TIM4,left_motor1);
// TIM_SetCompare2(TIM4,left_motor2);
// TIM_SetCompare3(TIM4,right_motor1);
// TIM_SetCompare4(TIM4,right_motor2);
}
///=====================/步进电机宏定义===============================
//#define Coil_A1 {A1=1;B1=0;C1=0;D1=0;}//A相通电,其他相断电
//#define Coil_B1 {A1=0;B1=1;C1=0;D1=0;}//B相通电,其他相断电
//#define Coil_C1 {A1=0;B1=0;C1=1;D1=0;}//C相通电,其他相断电
//#define Coil_D1 {A1=0;B1=0;C1=0;D1=1;}//D相通电,其他相断电
//#define Coil_AB1 {A1=1;B1=1;C1=0;D1=0;}//AB相通电,其他相断电
//#define Coil_BC1 {A1=0;B1=1;C1=1;D1=0;}//BC相通电,其他相断电
//#define Coil_CD1 {A1=0;B1=0;C1=1;D1=1;}//CD相通电,其他相断电
//#define Coil_DA1 {A1=1;B1=0;C1=0;D1=1;}//D相通电,其他相断电
//#define Coil_OFF {A1=0;B1=0;C1=0;D1=0;}//全部断电
====================延时可以正常步进驱动电机程序=============================
//正转
void motor_go(u16 data)
{
Coil_A1
delay_motor(data); //延时参数
Coil_AB1 //遇到Coil_AB1 用{A1=1;B1=1;C1=0;D1=0;}代替
delay_motor(data); //延时参数 //改变这个参数可以调整电机转速 ,
Coil_B1
delay_motor(data); //延时参数
Coil_BC1
delay_motor(data);
Coil_C1
delay_motor(data);
Coil_CD1
delay_motor(data);
Coil_D1
delay_motor(data);
Coil_DA1
delay_motor(data);
}
|
|