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

QQ登錄

只需一步,快速開始

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

按鍵控制電動(dòng)機(jī)的速度和轉(zhuǎn)向

[復(fù)制鏈接]
ID:154463 發(fā)表于 2016-12-13 17:07 | 顯示全部樓層 |閱讀模式
按鍵控制電動(dòng)機(jī)的速度和轉(zhuǎn)向

仿真圖

仿真圖


源程序:
  1. #include <REG52.H>
  2. #define uchar unsigned char
  3. #define uint unsigned int  
  4. #define ulong unsigned long
  5. #define CLK_WISE 0//順時(shí)針方向轉(zhuǎn)動(dòng)
  6. #define INVERSE 1//逆時(shí)針方向轉(zhuǎn)動(dòng)
  7. #define HIGH 2//高電平
  8. #define LOW 1//低電平
  9. #define NULL 0//低電平
  10. sbit CTRL0=P2^0;//CTRL0為高電平,CTRL1為低電平時(shí)順時(shí)針方向轉(zhuǎn)動(dòng)
  11. sbit CTRL1=P2^1;//CTRL0為低電平,CTRL1為高電平時(shí)逆時(shí)針方向轉(zhuǎn)動(dòng)
  12. uchar levelflag=0;//高低電平標(biāo)志,取值為HIGH或LOW,分別代表高電平和低電平,或?yàn)镹ULL
  13. bit direction=CLK_WISE;/***方向標(biāo)志,取值為CLK_WISE 或INVERSE*/
  14. static uchar speedcount=0;//加速標(biāo)志,越大轉(zhuǎn)速越快,最大到10,然后回歸到0,循環(huán)。
  15.                                                 //程序中可以依據(jù)它來改變占空比
  16. uchar highcount;        //記錄高電平定時(shí)次數(shù)

  17. /***定時(shí)器t0***/
  18. void time0(void) interrupt 1 using 1
  19. {       
  20.         TH0=0xff;
  21.         TL0=0xCE;
  22.         highcount++;//記錄一出次數(shù)

  23.         //頻率為固定的2kHZ左右,只是占空比發(fā)生變化
  24.         if(highcount<=speedcount)
  25.                 levelflag=HIGH;//高電平次數(shù)
  26.         if((highcount>speedcount)&&(highcount<=10))
  27.                 levelflag=LOW;//低電平次數(shù)       
  28.         if(highcount==10) //最大為10,然后從0開始循環(huán)。
  29.                 highcount=0;
  30.        
  31. }

  32. /****改變轉(zhuǎn)向標(biāo)志*****/
  33. void int1_srv (void) interrupt 2 using 2
  34. {
  35.         if(INT1==0)
  36.         {
  37.                 while(!INT1);
  38.                  direction=!direction;
  39.         }
  40. }

  41. /*******中斷,調(diào)節(jié)占空比********/
  42. void change(void) interrupt 0 using 0
  43. {
  44.         if(INT0==0)
  45.         {
  46.                 while(!INT0);
  47.                  speedcount++;//記錄加速次數(shù)               
  48.                 if(speedcount>10)
  49.                         speedcount=0;//最大為10,然后從0開始循環(huán)。
  50.         }
  51. }

  52. /****延時(shí)****/
  53. void delay(uchar i)
  54. {
  55.         while(i--)
  56.                 ;
  57. }

  58. /*****改變轉(zhuǎn)向*****/
  59. void run(void)
  60. {
  61.         if(direction==CLK_WISE)        //順時(shí)針轉(zhuǎn)
  62.         {
  63.                 CTRL1=0;
  64.                 if(levelflag==HIGH)//高電平
  65.                 {
  66.                         levelflag=NULL;
  67.                         CTRL0=1;
  68.                 }
  69.                 if(levelflag==LOW)//低電平
  70.                 {
  71.                         levelflag=NULL;
  72.                         CTRL0=0;
  73.                 }
  74.         }
  75.         if(direction==INVERSE)        //逆時(shí)針轉(zhuǎn)
  76.         {
  77.                 CTRL0=0;
  78.                 if(levelflag==HIGH)//高電平
  79.                 {
  80.                         levelflag=NULL;
  81.                         CTRL1=1;
  82.                 }
  83.                 if(levelflag==LOW)//低電平
  84.                 {
  85.                         levelflag=NULL;
  86.                         CTRL1=0;
  87.                 }
  88.         }
  89.        
  90. }
  91. void main()
  92. {       
  93.         EA=1;
  94.         TMOD=0x01;
  95.         ET0=1;//定時(shí)器0初始化
  96.         TR0=1;

  97.         EX0=1;
  98.         IT0=1;//外部中斷0

  99.         EX1=1;
  100.         IT1=1;//外部中斷1

  101.         TH0=0xff;
  102.         TL0=0xCE;//定時(shí)器0初始值

  103.         while(1)
  104.         {
  105.                 run();
  106.         }
  107. }
復(fù)制代碼
0.png

所有資料下載:
例6.7.zip (51.33 KB, 下載次數(shù): 28)

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

ID:1 發(fā)表于 2017-1-7 00:23 | 顯示全部樓層
51黑有你更精彩。!
回復(fù)

使用道具 舉報(bào)

ID:563570 發(fā)表于 2020-1-31 22:09 來自觸屏版 | 顯示全部樓層
真好,去試一試
回復(fù)

使用道具 舉報(bào)

ID:476093 發(fā)表于 2020-2-6 09:49 | 顯示全部樓層
去試試,我也學(xué)習(xí)一下
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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