一.硬件
1.需要一台步进电机,私服驱动器,stm32单片机;先按照说明文档,连接好硬件相关线路,对应好sign+,sign-,puls+,puls-线路,其中sign只是个io口拉高拉低操作,puls是pwm波形输出口,虽然有两根线,但只是需要控制一个IO口输出波形即可;
二.软件
采用主从模式,TIM1为主定时器,tim3未从定时器;不可乱选,查看技术文档选择;附对应文档截图;意思是浅文字部分是你选的主定时器,深色是你能选的从定时器,ITR是需要在代码里配置的;(主定时器也不是可以选乱的,对应IO口有对应的定时器)
1.初始化输出pwm波形的IO口代码
TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
TIM_ARRPreloadConfig(TIM1, ENABLE);
这三行是开启主从模式的,如果你单纯想让电机动起来,不需要控制,去掉这三行,纯运行void pwmz_init(u16 arr)也可以;arr是pwm波形的周期,下面有它的占空比arr/2-1;占空比对电机运转速度不影响,周期越大速度越慢;一般填写4320是100r/min已经是较快速度;
注意所选引脚对应的定时器通道,我选的IO口(pe8)恰好是TIM1_CH1N,看截图,也就是所谓的互补输出,所以才需要额外添加下面这些加粗代码
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
/*******************************************************************************
* 函 数 名 : pwmz_init
* 函数功能 : Z轴电机控制程序,主1从2
* 作 者 : Yy.Fu
*******************************************************************************/
void pwmz_init(u16 arr) // Z
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; // 这一句很重要!!!!!!!!!!!
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = arr / 2 - 1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
2.从定时器代码
TIM_SelectInputTrigger(TIM2, TIM_TS_ITR0);
TIM2->SMCR |= 0x07;
这两句代码就是前面提到的需要对应配置的。比如0x07如何来的,看截图,主TIM1从TIM2对应的是ITR0,也就是0x0;而7是你选择的触发模式,我选的是外部触发模式1,二进制是111,对应十进制7,所以得到0x07;TIM_TS_ITR0也是这个截图对应的;
void TIM2_PWM_Init(u16 PulseNum)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = PulseNum;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_SelectInputTrigger(TIM2, TIM_TS_ITR0);
TIM2->SMCR |= 0x07;
TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
3.从定时器被主定时器溢满出中断代码
times_z++;
if (times_z < currentIndex1 * 10)
{
Pulse_output_Z(3600, 10000);
return;
}
times_z = 0;
这一部分是为了让电机转一圈所用的循环;
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM_CtrlPWMOutputs(TIM1, DISABLE);
TIM_Cmd(TIM1, DISABLE);
TIM_Cmd(TIM2, DISABLE);
TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
}
times_z++;
if (times_z < currentIndex1 * 10)
{
Pulse_output_Z(3600, 10000);
return;
}
times_z = 0;
}
4.IO口拉高拉低控制方向代码
这代码放在系统初始化即可;文章来源:https://www.toymoban.com/news/detail-763832.html
/*******************************************************************************
* 函 数 名 : DIR_Crl1
* 函数功能 : IO口初始化,电机正反转
* 作 者 : Yy.Fu
*******************************************************************************/
void DIR_Crl_Z(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE, GPIO_Pin_9);
}
5..h文件代码
#include "stm32f10x.h"
#include "stm32f10x_exti.h"
static u16 times_z = 0;
void Pulse_output_Z(u16 arr, u16 PulseNum);
6.主代码(放在你想用的地方)
void Pulse_output_Z(u16 arr, u16 PulseNum)
{
TIM2_PWM_Init(PulseNum);
TIM_Cmd(TIM2, ENABLE);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
pwmz_init(arr);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
}
到此,一台步进电机就可以控制啦,如果控制不了,仔细检查相关配置;文章来源地址https://www.toymoban.com/news/detail-763832.html
到了这里,关于stm32F103单片机pwm控制伺服驱动器进而控制步进电机的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!