欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2214|回復: 0
打印 上一主題 下一主題
收起左側(cè)

STM32F4步進電機正轉(zhuǎn)反轉(zhuǎn)控制實驗,謝謝采納

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:939826 發(fā)表于 2021-6-18 15:47 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
關于STM32F4的步進電機控制實驗,可自由控制步進電機正轉(zhuǎn)反轉(zhuǎn),可運用到智能家居。謝謝采納。

單片機源程序如下:

  1. /*
  2. ****************************************************************************************
  3. * INCLUDES (頭文件包含)
  4. ****************************************************************************************
  5. */
  6. #include "motor.h"
  7. #include "common.h"
  8. #include "key.h"
  9. /*
  10. ****************************************************************************************
  11. * CONSTANTS (常量定義)
  12. ****************************************************************************************
  13. */


  14. /*
  15. ****************************************************************************************
  16. * TYPEDEFS (類型定義)
  17. ****************************************************************************************
  18. */


  19. /*
  20. ****************************************************************************************
  21. * LOCAL VARIABLES (靜態(tài)變量)
  22. ****************************************************************************************
  23. */


  24. /*
  25. ****************************************************************************************
  26. * LOCAL FUNCTIONS DECLARE (靜態(tài)函數(shù)聲明)
  27. ****************************************************************************************
  28. */
  29. static void stepper_motor_cw(u32 xms);
  30. static void stepper_motor_ccw(u32 xms);

  31. /*
  32. ****************************************************************************************
  33. * LOCAL FUNCTIONS (靜態(tài)函數(shù))
  34. ****************************************************************************************
  35. */
  36. //步進電機正轉(zhuǎn)
  37. static void stepper_motor_cw(u32 xms)
  38. {
  39.           //A+
  40.     STEPPER_MOTOR_IN1_H;
  41.     STEPPER_MOTOR_IN2_L;
  42.           STEPPER_MOTOR_IN3_L;
  43.     STEPPER_MOTOR_IN4_L;
  44.     TIM11_delayms(xms);
  45.           //B-
  46.           STEPPER_MOTOR_IN1_L;
  47.     STEPPER_MOTOR_IN2_L;
  48.           STEPPER_MOTOR_IN3_L;
  49.     STEPPER_MOTOR_IN4_H;
  50.     TIM11_delayms(xms);
  51.           //A-
  52.           STEPPER_MOTOR_IN1_L;
  53.     STEPPER_MOTOR_IN2_H;
  54.           STEPPER_MOTOR_IN3_L;
  55.     STEPPER_MOTOR_IN4_L;
  56.     TIM11_delayms(xms);
  57.           //B+
  58.           STEPPER_MOTOR_IN1_L;
  59.     STEPPER_MOTOR_IN2_L;
  60.           STEPPER_MOTOR_IN3_H;
  61.     STEPPER_MOTOR_IN4_L;
  62.     TIM11_delayms(xms);
  63. }
  64. //翻轉(zhuǎn)
  65. static void stepper_motor_ccw(u32 xms)
  66. {
  67.           // A+  --  B- -- A-  -- B+
  68.           //A+
  69.     STEPPER_MOTOR_IN1_L;
  70.     STEPPER_MOTOR_IN2_L;
  71.           STEPPER_MOTOR_IN3_H;
  72.     STEPPER_MOTOR_IN4_L;
  73.     TIM11_delayms(xms);
  74.           //B-
  75.           STEPPER_MOTOR_IN1_L;
  76.     STEPPER_MOTOR_IN2_H;
  77.           STEPPER_MOTOR_IN3_L;
  78.     STEPPER_MOTOR_IN4_L;
  79.     TIM11_delayms(xms);
  80.           //A-
  81.           STEPPER_MOTOR_IN1_L;
  82.     STEPPER_MOTOR_IN2_L;
  83.           STEPPER_MOTOR_IN3_L;
  84.     STEPPER_MOTOR_IN4_H;
  85.     TIM11_delayms(xms);
  86.           //B+
  87.           STEPPER_MOTOR_IN1_H;
  88.     STEPPER_MOTOR_IN2_L;
  89.           STEPPER_MOTOR_IN3_L;
  90.     STEPPER_MOTOR_IN4_L;
  91.     TIM11_delayms(xms);
  92. }
  93. /*
  94. ****************************************************************************************
  95. * PUBLIC FUNCTIONS (全局函數(shù))
  96. ****************************************************************************************
  97. */
  98. /*
  99. ****************************************************************************************
  100. * Function: dc_motor_init
  101. * Description: 直流電機初始化
  102. * Input: None
  103. * Output: None
  104. * Return: None
  105. * Author: Dingwei
  106. * Others: IA        --        PA7/TIM3CH2
  107.                                         IB         --        PA6/TIM3CH1
  108. * Date of completion: 2020-12-11
  109. * Date of last modify: 2020-12-11
  110. ****************************************************************************************
  111. */
  112. void dc_motor_init(void)
  113. {
  114.         //本質(zhì)就是PA6和PA7復用為TIM3CHn(輸出比較功能)的初始化配置
  115.         TIM_OCInitTypeDef                         tim_OCInitStruct;
  116.         TIM_TimeBaseInitTypeDef tim_TimeBaseInitStruct;
  117.         GPIO_InitTypeDef                          gpio_InitStruct;
  118.         /*1.        激活通信相關GPIO引腳,將他們配置為復用模式,并映射到該通信接口上*/
  119.         RCC_AHB1PeriphClockCmd (DCMOTOR_IA_Periph_ENABLE | DCMOTOR_IB_Periph_ENABLE, ENABLE);
  120.         //對GPIO初始化句柄賦值(寫入配置信息)
  121.         gpio_InitStruct.GPIO_Mode                =        GPIO_Mode_AF ;
  122.         gpio_InitStruct.GPIO_OType        =        GPIO_OType_PP ;
  123.         gpio_InitStruct.GPIO_Pin                =        DCMOTOR_IA_Pinx ; //若操作多個引腳使用 | 連接起來
  124.         gpio_InitStruct.GPIO_PuPd                =        GPIO_PuPd_NOPULL ;
  125.         gpio_InitStruct.GPIO_Speed        =        GPIO_Low_Speed ;
  126.         GPIO_Init(DCMOTOR_IA_GPIOx,&gpio_InitStruct);
  127.        
  128.         gpio_InitStruct.GPIO_Pin                =        DCMOTOR_IB_Pinx ;
  129.         GPIO_Init(DCMOTOR_IB_GPIOx,&gpio_InitStruct);
  130.        
  131.         //將IA和IB引腳映射到相應的TIM上
  132.         GPIO_PinAFConfig (DCMOTOR_IA_GPIOx, DCMOTOR_IA_PinSourcex, DCMOTOR_IA_AF);
  133.         GPIO_PinAFConfig (DCMOTOR_IB_GPIOx, DCMOTOR_IB_PinSourcex, DCMOTOR_IB_AF);
  134.        
  135.         /*2.        TIM時基單元配置*/
  136.         //激活基本定時器
  137.         RCC_APB1PeriphClockCmd (DCMOTOR_TIM_ENABLE, ENABLE);
  138.         //設定定時時長  ==  (arr+1)/ (84000000/(psc+1)) == (arr+1)*(psc+1) /84000000
  139.         tim_TimeBaseInitStruct.TIM_ClockDivision                         = TIM_CKD_DIV1 ;
  140.         tim_TimeBaseInitStruct.TIM_CounterMode                                = TIM_CounterMode_Up ;
  141.         tim_TimeBaseInitStruct.TIM_Period                                                        =        10000-1 ; //重載值
  142.         tim_TimeBaseInitStruct.TIM_Prescaler                                        = 84-1 ; //分頻
  143.         tim_TimeBaseInitStruct.TIM_RepetitionCounter        = 0 ;//僅對TIM1和TIM8有用
  144.         TIM_TimeBaseInit (DCMOTOR_TIMx, &tim_TimeBaseInitStruct);
  145.         //考慮部分寄存器寫入緩沖特性,需要產(chǎn)生一次更新事件,之后清除標志位
  146.         TIM_GenerateEvent (DCMOTOR_TIMx, TIM_EventSource_Update);
  147.         TIM_ClearFlag (DCMOTOR_TIMx, TIM_FLAG_Update);
  148.         //計數(shù)器清零
  149.         TIM_SetCounter (DCMOTOR_TIMx, 0);
  150.        
  151.         /*3. TIM3OC1和OC2初始化*/
  152.         //tim_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Reset ;
  153.         //tim_OCInitStruct.TIM_OCNIdleState = TIM_OCNIdleState_Reset ;
  154.         //tim_OCInitStruct.TIM_OCNPolarity = TIM_OCNPolarity_High ;
  155.         //tim_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Disable ;
  156.         tim_OCInitStruct.TIM_OCMode                         = TIM_OCMode_PWM1 ;
  157.         tim_OCInitStruct.TIM_OCPolarity         = TIM_OCPolarity_High ;//有效電平為高電平
  158.         tim_OCInitStruct.TIM_OutputState         = TIM_OutputState_Enable ;//輸出通道使能
  159.         tim_OCInitStruct.TIM_Pulse                                =        0 ;//比較值初值
  160.         TIM_OC1Init (DCMOTOR_TIMx, &tim_OCInitStruct);
  161.         TIM_OC2Init (DCMOTOR_TIMx, &tim_OCInitStruct);
  162.        
  163.         /*4. 使能TIM(開啟計數(shù))*/
  164.         TIM_Cmd(DCMOTOR_TIMx,ENABLE);
  165. }

  166. /*
  167. ****************************************************************************************
  168. * Function: dc_motor_speedControl
  169. * Description: 直流電機速度控制函數(shù)
  170. * Input: int speedPercent        速度百分比(有效數(shù)據(jù)在-100~+100范圍)
  171. * Output: None
  172. * Return: None
  173. * Author: Dingwei
  174. * Others: 速度的正負表示轉(zhuǎn)動方向
  175. * Date of completion: 2020-12-10
  176. * Date of last modify: 2020-12-10
  177. ****************************************************************************************
  178. */
  179. void dc_motor_speedControl(int speedPercent)
  180. {
  181.         if(speedPercent > 0)        //正轉(zhuǎn)
  182.         {
  183.                 if(speedPercent > 100)
  184.                 {
  185.                         speedPercent = 100;
  186.                 }
  187.                 DCMOTOR_IA_DUTYCYCLE(speedPercent);
  188.                 DCMOTOR_IB_DUTYCYCLE(0);
  189.         }
  190.         else if(speedPercent < 0)        //反轉(zhuǎn)
  191.         {
  192.                 if(speedPercent < -100)
  193.                 {
  194.                         speedPercent = -100;
  195.                 }
  196.                 DCMOTOR_IA_DUTYCYCLE(0);
  197.                 DCMOTOR_IB_DUTYCYCLE(-1*speedPercent);
  198.         }
  199.         else                                //停止
  200.         {
  201.                 DCMOTOR_IA_DUTYCYCLE(0);
  202.                 DCMOTOR_IB_DUTYCYCLE(0);
  203.         }
  204. }

  205. /*
  206. ****************************************************************************************
  207. * Function: stepper_motor_init
  208. * Description: 步進電機初始化
  209. * Input: None
  210. * Output: None
  211. * Return: None
  212. * Author: Dingwei
  213. * Others: PC3~0對應IN1~4,推挽輸出
  214. * Date of completion: 2020-12-10
  215. * Date of last modify: 2020-12-10
  216. ****************************************************************************************
  217. */
  218. void stepper_motor_init(void)
  219. {
  220.         //0. 激活GPIOC
  221.         RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOC, ENABLE);
  222.         //1. 創(chuàng)建GPIO初始化句柄
  223.         GPIO_InitTypeDef  gpio_InitStruct;
  224.         //2. 對GPIO初始化句柄賦值(寫入配置信息)
  225.         gpio_InitStruct.GPIO_Mode                =        GPIO_Mode_OUT ;
  226.         gpio_InitStruct.GPIO_OType        =        GPIO_OType_PP ;
  227.         gpio_InitStruct.GPIO_Pin                =        GPIO_Pin_3 | GPIO_Pin_2 | GPIO_Pin_1 | GPIO_Pin_0; //若操作多個引腳使用 | 連接起來
  228.         gpio_InitStruct.GPIO_PuPd                =        GPIO_PuPd_NOPULL ;
  229.         gpio_InitStruct.GPIO_Speed        =        GPIO_Low_Speed ;
  230.         //3. 庫函數(shù)GPIO初始化
  231.         GPIO_Init(GPIOC,&gpio_InitStruct);
  232.         //4. 初始時,LED1~3熄滅
  233.         GPIO_SetBits(GPIOC, GPIO_Pin_3);
  234.         GPIO_SetBits(GPIOC, GPIO_Pin_2);
  235.         GPIO_SetBits(GPIOC, GPIO_Pin_1);
  236.         GPIO_SetBits(GPIOC, GPIO_Pin_0);
  237. }

  238. /*
  239. ****************************************************************************************
  240. * Function: stepper_motor_move
  241. * Description: 步進電機運動
  242. * Input: int cycle 周期,正負表示正反轉(zhuǎn)
  243.                                 采用單四拍,每四拍對應一個周期
  244.                                         u32 xms   每個節(jié)拍的間隔
  245. * Output: None
  246. * Return: None
  247. * Author: Dingwei
  248. * Others: 單四拍
  249. * Date of completion: 2020-12-10
  250. * Date of last modify: 2020-12-10
  251. ****************************************************************************************
  252. */
  253. void stepper_motor_move(int cycle,u32 xms)
  254. {
  255.         int i;
  256.         if(cycle > 0)
  257.         {
  258.                 for(i=0 ; i <cycle ; i++)
  259.                 {
  260.                         stepper_motor_cw(xms);
  261.                 }
  262.         }
  263.         else if(cycle < 0)
  264.         {
  265.                 cycle *= -1;
  266.                 for(i=0 ; i <cycle ; i++)
  267.                 {
  268.                         stepper_motor_ccw(xms);
  269.                 }
  270.         }
  271.         else //cycle == 0
  272.         {
  273.                 STEPPER_MOTOR_IN1_L;
  274.                 STEPPER_MOTOR_IN2_L;
  275.                 STEPPER_MOTOR_IN3_L;
  276.                 STEPPER_MOTOR_IN4_L;
  277.         }
  278. }
  279. /*******************************************************************************
  280. * 函數(shù)名:
  281. * 函數(shù)參數(shù)  
  282. * 功能描述:
  283. * 參數(shù)說明:  
  284. * 返回值說明
  285. * 修改記錄:
  286.          IA PA7  IB PA6

  287.         stepper_motor_move(-185,1);
  288.   stepper_motor_move(185,1);
  289. *******************************************************************************/


  290. void stepper_motor_movecontrol(void)
  291. {

  292.         if(KEY1_Press)
  293.         {
  294.        
  295.                         while(KEY1_Press )
  296.                         {
  297.        
  298.                                 stepper_motor_move(-1,10);
  299.                                  TIM11_delayms(10);
  300.                         }
  301.          }
  302.         if(KEY2_Press)
  303.         {
  304.                          while(KEY2_Press )
  305.                                 {
  306.                        
  307.                                         stepper_motor_move(1,10);
  308.                                         TIM11_delayms(10);
  309.                
  310.                                 }
  311.        
  312.         }       
  313. }

  314. u8 buf3[10];
  315. u8 dangwei ;












  316. void dc_motor_speed(void)
  317. {
  318.         static int speed = 0;
  319.            if(KEY1_Press)        //正常按下    或  抖動導致誤識別
  320.         {
  321.                 TIM11_delayms(50);//延時時間 > 彈片恢復時間,且越小越好
  322.                 if(KEY1_Press)        //人為按鍵此時還是按下狀態(tài)  或  此時抖動消失
  323.                 {
  324.                         while(KEY1_Press);//等待手松開
  325.                                                 dangwei++;
  326.                               speed += 20;
  327.                         if(dangwei == 6)
  328.                         {
  329.                                                 dangwei = 0;
  330.                               speed = 0;
  331.                         }
  332.                 }
  333.         }
  334.                    if(KEY2_Press)        //正常按下    或  抖動導致誤識別
  335.         {
  336.                 TIM11_delayms(50);//延時時間 > 彈片恢復時間,且越小越好
  337.                 if(KEY2_Press)        //人為按鍵此時還是按下狀態(tài)  或  此時抖動消失
  338.                 {
  339.                         while(KEY2_Press );//等待手松開
  340.                                                 dangwei--;
  341.                               speed -= 20;
  342.                 }
  343.         }
  344.                 dc_motor_speedControl(speed);
  345. }
復制代碼

所有資料51hei提供下載:
Ex03-步進電機 (4).7z (387.74 KB, 下載次數(shù): 19)


評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表