關于STM32F4的步進電機控制實驗,可自由控制步進電機正轉(zhuǎn)反轉(zhuǎn),可運用到智能家居。謝謝采納。
單片機源程序如下:
- /*
- ****************************************************************************************
- * INCLUDES (頭文件包含)
- ****************************************************************************************
- */
- #include "motor.h"
- #include "common.h"
- #include "key.h"
- /*
- ****************************************************************************************
- * CONSTANTS (常量定義)
- ****************************************************************************************
- */
- /*
- ****************************************************************************************
- * TYPEDEFS (類型定義)
- ****************************************************************************************
- */
- /*
- ****************************************************************************************
- * LOCAL VARIABLES (靜態(tài)變量)
- ****************************************************************************************
- */
- /*
- ****************************************************************************************
- * LOCAL FUNCTIONS DECLARE (靜態(tài)函數(shù)聲明)
- ****************************************************************************************
- */
- static void stepper_motor_cw(u32 xms);
- static void stepper_motor_ccw(u32 xms);
- /*
- ****************************************************************************************
- * LOCAL FUNCTIONS (靜態(tài)函數(shù))
- ****************************************************************************************
- */
- //步進電機正轉(zhuǎn)
- 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);
- }
- //翻轉(zhuǎn)
- 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 (全局函數(shù))
- ****************************************************************************************
- */
- /*
- ****************************************************************************************
- * 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)
- {
- //本質(zhì)就是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);
- //考慮部分寄存器寫入緩沖特性,需要產(chǎn)生一次更新事件,之后清除標志位
- TIM_GenerateEvent (DCMOTOR_TIMx, TIM_EventSource_Update);
- TIM_ClearFlag (DCMOTOR_TIMx, TIM_FLAG_Update);
- //計數(shù)器清零
- 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(開啟計數(shù))*/
- TIM_Cmd(DCMOTOR_TIMx,ENABLE);
- }
- /*
- ****************************************************************************************
- * Function: dc_motor_speedControl
- * Description: 直流電機速度控制函數(shù)
- * Input: int speedPercent 速度百分比(有效數(shù)據(jù)在-100~+100范圍)
- * Output: None
- * Return: None
- * Author: Dingwei
- * Others: 速度的正負表示轉(zhuǎn)動方向
- * Date of completion: 2020-12-10
- * Date of last modify: 2020-12-10
- ****************************************************************************************
- */
- void dc_motor_speedControl(int speedPercent)
- {
- if(speedPercent > 0) //正轉(zhuǎn)
- {
- if(speedPercent > 100)
- {
- speedPercent = 100;
- }
- DCMOTOR_IA_DUTYCYCLE(speedPercent);
- DCMOTOR_IB_DUTYCYCLE(0);
- }
- else if(speedPercent < 0) //反轉(zhuǎn)
- {
- 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. 創(chuàng)建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. 庫函數(shù)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 周期,正負表示正反轉(zhuǎn)
- 采用單四拍,每四拍對應一個周期
- u32 xms 每個節(jié)拍的間隔
- * 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;
- }
- }
- /*******************************************************************************
- * 函數(shù)名:
- * 函數(shù)參數(shù)
- * 功能描述:
- * 參數(shù)說明:
- * 返回值說明
- * 修改記錄:
- 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) //人為按鍵此時還是按下狀態(tài) 或 此時抖動消失
- {
- while(KEY1_Press);//等待手松開
- dangwei++;
- speed += 20;
- if(dangwei == 6)
- {
- dangwei = 0;
- speed = 0;
- }
- }
- }
- if(KEY2_Press) //正常按下 或 抖動導致誤識別
- {
- TIM11_delayms(50);//延時時間 > 彈片恢復時間,且越小越好
- if(KEY2_Press) //人為按鍵此時還是按下狀態(tài) 或 此時抖動消失
- {
- while(KEY2_Press );//等待手松開
- dangwei--;
- speed -= 20;
- }
- }
- dc_motor_speedControl(speed);
- }
復制代碼
所有資料51hei提供下載:
Ex03-步進電機 (4).7z
(387.74 KB, 下載次數(shù): 19)
2021-6-18 16:48 上傳
點擊文件名下載附件
STM32F4 步進電機控制文件 下載積分: 黑幣 -5
|