关于STM32F4的步进电机控制实验,可自由控制步进电机正转反转,可运用到智能家居。谢谢采纳。
单片机源程序如下:
- /*
- ****************************************************************************************
- * INCLUDES (头文件包含)
- ****************************************************************************************
- */
- #include "motor.h"
- #include "common.h"
- #include "key.h"
- /*
- ****************************************************************************************
- * CONSTANTS (常量定义)
- ****************************************************************************************
- */
- /*
- ****************************************************************************************
- * TYPEDEFS (类型定义)
- ****************************************************************************************
- */
- /*
- ****************************************************************************************
- * LOCAL VARIABLES (静态变量)
- ****************************************************************************************
- */
- /*
- ****************************************************************************************
- * LOCAL FUNCTIONS DECLARE (静态函数声明)
- ****************************************************************************************
- */
- static void stepper_motor_cw(u32 xms);
- static void stepper_motor_ccw(u32 xms);
- /*
- ****************************************************************************************
- * LOCAL FUNCTIONS (静态函数)
- ****************************************************************************************
- */
- //步进电机正转
- static void stepper_motor_cw(u32 xms)
- {
- //A+
- STEPPER_MOTOR_IN1_H;
- STEPPER_MOTOR_IN2_L;
- STEPPER_MOTOR_IN3_L;
- STEPPER_MOTOR_IN4_L;
- TIM11_delayms(xms);
- //B-
- STEPPER_MOTOR_IN1_L;
- STEPPER_MOTOR_IN2_L;
- STEPPER_MOTOR_IN3_L;
- STEPPER_MOTOR_IN4_H;
- TIM11_delayms(xms);
- //A-
- STEPPER_MOTOR_IN1_L;
- STEPPER_MOTOR_IN2_H;
- STEPPER_MOTOR_IN3_L;
- STEPPER_MOTOR_IN4_L;
- TIM11_delayms(xms);
- //B+
- STEPPER_MOTOR_IN1_L;
- STEPPER_MOTOR_IN2_L;
- STEPPER_MOTOR_IN3_H;
- STEPPER_MOTOR_IN4_L;
- TIM11_delayms(xms);
- }
- //翻转
- static void stepper_motor_ccw(u32 xms)
- {
- // A+ -- B- -- A- -- B+
- //A+
- STEPPER_MOTOR_IN1_L;
- STEPPER_MOTOR_IN2_L;
- STEPPER_MOTOR_IN3_H;
- STEPPER_MOTOR_IN4_L;
- TIM11_delayms(xms);
- //B-
- STEPPER_MOTOR_IN1_L;
- STEPPER_MOTOR_IN2_H;
- STEPPER_MOTOR_IN3_L;
- STEPPER_MOTOR_IN4_L;
- TIM11_delayms(xms);
- //A-
- STEPPER_MOTOR_IN1_L;
- STEPPER_MOTOR_IN2_L;
- STEPPER_MOTOR_IN3_L;
- STEPPER_MOTOR_IN4_H;
- TIM11_delayms(xms);
- //B+
- STEPPER_MOTOR_IN1_H;
- STEPPER_MOTOR_IN2_L;
- STEPPER_MOTOR_IN3_L;
- STEPPER_MOTOR_IN4_L;
- TIM11_delayms(xms);
- }
- /*
- ****************************************************************************************
- * PUBLIC FUNCTIONS (全局函数)
- ****************************************************************************************
- */
- /*
- ****************************************************************************************
- * Function: dc_motor_init
- * Description: 直流电机初始化
- * Input: None
- * Output: None
- * Return: None
- * Author: Dingwei
- * Others: IA -- PA7/TIM3CH2
- IB -- PA6/TIM3CH1
- * Date of completion: 2020-12-11
- * Date of last modify: 2020-12-11
- ****************************************************************************************
- */
- void dc_motor_init(void)
- {
- //本质就是PA6和PA7复用为TIM3CHn(输出比较功能)的初始化配置
- TIM_OCInitTypeDef tim_OCInitStruct;
- TIM_TimeBaseInitTypeDef tim_TimeBaseInitStruct;
- GPIO_InitTypeDef gpio_InitStruct;
- /*1. 激活通信相关GPIO引脚,将他们配置为复用模式,并映射到该通信接口上*/
- RCC_AHB1PeriphClockCmd (DCMOTOR_IA_Periph_ENABLE | DCMOTOR_IB_Periph_ENABLE, ENABLE);
- //对GPIO初始化句柄赋值(写入配置信息)
- gpio_InitStruct.GPIO_Mode = GPIO_Mode_AF ;
- gpio_InitStruct.GPIO_OType = GPIO_OType_PP ;
- gpio_InitStruct.GPIO_Pin = DCMOTOR_IA_Pinx ; //若操作多个引脚使用 | 连接起来
- gpio_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ;
- gpio_InitStruct.GPIO_Speed = GPIO_Low_Speed ;
- GPIO_Init(DCMOTOR_IA_GPIOx,&gpio_InitStruct);
-
- gpio_InitStruct.GPIO_Pin = DCMOTOR_IB_Pinx ;
- GPIO_Init(DCMOTOR_IB_GPIOx,&gpio_InitStruct);
-
- //将IA和IB引脚映射到相应的TIM上
- GPIO_PinAFConfig (DCMOTOR_IA_GPIOx, DCMOTOR_IA_PinSourcex, DCMOTOR_IA_AF);
- GPIO_PinAFConfig (DCMOTOR_IB_GPIOx, DCMOTOR_IB_PinSourcex, DCMOTOR_IB_AF);
-
- /*2. TIM时基单元配置*/
- //激活基本定时器
- RCC_APB1PeriphClockCmd (DCMOTOR_TIM_ENABLE, ENABLE);
- //设定定时时长 == (arr+1)/ (84000000/(psc+1)) == (arr+1)*(psc+1) /84000000
- tim_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1 ;
- tim_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up ;
- tim_TimeBaseInitStruct.TIM_Period = 10000-1 ; //重载值
- tim_TimeBaseInitStruct.TIM_Prescaler = 84-1 ; //分频
- tim_TimeBaseInitStruct.TIM_RepetitionCounter = 0 ;//仅对TIM1和TIM8有用
- TIM_TimeBaseInit (DCMOTOR_TIMx, &tim_TimeBaseInitStruct);
- //考虑部分寄存器写入缓冲特性,需要产生一次更新事件,之后清除标志位
- TIM_GenerateEvent (DCMOTOR_TIMx, TIM_EventSource_Update);
- TIM_ClearFlag (DCMOTOR_TIMx, TIM_FLAG_Update);
- //计数器清零
- TIM_SetCounter (DCMOTOR_TIMx, 0);
-
- /*3. TIM3OC1和OC2初始化*/
- //tim_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Reset ;
- //tim_OCInitStruct.TIM_OCNIdleState = TIM_OCNIdleState_Reset ;
- //tim_OCInitStruct.TIM_OCNPolarity = TIM_OCNPolarity_High ;
- //tim_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Disable ;
- tim_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1 ;
- tim_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High ;//有效电平为高电平
- tim_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable ;//输出通道使能
- tim_OCInitStruct.TIM_Pulse = 0 ;//比较值初值
- TIM_OC1Init (DCMOTOR_TIMx, &tim_OCInitStruct);
- TIM_OC2Init (DCMOTOR_TIMx, &tim_OCInitStruct);
-
- /*4. 使能TIM(开启计数)*/
- TIM_Cmd(DCMOTOR_TIMx,ENABLE);
- }
- /*
- ****************************************************************************************
- * Function: dc_motor_speedControl
- * Description: 直流电机速度控制函数
- * Input: int speedPercent 速度百分比(有效数据在-100~+100范围)
- * Output: None
- * Return: None
- * Author: Dingwei
- * Others: 速度的正负表示转动方向
- * Date of completion: 2020-12-10
- * Date of last modify: 2020-12-10
- ****************************************************************************************
- */
- void dc_motor_speedControl(int speedPercent)
- {
- if(speedPercent > 0) //正转
- {
- if(speedPercent > 100)
- {
- speedPercent = 100;
- }
- DCMOTOR_IA_DUTYCYCLE(speedPercent);
- DCMOTOR_IB_DUTYCYCLE(0);
- }
- else if(speedPercent < 0) //反转
- {
- if(speedPercent < -100)
- {
- speedPercent = -100;
- }
- DCMOTOR_IA_DUTYCYCLE(0);
- DCMOTOR_IB_DUTYCYCLE(-1*speedPercent);
- }
- else //停止
- {
- DCMOTOR_IA_DUTYCYCLE(0);
- DCMOTOR_IB_DUTYCYCLE(0);
- }
- }
- /*
- ****************************************************************************************
- * Function: stepper_motor_init
- * Description: 步进电机初始化
- * Input: None
- * Output: None
- * Return: None
- * Author: Dingwei
- * Others: PC3~0对应IN1~4,推挽输出
- * Date of completion: 2020-12-10
- * Date of last modify: 2020-12-10
- ****************************************************************************************
- */
- void stepper_motor_init(void)
- {
- //0. 激活GPIOC
- RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOC, ENABLE);
- //1. 创建GPIO初始化句柄
- GPIO_InitTypeDef gpio_InitStruct;
- //2. 对GPIO初始化句柄赋值(写入配置信息)
- gpio_InitStruct.GPIO_Mode = GPIO_Mode_OUT ;
- gpio_InitStruct.GPIO_OType = GPIO_OType_PP ;
- gpio_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_2 | GPIO_Pin_1 | GPIO_Pin_0; //若操作多个引脚使用 | 连接起来
- gpio_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ;
- gpio_InitStruct.GPIO_Speed = GPIO_Low_Speed ;
- //3. 库函数GPIO初始化
- GPIO_Init(GPIOC,&gpio_InitStruct);
- //4. 初始时,LED1~3熄灭
- GPIO_SetBits(GPIOC, GPIO_Pin_3);
- GPIO_SetBits(GPIOC, GPIO_Pin_2);
- GPIO_SetBits(GPIOC, GPIO_Pin_1);
- GPIO_SetBits(GPIOC, GPIO_Pin_0);
- }
- /*
- ****************************************************************************************
- * Function: stepper_motor_move
- * Description: 步进电机运动
- * Input: int cycle 周期,正负表示正反转
- 采用单四拍,每四拍对应一个周期
- u32 xms 每个节拍的间隔
- * Output: None
- * Return: None
- * Author: Dingwei
- * Others: 单四拍
- * Date of completion: 2020-12-10
- * Date of last modify: 2020-12-10
- ****************************************************************************************
- */
- void stepper_motor_move(int cycle,u32 xms)
- {
- int i;
- if(cycle > 0)
- {
- for(i=0 ; i <cycle ; i++)
- {
- stepper_motor_cw(xms);
- }
- }
- else if(cycle < 0)
- {
- cycle *= -1;
- for(i=0 ; i <cycle ; i++)
- {
- stepper_motor_ccw(xms);
- }
- }
- else //cycle == 0
- {
- STEPPER_MOTOR_IN1_L;
- STEPPER_MOTOR_IN2_L;
- STEPPER_MOTOR_IN3_L;
- STEPPER_MOTOR_IN4_L;
- }
- }
- /*******************************************************************************
- * 函数名:
- * 函数参数
- * 功能描述:
- * 参数说明:
- * 返回值说明
- * 修改记录:
- IA PA7 IB PA6
- stepper_motor_move(-185,1);
- stepper_motor_move(185,1);
- *******************************************************************************/
- void stepper_motor_movecontrol(void)
- {
- if(KEY1_Press)
- {
-
- while(KEY1_Press )
- {
-
- stepper_motor_move(-1,10);
- TIM11_delayms(10);
- }
- }
- if(KEY2_Press)
- {
- while(KEY2_Press )
- {
-
- stepper_motor_move(1,10);
- TIM11_delayms(10);
-
- }
-
- }
- }
- u8 buf3[10];
- u8 dangwei ;
- void dc_motor_speed(void)
- {
- static int speed = 0;
- if(KEY1_Press) //正常按下 或 抖动导致误识别
- {
- TIM11_delayms(50);//延时时间 > 弹片恢复时间,且越小越好
- if(KEY1_Press) //人为按键此时还是按下状态 或 此时抖动消失
- {
- while(KEY1_Press);//等待手松开
- dangwei++;
- speed += 20;
- if(dangwei == 6)
- {
- dangwei = 0;
- speed = 0;
- }
- }
- }
- if(KEY2_Press) //正常按下 或 抖动导致误识别
- {
- TIM11_delayms(50);//延时时间 > 弹片恢复时间,且越小越好
- if(KEY2_Press) //人为按键此时还是按下状态 或 此时抖动消失
- {
- while(KEY2_Press );//等待手松开
- dangwei--;
- speed -= 20;
- }
- }
- dc_motor_speedControl(speed);
- }
复制代码
所有资料51hei提供下载:
Ex03-步进电机 (4).7z
(387.74 KB, 下载次数: 19)
|