刚学单片机就用STM32?好牛叉的样子。
控制方法很简单,
比如五线四相步进电机
四拍控制,轮流给每一相一次高电平(其他相低电平),A--延时--B--延时----C---延时--D--延时----A----延时--B这样循环,但是频率不能太大,一般步进电机转速在1200转/s以内,反转倒过来给电平就行了,D--C--B---A
详细请百度搜步进电机
STM32单片机怎么产生脉冲信号控制步进电动机:
#include "stepmotorh"
#include
u32 PUL_CNT; // TIM3脉冲计数
vu32 step_done;
vu32 run_state;
#define run_state_stop 0
#define run_state_acc 1
#define run_state_run 2
#define run_state_dec 3
void STEPMOTOR_CTRL_INIT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //GPIO时钟使能
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //定时器3时钟使能
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //定时器2时钟使能
GPIO_InitStructureGPIO_Pin = GPIO_Pin_7; //PA7为TIM3通道2
GPIO_InitStructureGPIO_Mode = GPIO_Mode_AF_PP; //复用推免输出
GPIO_InitStructureGPIO_Speed = GPIO_Speed_50MHz; //GPIO口响应速度
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructureGPIO_Pin = GPIO_Pin_6; //PA6为DIR控制输出
GPIO_InitStructureGPIO_Mode = GPIO_Mode_Out_PP; //推免输出
GPIO_InitStructureGPIO_Speed = GPIO_Speed_50MHz; //GPIO口响应速度
GPIO_Init(GPIOA, &GPIO_InitStructure);
//TIM3_Configuration
TIM_TimeBaseStructureTIM_Period = 23999; //自动重装载寄存器
TIM_TimeBaseStructureTIM_Prescaler = 2; //预分频器,t=(23999+1)(2+1)/72M
TIM_TimeBaseStructureTIM_CounterMode = TIM_CounterMode_Up; //计数器向上计数模式
TIM_TimeBaseStructureTIM_ClockDivision = 0x0; //时钟分频因子
TIM_TimeBaseStructureTIM_RepetitionCounter = 0x0; //每次溢出都产生事件更新
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); //写TIM3各寄存器参数
TIM_ClearFlag(TIM3,TIM_FLAG_Update); //中断标志位清零
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //允许捕获/比较3中断
TIM_OCInitStructureTIM_OCMode = TIM_OCMode_PWM2; //PWM模式2 TIM3_CCMR1[14:12]=111 在向上计数时,一旦TIMx_CNT
TIM_OCInitStructureTIM_OutputState = TIM_OutputState_Enable; //输入/捕获2输出允许
TIM_OCInitStructureTIM_Pulse = 40; //确定占空比,这个值决定了有效电平的时间。
TIM_OCInitStructureTIM_OCPolarity = TIM_OCPolarity_Low; //输出极性,低电平有效
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //配置定时器输出模式,比较参数等
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器
//TIM2_Configuration
TIM_DeInit(TIM2); //TIM2重新配置为缺省值,默认状态
TIM_TimeBaseStructureTIM_Period = 359; //自动重装载寄存器
TIM_TimeBaseStructureTIM_Prescaler = 199; //时钟预分频器
TIM_TimeBaseStructureTIM_CounterMode = TIM_CounterMode_Up; //计数器向上计数模式
TIM_TimeBaseStructureTIM_ClockDivision = 0x0; //时钟分频因子
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure); //配置TIM2寄存器各参数
TIM_ClearFlag(TIM2,TIM_FLAG_Update); //中断标志位清零
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); //允许捕获/比较2中断
NVIC_InitStructureNVIC_IRQChannel = TIM3_IRQn ; //选择定时器TIM3
NVIC_InitStructureNVIC_IRQChannelPreemptionPriority = 1; //选择抢先式优先级(与中断嵌套级别有关)
NVIC_InitStructureNVIC_IRQChannelSubPriority = 2; //选择子优先级(同抢先式优先级的响应顺序)
NVIC_InitStructureNVIC_IRQChannelCmd = ENABLE; //选择使能中断源
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructureNVIC_IRQChannel = TIM2_IRQn ; //选择定时器TIM2
NVIC_InitStructureNVIC_IRQChannelPreemptionPriority = 1; //选择抢先式优先级(与中断嵌套级别有关)
NVIC_InitStructureNVIC_IRQChannelSubPriority = 3; //选择子优先级(同抢先式优先级的响应顺序)
NVIC_InitStructureNVIC_IRQChannelCmd = ENABLE; //选择使能中断源
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_Configuration(u32 period)
{
TIM3->ARR = period-1;
TIM3->CCR2 = period >> 2;
//TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//TIM_OCInitTypeDef TIM_OCInitStructure;
//TIM_TimeBaseStructureTIM_Period = period-1; //自动重装载寄存器
//TIM_TimeBaseStructureTIM_Prescaler = 29; //预分频器,f=72M/[(period+1)(29+1)], ft = 2400000
//TIM_TimeBaseStructureTIM_CounterMode = TIM_CounterMode_Up; //计数器向上计数模式
//TIM_TimeBaseStructureTIM_ClockDivision = 0x0; //时钟分频因子
//TIM_TimeBaseStructureTIM_RepetitionCounter = 0x0; //每次溢出都产生事件更新
//TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); //写TIM3各寄存器参数
//TIM_OCInitStructureTIM_OCMode = TIM_OCMode_PWM2; //PWM模式2 TIM3_CCMR1[14:12]=111 在向上计数时,一旦TIMx_CNT
//TIM_OCInitStructureTIM_OutputState = TIM_OutputState_Enable; //输入/捕获2输出允许
//TIM_OCInitStructureTIM_Pulse = period >> 2; //确定占空比,25%
//TIM_OCInitStructureTIM_OCPolarity = TIM_OCPolarity_Low; //输出极性,低电平有效
//TIM_OC2Init(TIM3, &TIM_OCInitStructure); //配置定时器输出模式,比较参数等
}
//void MOTOR_RUN(u32 acc, u32 dec, u32 topspeed, u32 dis)
//步进电机运行参数
//acc -- 加速度,单位: round/min/s
//dec -- 减速度,单位: round/min/s
//topspeed -- 最高速度,单位: round/min
//dis -- 总角位移,单位: round/10000
void MOTOR_RUN(u32 acc, u32 dec, u32 topspeed, u32 dis)
{
u32 t_acc,t_dec,step_all,step_acc,step_dec,step_run;
u32 i,tim_cnt,tim_rest,tim_cnt_temp;
step_all = (float)dis (N_MOTOR 00001);
t_acc = topspeed 1000 / acc; //unit: ms
t_dec = topspeed 1000 / dec; //unit: ms
if(topspeed (t_acc + t_dec) / 12 > dis) //达不到最高速度 // topspeed/60/1000 (t_acc + t_dec) / 2 > dis / 10000
{
topspeed = sqrt(dis acc dec 12 / (acc + dec) / 1000);
t_acc = topspeed 1000 / acc; //unit: ms
t_dec = topspeed 1000 / dec; //unit: ms
}
step_acc = N_MOTOR ((float)topspeedtopspeed/(acc120));
step_dec = N_MOTOR ((float)topspeedtopspeed/(dec120));
if(step_all > step_acc + step_dec)
step_run = step_all - step_acc - step_dec;
else
step_run = 0;
//tim_cnt = 52363 ft / (sqrt(accN_MOTOR/2)); //(ft sqrt(60)0676) / sqrt(accN_MOTOR/2);
tim_cnt = 77460 ft / (sqrt(accN_MOTOR/2));
tim_rest = 0;
i = 0;
TIM3_Configuration(tim_cnt);
run_state = run_state_acc;
TIM_Cmd(TIM3,ENABLE);
step_done = 0;
while(step_done==0);
while(i
{
i++;
//tim_cnt_temp = tim_cnt;
//tim_cnt = tim_cnt - (2tim_cnt+tim_rest) / (4i+1);
//tim_rest = (2tim_cnt_temp+tim_rest) % (4i+1);
tim_cnt_temp = tim_cnt / ( sqrt((float)(i+1)) + sqrt((float)(i)) );
TIM3_Configuration(tim_cnt_temp);
step_done = 0;
while(step_done==0);
}
if(step_run > 0)
{
run_state = run_state_run;
tim_cnt = ft 60 / (N_MOTORtopspeed);
i = 0;
TIM3_Configuration(tim_cnt);
while(i
{
step_done = 0;
while(step_done==0);
i++;
}
}
run_state = run_state_dec;
tim_rest = 0;
i=0;
tim_cnt = tim_cnt + (2tim_cnt+tim_rest) / (4(step_dec-i)-1);
while(i
{
TIM3_Configuration(tim_cnt);
step_done = 0;
while(step_done==0);
i++;
tim_cnt_temp = tim_cnt;
tim_cnt = tim_cnt + (2tim_cnt+tim_rest) / (4(step_dec-i)-1);
tim_rest = (2tim_cnt_temp+tim_rest) % (4(step_dec-i)-1);
}
run_state = run_state_stop;
TIM_Cmd(TIM3,DISABLE);
}
void TIM2_IRQHandler(void)
{
}
void TIM3_IRQHandler(void)
{
TIM_ClearFlag(TIM3,TIM_FLAG_Update);
step_done = 1;
//PUL_CNT++;
}
文件:stepmotorh 声明步进电机控制头文件
#define N_MOTOR 10000 //步进电机细分
#define ft 24000000
void STEPMOTOR_CTRL_INIT(void);
void MOTOR_RUN(u32 acc, u32 dec, u32 topspeed, u32 dis);
文件:mainc 主函数,设置加速度,减速度,最大速度和步数的参数值
#include "mainh"
#define LED_SET() GPIO_SetBits(GPIOB,GPIO_Pin_8)
#define LED_RST() GPIO_ResetBits(GPIOB,GPIO_Pin_8)
#define SET_DIR_CW() GPIO_SetBits(GPIOA,GPIO_Pin_6)
#define SET_DIR_CCW() GPIO_ResetBits(GPIOA,GPIO_Pin_6)
void NVIC_Configuration(void);
void LED_init(void);
void soft_delayms(u16 t);
int main(void)
{
SystemInit();
STEPMOTOR_CTRL_INIT();
soft_delayms(1000);
while(1)
{
SET_DIR_CW();
MOTOR_RUN(600,600,1000,500000);
soft_delayms(1000);
SET_DIR_CCW();
MOTOR_RUN(600,600,1000,500000);
soft_delayms(1000);
}
return 0;
}
void NVIC_Configuration(void)
{
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000); //将中断矢量放到Flash的0地址
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //设置优先级配置的模式,详情请阅读原材料中的文章
}
void LED_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructGPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructGPIO_Pin = GPIO_Pin_8;
GPIO_InitStructGPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void soft_delayms(u16 t)
{
u16 tt;
while(t--)
{
tt = 10000;
while(tt--);
}
}
#ifndef _MAIN_H
#define _MAIN_H
#include "stm32f10xh"
#include "stepmotorh"
#endif
像这种调试问题,解决方法:首先测量信号端口对地的电阻,判断是否短路;第二程序是否设置正确,仔细检查,是否有其他地方设置改IO口,第三,是否该IO的驱动能力不足,即电流输出能力,若不足,可以加上啦电阻加强或者用逻辑IC(74HC08)转接一下信号,基本可以解决问题。
硬件问题。stm32电机驱动一边转动是由于硬件问题导致的。采用STM32驱动28BYJ4步进电机,实现正转反转,完成角度调整。步进电机是一种将电脉冲转化为角位移的执行机构。当步进驱动器接收到一个脉冲信号,驱动步进电机按设定的方向转动------一个固定的角度(及步进角)。
以上就是关于用stm32单片机怎么控制步进电机的正转,反转。刚学单片机。好多疑问啊。全部的内容,包括:用stm32单片机怎么控制步进电机的正转,反转。刚学单片机。好多疑问啊。、STM32单片机怎么产生脉冲信号控制步进电动机、基于stm32单片机与tb6600驱动器控制42步进电机的问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)