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

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

QQ登錄

只需一步,快速開始

搜索
查看: 2435|回復(fù): 1
收起左側(cè)

求大家?guī)臀铱纯磫纹瑱C(jī)程序 如何改成讓兩個(gè)電機(jī)同時(shí)轉(zhuǎn)動(dòng)且一個(gè)正轉(zhuǎn)一個(gè)反轉(zhuǎn)

[復(fù)制鏈接]
回帖獎(jiǎng)勵(lì) 10 黑幣 回復(fù)本帖可獲得 10 黑幣獎(jiǎng)勵(lì)! 每人限 1 次(中獎(jiǎng)概率 10%)
ID:919394 發(fā)表于 2021-5-14 12:31 | 顯示全部樓層 |閱讀模式
求大家?guī)臀铱纯慈绾胃某勺寖蓚(gè)電機(jī)同時(shí)轉(zhuǎn)動(dòng)且一個(gè)正轉(zhuǎn)一個(gè)反轉(zhuǎn)
捕獲2.PNG

單片機(jī)源程序如下:
  1. /*******************************************************************************

  2. * 實(shí) 驗(yàn) 名: 步進(jìn)電機(jī)實(shí)驗(yàn)

  3. * 使用的IO: 電機(jī)用P2口,按鍵使用P1.0、P1.1、P1.2、P1.3、P1.4、P1.5

  4. * 按鍵功能:K1;正轉(zhuǎn)  K2;反轉(zhuǎn)  K3;低速轉(zhuǎn)動(dòng)  K4;高速轉(zhuǎn)動(dòng)  K5:電機(jī)切換  K6;停止

  5. * 實(shí)驗(yàn)效果: 按下K1鍵,順時(shí)針轉(zhuǎn),按下K2鍵,逆時(shí)針轉(zhuǎn),按下K3鍵,低速,按下K4鍵,高速。
  6.                         按下K5鍵,轉(zhuǎn)動(dòng)的電機(jī)停止,并切換到控制另一個(gè)電機(jī)
  7.                         按下K6鍵,轉(zhuǎn)動(dòng)的電機(jī)停止,按下K1或K2時(shí),剛才停止的電機(jī)繼續(xù)轉(zhuǎn)動(dòng)


  8. *******************************************************************************/

  9. #include "reg52.h"


  10. //電機(jī)IO

  11. #define GPIO_MOTOR P2            //宏定義 對(duì)端口進(jìn)行操作,定義電機(jī)接口為P1

  12. //sbit F1 = P2^0;

  13. //sbit F2 = P2^1;

  14. //sbit F3 = P2^2;

  15. //sbit F4 = P2^3;

  16. //sbit F5 = P2^4;

  17. //sbit F6 = P2^5;

  18. //sbit F7 = P2^6;

  19. //sbit F8 = P2^7;                //另種操作,位操作,定義電機(jī)接口為P1.0~p1.7

  20. //按鍵IO

  21. sbit K1=P1^0;          //正轉(zhuǎn)

  22. sbit K2=P1^1;          //反轉(zhuǎn)

  23. sbit K3=P1^2;          // 減速

  24. sbit K4=P1^3;          // 加速

  25. sbit K5=P1^4;          // 電機(jī)切換

  26. sbit K6=P1^5;          // 停止

  27. unsigned char code FFW_X[8]= {0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9}; //X反轉(zhuǎn)順序

  28. unsigned char code FFZ_X[8]= {0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1}; //X正轉(zhuǎn)順序

  29. unsigned char code FFW_Y[8]= {0x1f,0x3f,0x2f,0x6f,0x4f,0xcf,0x8f,0x9f}; //Y反轉(zhuǎn)順序

  30. unsigned char code FFZ_Y[8]= {0x9f,0x8f,0xcf,0x4f,0x6f,0x2f,0x3f,0x1f}; //Y正轉(zhuǎn)順序

  31. unsigned char Direction,Speed,X_Y=0,stop=0;           //  Direction  轉(zhuǎn)向標(biāo)志位,Speed 延時(shí)函數(shù)的參數(shù),通過改變它大小來控制電機(jī)轉(zhuǎn)速
  32. //X_Y 電機(jī)的參數(shù)  X_Y=0為Y電機(jī) X_Y=1為X電機(jī)
  33. void Delay(unsigned int t);                                           //stop為控制電機(jī)停止的標(biāo)志位,但它為1時(shí),電機(jī)停止

  34. void Motor();                                                                   //電機(jī)旋轉(zhuǎn)函數(shù)


  35. /*******************************************************************************

  36. * 函 數(shù) 名 : main

  37. * 函數(shù)功能 : 主函數(shù)

  38. * 輸    入 : 無

  39. * 輸    出 : 無

  40. *******************************************************************************/


  41. void main(void)

  42. {

  43.     unsigned char i;

  44.     Speed=20;                   //Speed 延時(shí)函數(shù)的參數(shù),通過改變它大小來控制電機(jī)轉(zhuǎn)速

  45.     while(1)

  46.     {

  47.         /********************檢測(cè)按鍵K1是否按下***********************/
  48.         if(K1==0) //檢測(cè)按鍵K1是否按下

  49.         {

  50.             Delay(1); //消除抖動(dòng)

  51.             if(K1==0)

  52.             {

  53.                 Direction=1;           //Direction 轉(zhuǎn)向標(biāo)志位,為1時(shí)為正轉(zhuǎn)
  54.                 stop=0;                   //按下K1時(shí),對(duì)stop停止標(biāo)志位清零,如果之前按下停止鍵,清零之后才能啟動(dòng)

  55.             }

  56.             while((i<200)&&(K1==0))//檢測(cè)按鍵是否松開

  57.             {

  58.                 Delay(1);

  59.                 i++;

  60.             }

  61.             i=0;

  62.         }

  63.         /********************檢測(cè)按鍵K2是否按下***********************/

  64.         if(K2==0) //檢測(cè)按鍵K2是否按下

  65.         {

  66.             Delay(1); //消除抖動(dòng)

  67.             if(K2==0)

  68.             {

  69.                 Direction=2;                  //Direction 轉(zhuǎn)向標(biāo)志位,為2時(shí)為反轉(zhuǎn)
  70.                 stop=0;                          //按下K1時(shí),也對(duì)stop停止標(biāo)志位清零,如果之前按下停止鍵,清零之后才能啟動(dòng)

  71.             }

  72.             while((i<200)&&(K2==0))//檢測(cè)按鍵是否松開

  73.             {

  74.                 Delay(1);

  75.                 i++;

  76.             }

  77.             i=0;

  78.         }

  79.         /********************檢測(cè)按鍵K3是否按下***********************/
  80.         if(K3==0)//檢測(cè)按鍵K3是否按下

  81.         {

  82.             Delay(1);//消除抖動(dòng)

  83.             if(K3==0)

  84.             {

  85.                 Speed=60;                                   //Speed 延時(shí)函數(shù)的參數(shù),通過改變它大小來控制電機(jī)轉(zhuǎn)速
  86.                 //87行代碼定義Speed=30;但按鍵K3按下后,Speed=60,這時(shí)轉(zhuǎn)速就會(huì)降下來
  87.             }

  88.             while((i<200)&&(K3==0))//檢測(cè)按鍵是否松開

  89.             {

  90.                 Delay(1);

  91.                 i++;

  92.             }

  93.             i=0;

  94.         }


  95.         /********************檢測(cè)按鍵K3是否按下***********************/

  96.         if(K4==0) //檢測(cè)按鍵K4是否按下

  97.         {

  98.             Delay(1); //消除抖動(dòng)

  99.             if(K4==0)

  100.             {

  101.                 Speed=30;                         //Speed 延時(shí)函數(shù)的參數(shù),通過改變它大小來控制電機(jī)轉(zhuǎn)速
  102.                 //87行代碼定義Speed=30;但按鍵K4按下后,Speed=20,這時(shí)轉(zhuǎn)速就會(huì)降下來升上去
  103.             }

  104.             while((i<200)&&(K4==0))//檢測(cè)按鍵是否松開

  105.             {

  106.                 Delay(1);

  107.                 i++;

  108.             }

  109.             i=0;

  110.         }

  111.         if(K5==0)//檢測(cè)按鍵K1是否按下

  112.         {

  113.             Delay(1);//消除抖動(dòng)

  114.             if(K5==0)

  115.             {
  116.                 Direction=0;                                   //X_Y 電機(jī)的參數(shù)  X_Y=0為Y電機(jī) X_Y=1為X電機(jī)
  117.                 if(X_Y==0)                                   //當(dāng)切換電機(jī)時(shí),對(duì)Direction=0;對(duì)標(biāo)志位清零,防止上個(gè)電機(jī)的轉(zhuǎn)向影響本次所控電機(jī)
  118.                     X_Y=1;
  119.                 else
  120.                     X_Y=0;

  121.             }

  122.             while((i<200)&&(K5==0))//檢測(cè)按鍵是否松開

  123.             {

  124.                 Delay(1);

  125.                 i++;

  126.             }

  127.             i=0;

  128.         }

  129.         if(K6==0)//檢測(cè)按鍵K1是否按下

  130.         {

  131.             Delay(1);//消除抖動(dòng)

  132.             if(K6==0)

  133.             {
  134.                 stop=1;                                   //stop為控制電機(jī)停止的標(biāo)志位,但它為1時(shí),電機(jī)停止

  135.             }

  136.             while((i<200)&&(K6==0))//檢測(cè)按鍵是否松開

  137.             {

  138.                 Delay(1);

  139.                 i++;

  140.             }

  141.             i=0;

  142.         }

  143.         Motor();

  144.     }

  145. }

  146. /*******************************************************************************

  147. * 函 數(shù) 名: Motor

  148. * 函數(shù)功能: 電機(jī)旋轉(zhuǎn)函數(shù)

  149. * 輸    入: 無

  150. * 輸    出: 無

  151. *******************************************************************************/


  152. void Motor()

  153. {

  154.     unsigned char i;

  155.     for(i=0; i<8; i++)

  156.     {

  157.         if(Direction==1&&X_Y==0&&stop==0)                 //正轉(zhuǎn)&Y電機(jī)&電機(jī)沒按下停止

  158.             GPIO_MOTOR = FFW_Y[i]&0xf0;//取數(shù)據(jù),‘&’按位與

  159.         if(Direction==2&&X_Y==0&&stop==0)                 //反轉(zhuǎn)&Y電機(jī)&電機(jī)沒按下停止

  160.             GPIO_MOTOR = FFZ_Y[i]&0xf0;

  161.         if(Direction==1&&X_Y!=0&&stop==0)                 //正轉(zhuǎn)&X電機(jī)&電機(jī)沒按下停止

  162.             GPIO_MOTOR = FFW_X[i]&0x0F;//取數(shù)據(jù)

  163.         if(Direction==2&&X_Y!=0&&stop==0)                 //反轉(zhuǎn)&X電機(jī)&電機(jī)沒按下停止

  164.             GPIO_MOTOR = FFZ_X[i]&0x0f;

  165.         Delay(Speed); //調(diào)節(jié)轉(zhuǎn)速

  166.     }

  167. }

  168. /*******************************************************************************

  169. * 函 數(shù) 名  : Delay

  170. * 函數(shù)功能  : 延時(shí)

  171. * 輸    入  : t

  172. * 輸    出  : 無

  173. *******************************************************************************/

  174. void Delay(unsigned int t)

  175. {

  176.     unsigned int k;

  177.     while(t--)

  178.     {

  179.         for(k=0; k<45; k++)

  180.         { }

  181.     }

  182. }
復(fù)制代碼


回復(fù)

使用道具 舉報(bào)

ID:844772 發(fā)表于 2021-5-14 14:44 | 顯示全部樓層
我就喜歡回答不用動(dòng)腦的事。
/*******************************************************************************

* 函 數(shù) 名: Motor2

* 函數(shù)功能: 電機(jī)X正,Y反旋轉(zhuǎn)函數(shù)

* 輸    入: 無

* 輸    出: 無

*******************************************************************************/


void Motor2()

{

    unsigned char i;
    for(i=0; i<8; i++)
    {
          GPIO_MOTOR = FFZ_Y&0xf0;  //反轉(zhuǎn)&Y電機(jī)
          GPIO_MOTOR = FFW_X&0x0F;//取數(shù)據(jù) 正轉(zhuǎn)&X電機(jī)
          Delay(Speed); //調(diào)節(jié)轉(zhuǎn)速
    }
}

評(píng)分

參與人數(shù) 1黑幣 +20 收起 理由
admin + 20 回帖助人的獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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