步进电机驱动器接线方式都是通用的,在此说明一下步进电机驱动器的四个脚:
PUL:脉冲输入,单片机输出PWM脉冲的引脚接步进电机驱动器的PUL脚;
DIR:方向控制,单片机引出一个引脚接到步进电机驱动器的DIR脚,通过切换连到DIR上的高低电平可控制步进电机正转或反转;
ENA:电机使能,单片机引出一个引脚接到ENA,通过给ENA高电平或低电平可以使能或禁用连接到步进电机驱动器的步进电机
COM:共阳极或共阴极,将该脚连接到单片机系统的VCC或GND可将步进电机驱动器切换为共阳极或共阴极模式
注意若步进电机驱动器是差分输入的,则PUL、DIR、ENA中的每个引脚会被分为+、-两个引脚,
当共阳极接法时:所有+引脚接到单片机系统的VCC,所有-引脚接到单片机的GPIO;
当共阴极接法时:所有-引脚接到单片机系统的GND,所有+引脚接到单片机的GPIO;
实际应用:做实验时步进电机驱动器细分数可以随便选,我是按照共阳极接法接的。 我用的是指南者开发板,步进电机选用的是42步进电机,此处我用的是野火的步进电机驱动器,接线方式是共阳极接法,野火的步进电机驱动器资料见野火EBF-MSD4805 2相步进电机驱动器128细分 57 86 42步进电机-淘宝网 (taobao.com)
一、main.c
#include "stm32f10x.h"
#include "tim.h"
#include "key.h"
#define Default_speed 3600
uint16_t NUM_ARR=Default_speed;
uint16_t Displacement=0;
uint16_t Status=1;
void delay(uint32_t count);
int main(void)
{
Key_GPIO_Config();
ledInit();
TIM_Slave_config(0);
TIM_Master_config(NUM_ARR,20);
GPIO_SetBits(GPIOA, GPIO_Pin_8);
while(1)
{
//停止键被按下
if(Key_Scan(STOP_ENA_PORT,STOP_ENA_PIN)==KEY_ON)
{
TIM_Cmd(TIM_Master,DISABLE);
TIM_ARRPreloadConfig(TIM_Master,DISABLE);
TIM_SetAutoreload(TIM_Master, Default_speed);
TIM_SetCompare1(TIM_Master, Default_speed/2);
TIM_SetCounter(TIM_Master,0);
}
//步进电机轴逆时针旋转键被按下
if(Key_Scan(UP_DIR_PORT,UP_DIR_PIN)==KEY_ON)
{
if(NUM_ARR<Default_speed)
{
while(NUM_ARR<Default_speed)
{
NUM_ARR+=200;
delay(0xffff);
}
}
NUM_ARR=Default_speed;
GPIO_SetBits(OUTPUT_DIR_PORT, OUTPUT_DIR_PIN);
StepperSwitch(DISABLE);
delay(0x1fffff);
StepperSwitch(ENABLE);
}
//步进电机轴顺时针旋转键被按下
if(Key_Scan(DOWN_DIR_PORT,DOWN_DIR_PIN)==KEY_ON)
{
if(NUM_ARR<Default_speed)
{
while(NUM_ARR<Default_speed)
{
NUM_ARR+=200;
delay(0xffff);
}
}
NUM_ARR=Default_speed;
GPIO_ResetBits(OUTPUT_DIR_PORT, OUTPUT_DIR_PIN);
StepperSwitch(DISABLE);
delay(0x1fffff);
StepperSwitch(ENABLE);
}
//按下后步进电机转速加快
if(Key_Scan(FASTER_PORT,FASTER_PIN)==KEY_ON)
{
TIM_Cmd(TIM_Master,DISABLE);
TIM_ARRPreloadConfig(TIM_Master,DISABLE);
delay(0xffff);
NUM_ARR-=100;
if(NUM_ARR<MinimumSpeed)
{
NUM_ARR=MinimumSpeed;
}
TIM_SetAutoreload(TIM_Master, NUM_ARR);
TIM_SetCompare1(TIM_Master, NUM_ARR/2);
TIM_ARRPreloadConfig(TIM_Master,ENABLE);
TIM_SetCounter(TIM_Master,0);
TIM_Cmd(TIM_Master,ENABLE);
}
//按下后步进电机转速变慢
if(Key_Scan(SLOWER_PORT,SLOWER_PIN)==KEY_ON)
{
TIM_Cmd(TIM_Master,DISABLE);
TIM_ARRPreloadConfig(TIM_Master,DISABLE);
delay(0xffff);
NUM_ARR+=100;
if(NUM_ARR>MaxSpeed)
{
NUM_ARR=MaxSpeed;
}
TIM_SetAutoreload(TIM_Master, NUM_ARR);
TIM_SetCompare1(TIM_Master, NUM_ARR/2);
TIM_ARRPreloadConfig(TIM_Master,ENABLE);
TIM_SetCounter(TIM_Master,0);
TIM_Cmd(TIM_Master,ENABLE);
}
}
}
void delay(uint32_t count)
{
for(;count!=0;count--);
}
二、定时器程序:
1.tim.h
#ifndef __TIM_H
#define __TIM_H
#include "stm32f10x.h"
//#define Prescaler 71//预分频系数
//主定时器配置
//时钟配置
#define GPIOofTIM_MasterClockCmd RCC_APB2PeriphClockCmd
#define TIM_Master_PeriphClockCmd RCC_APB1PeriphClockCmd
#define RCC_APBXPeriph_GPIOX RCC_APB2Periph_GPIOA
#define RCC_APBXPeriph_TIMX RCC_APB1Periph_TIM2
//预装载配置
#define TIM_OCXPreload_Config TIM_OC1PreloadConfig
//定时器通道配置
#define TIM_Master TIM2
#define TIM_Master_OCInit TIM_OC1Init
//GPIO配置
#define PUL_Master_GPIOX GPIOA
#define TIM_Master_GPIO GPIO_Pin_0
//----------------------------------------------------------------------------------------------------
//从定时器配置
//时钟配置
#define TIM_Slave_PeriphClockCmd RCC_APB1PeriphClockCmd
#define TIMSlave_RCC_APBXPeriph RCC_APB1Periph_TIM4
//外设配置
#define TIM_Slave TIM4
#define TIM_Slave_OCInit TIM_OC1Init
//中断配置
#define TIM_Slave_IRQn TIM4_IRQn
//输入触发源
#define Slave_Input_Trigger_source TIM_TS_ITR1
//中断函数
#define TIM_Slave_IRQHandler TIM4_IRQHandler
void TIMSlave_NVICConfig(void);
void TIM_Slave_config(u16 PulseNum);
void TIM_Master_config(u16 arr,u16 psc);
void StepperSwitch(FunctionalState NewState);
#endif /*__TIM_H*/
2.tim.c
#include "tim.h"
/**************************
* 函数名:TIM_Master_config
* 描述 :配置主定时器,主定时器控制步进电机转速
* 输入 :PulseFrequency,脉冲频率,注意输入数字越大则步进电机转速越慢,PulseFrequency范围在0-65535
* 输出 :无
* 调用 :Pulse_output
* 返回值:无
****************************/
void TIM_Master_config(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIOofTIM_MasterClockCmd(RCC_APBXPeriph_GPIOX , ENABLE); //开启GPIO时钟
TIM_Master_PeriphClockCmd(RCC_APBXPeriph_TIMX , ENABLE);//开启主定时器时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
//PUL脉冲输出引脚
GPIO_InitStructure.GPIO_Pin = TIM_Master_GPIO; //主定时器通道一的GPIO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(PUL_Master_GPIOX, &GPIO_InitStructure);
TIM_DeInit(TIM_Master);
TIM_TimeBaseStructure.TIM_Period = arr-1; //设置一个脉冲的时间
TIM_TimeBaseStructure.TIM_Prescaler =psc-1; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM_Master, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = arr/2;//脉冲占空比50%,设定tim_ccr的值,输出脉冲发生跳变
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;
TIM_Master_OCInit(TIM_Master, &TIM_OCInitStructure);
TIM_ARRPreloadConfig(TIM_Master, DISABLE); // 失能TIMx在ARR上的预装载寄存器
TIM_OCXPreload_Config(TIM_Master, TIM_OCPreload_Enable); //使能TIMx在CCR1上的预装载寄存器
TIM_SelectMasterSlaveMode(TIM_Master, TIM_MasterSlaveMode_Enable);
TIM_SelectOutputTrigger(TIM_Master, TIM_TRGOSource_Update);//使用更新事件作为触发输出信号
TIM_Cmd(TIM_Master, DISABLE);
}
/**************************
* 函数名:TIM_Slave_config
* 描述 :配置从定时器及其中断,从定时器控制步进电机转动角度
* 输入 :PulseNum,即脉冲个数,范围在0-65535
* 输出 :无
* 调用 :Pulse_output
* 返回值:无
****************************/
void TIM_Slave_config(u16 PulseNum)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //对应结构体声明
TIM_Slave_PeriphClockCmd(TIMSlave_RCC_APBXPeriph, ENABLE);
TIM_TimeBaseStructure.TIM_Period = PulseNum-1; //配置要输出的PWM脉冲个数
TIM_TimeBaseStructure.TIM_Prescaler =0; //预分频系数为0
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM_Slave, &TIM_TimeBaseStructure);
TIM_ARRPreloadConfig(TIM_Slave, DISABLE); // 失能TIMx在ARR上的预装载寄存器
TIM_ITRxExternalClockConfig(TIM_Slave, Slave_Input_Trigger_source);//设置从定时器内部触发为外部时钟模式,触发源为TIM_TS_ITRx
TIM_SelectSlaveMode(TIM_Slave, TIM_SlaveMode_External1); //选择从定时器为从模式,主定时器更新事件所产生的出发输出上升沿作为从定时器的触发输入信号
TIM_ITConfig(TIM_Slave, TIM_IT_Update, ENABLE); //使能从定时器中断,当从定时器的计数器溢出时产生中断
TIMSlave_NVICConfig();
TIM_Cmd(TIM_Slave, DISABLE);
}
/**************************
* 函数名:TIMSlave_NVICConfig
* 描述 :配置从定时器中断
* 输入 :无
* 输出 :无
* 调用 :Pulse_output
* 返回值:无
****************************/
void TIMSlave_NVICConfig(void)
{
NVIC_InitTypeDef NVIC_InitStructure; //NVIC 对应结构体声明
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel=TIM_Slave_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**************************
* 函数名:StepperSwitch
* 描述 :控制步进电机启动停止
* 输入 :脉冲频率PulseFrequency,脉冲个数PulseNum,两者范围均为0-65535
* 输出 :无
* 调用 :主函数
* 返回值:无
****************************/
void StepperSwitch(FunctionalState NewState)
{
TIM_Cmd(TIM_Master, NewState);
TIM_Cmd(TIM_Slave, NewState);
}
三、中断函数
#include "stm32f10x_it.h"
#include "stm32f10x.h"
#include "tim.h"
extern uint16_t Status;
extern uint16_t Displacement;
void TIM_Slave_IRQHandler(void)
{
if (TIM_GetITStatus(TIM_Slave, TIM_IT_Update) == SET) // 更新事件:计数器溢出
{
Status=0;
Displacement=0;
TIM_ClearITPendingBit(TIM_Slave, TIM_IT_Update); // 清除中断标志位
TIM_Cmd(TIM_Master, DISABLE); // 关闭主定时器
TIM_Cmd(TIM_Slave, DISABLE); // 关闭从定时器
TIM_SetAutoreload(TIM_Slave, 0);
TIM_SetCounter(TIM_Slave,0);
}
}
具体代码见
链接:https://pan.baidu.com/s/12N2VbgCiJNQ8wBMTKZDpvA
提取码:qwer
其中software为原始版本,software(5)为最新版本,不过最新版本我并没有验证过,应该有错误,可以参考旧版本代码。
关于主从定时器,参考:(3条消息) 嵌入式STM32学习笔记(5)——定时器主从模式,精确输出PWM脉冲数量_abcvincent的博客-CSDN博客_定时器主从模式