找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

stm32+28byj-48步進電機源程序

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:421049 發(fā)表于 2019-7-27 01:09 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
stm32驅(qū)動28byj-48步進電機

單片機源程序如下:
  1. #include "28byz48.h"
  2. #include "delay.h"

  3. #define uint unsigned int
  4.        
  5. //步進電機正反轉(zhuǎn)數(shù)組1
  6. uint16_t phasecw[4] ={0x0200,0x0100,0x0080,0x0040};// D-C-B-A   
  7. uint16_t phaseccw[4]={0x0040,0x0080,0x0100,0x0200};// A-B-C-D.

  8. void Delay_xms(uint x)
  9. {
  10. uint i,j;
  11. for(i=0;i<x;i++)
  12.   for(j=0;j<112;j++);
  13. }

  14. void Moto_Init(void)
  15. {
  16. GPIO_InitTypeDef GPIO_InitStructure;
  17. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  18. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 ;
  19. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  20. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  21. GPIO_Init(GPIOC,&GPIO_InitStructure);
  22. GPIO_ResetBits(GPIOC,GPIO_Pin_6 | GPIO_Pin_7 |GPIO_Pin_8 |GPIO_Pin_9 );
  23. // IN4: PC9   d
  24. // IN3: PC8   c
  25. // IN2: PC7   b
  26. // IN1: PC6   a
  27. }

  28. void Motorcw(uint speed)  
  29. {  
  30.     uint8_t i=0;  
  31.   
  32.     for(i=0;i<4;i++)  
  33.     {  
  34.         GPIO_Write(GPIOC,phasecw[i]);  
  35.         delay_ms(speed);  
  36.     }  
  37. }

  38. void Motorccw(int speed)  
  39. {  
  40.     uint8_t i;  
  41.     for(i=0;i<4;i++)  
  42.     {  
  43.         GPIO_Write(GPIOC,phaseccw[i]);  
  44.         delay_ms(speed);
  45.     }  
  46. }

  47. void MotorStop(void)  
  48. {
  49.          //GPIO_ResetBits(GPIOC,GPIO_Pin_6 | GPIO_Pin_7 |GPIO_Pin_8 |GPIO_Pin_9 );
  50. GPIO_Write(GPIOC,0x0000);  
  51. }


  52. //由于   *一個脈沖*   *輸出軸*  轉(zhuǎn)0.08789度(電機實轉(zhuǎn)0.08789*64=5.625度),即步進角為5.625度。
  53. //則轉(zhuǎn)完A-B-C-D為  *8個脈沖*  ,即0.08789*8=0.70312度。若稱A-B-C-D為一個周期,則j為需要的轉(zhuǎn)完angle角度所需的周期數(shù)。
  54. void Motorcw_angle(int angle,int speed)
  55. {
  56.         int i,j;
  57.         j=(int)(angle/0.70312);
  58.         for(i=0;i<j;i++)
  59.         {
  60.                 Motorcw(speed);
  61.         }
  62. }


  63. void Motorccw_angle(int angle,int speed)
  64. {
  65.         int i,j;
  66.         j=(int)(angle/0.70312);
  67.         for(i=0;i<j;i++)
  68.         {
  69.                 Motorccw(speed);
  70.         }
  71. }
復制代碼

所有資料51hei提供下載:
28BYJ-48(STM32).7z (181.07 KB, 下載次數(shù): 83)



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

使用道具 舉報

沙發(fā)
ID:1 發(fā)表于 2019-7-27 21:44 | 只看該作者
本帖需要重新編輯補全電路原理圖,源碼,詳細說明與圖片即可獲得100+黑幣(帖子下方有編輯按鈕)
回復

使用道具 舉報

板凳
ID:528105 發(fā)表于 2020-2-13 13:51 | 只看該作者
admin 發(fā)表于 2019-7-27 21:44
本帖需要重新編輯補全電路原理圖,源碼,詳細說明與圖片即可獲得100+黑幣(帖子下方有編輯按鈕)

謝謝分享,下載下來學習學習!
回復

使用道具 舉報

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

本版積分規(guī)則

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

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

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