• 项目:stm32 PWM驱动步进电机
  • 代码:
  • StepperMotor.c
#include "stm32f10x.h"
#include "PWM.h"

//PA4(AIN1),PA5(AIN2)步进电机GPIO端口初始化
void StepperMotor_Init(void)
{
	//配置PB1为复用推挽输出
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	PWM_Init();		//初始化TIM3_CH4(PB1)输出PWM
}	



//设定步进电机速度函数
void StepperMotor_SetSpeed(int8_t Speed)
{
	if(Speed >= 0)
	{
		GPIO_SetBits(GPIOA, GPIO_Pin_4);
		GPIO_ResetBits(GPIOA, GPIO_Pin_5);
		PWM_SetCompare4(Speed);		//设置CCR值来设置占空比,从而实现调速
	}
	else
	{
		GPIO_ResetBits(GPIOA, GPIO_Pin_4);
		GPIO_SetBits(GPIOA, GPIO_Pin_5);
		PWM_SetCompare4(-Speed);
	}
}
  • StepperMotor.h
#ifndef __StepperMotor_H
#define __StepperMotor_H
void StepperMotor_Init(void);

void StepperMotor_SetSpeed(int8_t Speed);

#endif
  • main.c
#include "stm32f10x.h"
#include "Delay.h"
#include "OLED.h"
#include "StepperMotor.h"
#include "Key.h"

uint8_t KeyNum;
int8_t Speed;

int main(void)
{
	OLED_Init();
	StepperMotor_Init();	
	StepperMotor_SetSpeed(-50);
	OLED_ShowString(1, 1, "Speed:");
	
	while(1)	
	{	
		KeyNum = Key_GetNum();
		if(KeyNum == 1)
		{
			Speed += 20;
			if(Speed> 100)
			{
				Speed = -100;
			}
		}
		StepperMotor_SetSpeed(Speed);
		OLED_ShowSignedNum(1, 7, Speed, 3);
		
	}
}
  • PWM.c
#include "stm32f10x.h"

void PWM_Init(void)
{
	//PB1默认TIM3_CH4通道
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	
	//配置PB1为复用推挽输出
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;		//复用推挽输出,中断控制来自片上外设(TIM3_CH3),而非输出数据寄存器
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	//设置TIM3定时器内部时钟源
	TIM_InternalClockConfig(TIM3);
	//频率2kHz,占空比100%
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStructure.TIM_Period = 100 - 1;		//ARR
	TIM_TimeBaseInitStructure.TIM_Prescaler = 36 - 1;		//PSC
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
	//清除更新标志位
	TIM_ClearFlag(TIM3, TIM_FLAG_Update);
	TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);	//TIM3中断配置使能
	
	TIM_OCInitTypeDef	TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure);		//给结构体赋初始值
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;		//输出比较模式
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;	//极性选择
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;	//输出使能
	TIM_OCInitStructure.TIM_Pulse = 0;		//初始值设CCR为0
	TIM_OC4Init(TIM3, &TIM_OCInitStructure);
	//打开TIM3定时器外设
	TIM_Cmd(TIM3, ENABLE);
	
}


//PWM占空比的函数
void PWM_SetCompare4(uint16_t Compare4)
{
	TIM_SetCompare4(TIM3, Compare4);	//设置CCR值
}
  • PWM.h
#ifndef __PWM_H
#define __PWM_H
void PWM_Init(void);
void PWM_SetCompare4(uint16_t Compare4);

#endif