找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

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

單片機(jī)按鍵控制步進(jìn)電機(jī)的源碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:342381 發(fā)表于 2018-5-31 18:37 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
單片機(jī)源程序如下:
  1. /*接線(xiàn)方式:
  2. IN1 ---- P00
  3. IN2 ---- P01
  4. IN3 ---- P02
  5. IN4 ---- P03
  6. *********************/
  7. #include<reg52.h>
  8. #define uchar unsigned char
  9. #define uint  unsigned int
  10. #define MotorData P0                    //步進(jìn)電機(jī)控制接口定義
  11. uchar phasecw[4] ={0x08,0x04,0x02,0x01};//正轉(zhuǎn) 電機(jī)導(dǎo)通相序 D-C-B-A
  12. uchar phaseccw[4]={0x01,0x02,0x04,0x08};//反轉(zhuǎn) 電機(jī)導(dǎo)通相序 A-B-C-D
  13. uchar count,count1=20;//定義全局變量
  14. sbit s1=P1^0;// 順時(shí)針轉(zhuǎn)
  15. sbit s2=P1^1;// 逆時(shí)針轉(zhuǎn)
  16. sbit s3=P1^2;// 停止
  17. sbit s4=P1^3;// 加速
  18. sbit s5=P1^4;// 減速
  19. //ms延時(shí)函數(shù)
  20. void Delay_xms(uint x)
  21. {
  22. uint i,j;
  23. for(i=0;i<x;i++)
  24.   for(j=0;j<112;j++);
  25. }
  26. //順時(shí)針轉(zhuǎn)動(dòng)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  27. void MotorCW(void)
  28. {
  29. uchar i;
  30. for(i=0;i<4;i++)
  31.   {
  32.    MotorData=phasecw[i];
  33.    Delay_xms(count1);//轉(zhuǎn)速調(diào)節(jié)
  34.   }
  35. }
  36. //逆時(shí)針轉(zhuǎn)動(dòng)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  37. void MotorCCW(void)
  38. {
  39. uchar i;
  40. for(i=0;i<4;i++)
  41.   {
  42.    MotorData=phaseccw[i];
  43.    Delay_xms(count1);//轉(zhuǎn)速調(diào)節(jié)
  44.   }
  45. }
  46. //停止轉(zhuǎn)動(dòng)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  47. void MotorStop(void)
  48. {
  49. MotorData=0x00;
  50. }
  51. //按鍵掃描
  52. void keyscan()
  53. {
  54.    if(s1==0)
  55.     {Delay_xms(5);
  56.      if(s1==0)
  57.        {while(!s1);
  58.          count=1;       //選擇順時(shí)針轉(zhuǎn)動(dòng)
  59.        }
  60.     }
  61.    if(s2==0)
  62.     {Delay_xms(5);
  63.      if(s2==0)
  64.        {while(!s2);
  65.          count=2;       //選擇逆時(shí)針轉(zhuǎn)動(dòng)
  66.        }
  67.     }
  68.     if(s3==0)
  69.     {Delay_xms(5);
  70.      if(s3==0)
  71.        {while(!s3);
  72.         count=3;       //選擇停止轉(zhuǎn)動(dòng)
  73.        }
  74.     }  
  75.     if(s4==0)
  76.     {Delay_xms(5);
  77.      if(s4==0)
  78.        {while(!s4);
  79.         count1--;      //count減小會(huì)加速
  80.          if(count1<=4)
  81.             count1=4;
  82.        }
  83.     }  
  84.     if(s5==0)
  85.     {Delay_xms(5);
  86.      if(s5==0)
  87.        {while(!s5);
  88.         count1++;       //count增加會(huì)減速
  89.          if(count1>=50)
  90.             count1=50;
  91.        }
  92.     }  
  93. }
  94. //主函數(shù)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  95. void main(void)
  96. {

  97. Delay_xms(50);//等待系統(tǒng)穩(wěn)定
  98. while(1)
  99. {  keyscan();
  100.           switch(count)
  101.         {
  102.                 case 1:        MotorCW();   //順時(shí)針轉(zhuǎn)動(dòng)  
  103.                                 break;
  104.                 case 2:        MotorCCW();  //逆時(shí)針轉(zhuǎn)動(dòng)
  105.                                 break;
  106.                 case 3:        MotorStop();  //停止轉(zhuǎn)動(dòng)
  107.                                 break;
  108.                  
  109.     }
  110. }
  111. }
復(fù)制代碼

所有資料51hei提供下載:
按鍵控制步進(jìn)電機(jī).zip (1.64 MB, 下載次數(shù): 40)



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

使用道具 舉報(bào)

沙發(fā)
ID:492517 發(fā)表于 2019-4-17 23:08 | 只看該作者
樓主,你這個(gè)有電路圖嗎?或者說(shuō)你這個(gè)按鍵需要外接上拉電阻嗎?
回復(fù)

使用道具 舉報(bào)

板凳
ID:492517 發(fā)表于 2019-4-17 23:09 | 只看該作者
請(qǐng)問(wèn)這個(gè)按鍵需要外加上拉電阻嗎?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表