標題: 單片機步進電機代碼與Proteus仿真 [打印本頁]

作者: 人才市場ABC    時間: 2021-4-27 15:42
標題: 單片機步進電機代碼與Proteus仿真
  1. #include<reg51.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. #define out P2
  5. sbit pos=P0^0;
  6. sbit neg=P0^1;
  7. uchar code turn[]={0x02,0x06,0x04,0x0c,0x08,0x09,0x01,0x03};       //步進脈沖數(shù)組
  8. void delayms(uint j)  //延時
  9. {
  10.     uchar i;
  11.     for(;j>0;j--)
  12.     {
  13.          i=250;
  14.          while(--i);
  15.          i=250;
  16.          while(--i);
  17.          i=249;
  18.          while(--i);
  19.     }
  20. }
  21. void main()
  22. {

  23.       uchar i;
  24.       out=0x03;
  25.       while(1)
  26.       {
  27.             if(pos==0)
  28.             {
  29.                    if(i<8)
  30.                    {
  31.                        i++;
  32.                    }
  33.                    else
  34.                    {
  35.                        i=0;
  36.                    }
  37.                    out=turn[i];
  38.                    delayms(50);
  39.             }
  40.             if(neg==0)
  41.             {
  42.                  if(i>0)
  43.                  {
  44.                     i=i-1;
  45.                  }
  46.                  else
  47.                  {
  48.                      i=7;
  49.                  }

  50.             }
  51.           }

  52. }

復(fù)制代碼

51hei.png (9.98 KB, 下載次數(shù): 130)

51hei.png

51hei.png (10.47 KB, 下載次數(shù): 144)

51hei.png

步進電機.rar

28.02 KB, 下載次數(shù): 16, 下載積分: 黑幣 -5






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1