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

QQ登錄

只需一步,快速開始

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

步進(jìn)電機(jī)正反轉(zhuǎn)調(diào)速LCD顯示實(shí)驗(yàn)程序

[復(fù)制鏈接]
ID:71259 發(fā)表于 2014-12-30 01:32 | 顯示全部樓層 |閱讀模式
  1. #include <reg52.h>       //51芯片管腳定義頭文件
  2. #include <intrins.h>  //內(nèi)部包含延時(shí)函數(shù) _nop_();

  3. #define uchar unsigned char
  4. #define uint  unsigned int

  5. uchar code FFW[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};   //正轉(zhuǎn)相序
  6. uchar code REV[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1};   //反轉(zhuǎn)相序

  7. sbit  K1   = P3^2;       //運(yùn)行與停止
  8. sbit  K2   = P3^3;       //速率調(diào)整
  9. sbit  K3   = P3^4;       //方向轉(zhuǎn)換
  10.   
  11. sbit  BEEP = P3^6;       //蜂鳴器

  12. sbit  RS = P2^0;     //液晶管腳定義      
  13. sbit  RW = P2^1;
  14. sbit  E = P2^2;
  15. sbit  BF=P0^7;    //忙碌標(biāo)志位

  16. bit  on_off=0;            //運(yùn)行與停止標(biāo)志
  17. bit  direction=1;         //方向標(biāo)志


  18. uchar   rate=2;               //預(yù)設(shè)定速率
  19. uchar   data_temp;  

  20. uchar u=0,v=0;



  21. uchar code  cdis1[ ] = {" STEPPING MOTOR "};
  22. uchar code  cdis2[ ] = {"CONTROL  PROCESS"};
  23. uchar code  cdis3[ ] = {"    STOP  "};
  24. uchar code  cdis4[ ] = {"    RATE:       "};
  25. uchar code  cdis5[ ] = {"   RUNNING "};

  26. /********************************************************/
  27. /*                                                
  28. /* 延時(shí)t毫秒
  29. /* 11.0592MHz時(shí)鐘,延時(shí)約1ms                                    
  30. /*                                                     
  31. /********************************************************/
  32. void delay(uint t)
  33. {                          
  34.    uchar k;
  35.    while(t--)
  36.    {
  37.      for(k=0; k<125; k++)
  38.      { }
  39.    }
  40. }

  41. /********************************************************/
  42. void delayB(uchar x)    //x*0.14MS
  43. {
  44.    uchar i;
  45.    while(x--)
  46.    {
  47.      for (i=0; i<13; i++)
  48.      { }
  49.    }
  50. }

  51. /********************************************************/
  52. void beep()
  53. {
  54.    uchar j;
  55.    for (j=0;j<100;j++)
  56.     {
  57.      delayB(4);
  58.      BEEP=!BEEP;                 //BEEP取反
  59.     }
  60.      BEEP=1;                    //關(guān)閉蜂鳴器
  61.   delay(170);
  62. }

  63. /********************************************************/
  64. /*                                                               
  65. /*檢查LCD忙狀態(tài)                                                  
  66. /*BF為1時(shí),忙,等待。為0時(shí),閑,可寫指令與數(shù)據(jù)。   
  67. /*                                                              
  68. /********************************************************/

  69. bit BusyTest()
  70. {                        
  71.     bit result;
  72.     RS = 0;
  73.     RW = 1;
  74.     E= 1;
  75.   _nop_();
  76.   _nop_();
  77.     result = BF;
  78.     E = 0;
  79.     return(result);
  80. }

  81. /*****************************************************
  82. 函數(shù)功能:將模式設(shè)置指令或顯示地址寫入液晶模塊
  83. 入口參數(shù):dictate
  84. ***************************************************/
  85. void WriteInstruction (unsigned char dictate)
  86. {  
  87.     while(BusyTest()==1); //如果忙就等待
  88.   RS=0;                  //根據(jù)規(guī)定,RS和R/W同時(shí)為低電平時(shí),可以寫入指令
  89.   RW=0;  
  90.   E=0;                   //E置低電平(寫指令時(shí),
  91.                              // 就是讓E從0到1發(fā)生正跳變,所以應(yīng)先置"0"
  92.   _nop_();
  93.   _nop_();             //空操作兩個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  94.   P0=dictate;            //將數(shù)據(jù)送入P0口,即寫入指令或地址
  95.   _nop_();
  96.   _nop_();
  97.   _nop_();
  98.   _nop_();               //空操作四個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  99.   E=1;                   //E置高電平
  100.   _nop_();
  101.   _nop_();
  102.   _nop_();
  103.   _nop_();               //空操作四個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  104.    E=0;                  //當(dāng)E由高電平跳變成低電平時(shí),液晶模塊開始執(zhí)行命令
  105. }

  106. /*****************************************************
  107. 函數(shù)功能:指定字符顯示的實(shí)際地址
  108. 入口參數(shù):x
  109. ***************************************************/
  110. void WriteAddress(unsigned char x)
  111. {
  112.      WriteInstruction(x|0x80); //顯示位置的確定方法規(guī)定為"80H+地址碼x"
  113. }

  114. /*****************************************************
  115. 函數(shù)功能:將數(shù)據(jù)(字符的標(biāo)準(zhǔn)ASCII碼)寫入液晶模塊
  116. 入口參數(shù):y(為字符常量)
  117. ***************************************************/
  118. void WriteData(unsigned char y)
  119. {
  120.     while(BusyTest()==1);
  121.    RS=1;           //RS為高電平,RW為低電平時(shí),可以寫入數(shù)據(jù)
  122.    RW=0;
  123.    E=0;            //E置低電平(寫指令時(shí),,
  124.                        // 就是讓E從0到1發(fā)生正跳變,所以應(yīng)先置"0"
  125.    P0=y;           //將數(shù)據(jù)送入P0口,即將數(shù)據(jù)寫入液晶模塊
  126.    _nop_();
  127.    _nop_();
  128.     _nop_();
  129.     _nop_();       //空操作四個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  130.    E=1;          //E置高電平
  131.    _nop_();
  132.    _nop_();
  133.    _nop_();
  134.    _nop_();        //空操作四個(gè)機(jī)器周期,給硬件反應(yīng)時(shí)間
  135.    E=0;            //當(dāng)E由高電平跳變成低電平時(shí),液晶模塊開始執(zhí)行命令
  136. }

  137. /*****************************************************
  138. 函數(shù)功能:對(duì)LCD的顯示模式進(jìn)行初始化設(shè)置
  139. ***************************************************/
  140. void LcdInt(void)
  141. {
  142.     delay(15);             //延時(shí)15ms,首次寫指令時(shí)應(yīng)給LCD一段較長的反應(yīng)時(shí)間
  143.     WriteInstruction (0x38);  //顯示模式設(shè)置:16×2顯示,5×7點(diǎn)陣,8位數(shù)據(jù)接口
  144. delay(5);   //延時(shí)5ms 
  145. WriteInstruction (0x38);
  146. delay(5);
  147. WriteInstruction (0x38); //3次寫 設(shè)置模式
  148. delay(5);
  149. WriteInstruction (0x0c);  //顯示模式設(shè)置:顯示開,無
  150. delay(5);
  151. WriteInstruction (0x06);  //顯示模式設(shè)置:光標(biāo)右移,字符不移
  152. delay(5);            
  153. WriteInstruction (0x01);  //清屏幕指令,將以前的顯示內(nèi)容清除
  154. delay(5);
  155. }
  156. /********************************************************/
  157. /*                                                      
  158. /* LCD1602初始顯示子程序                                            
  159. /*                                                     
  160. /********************************************************/
  161. void LcdInt_Dis()
  162. {
  163. uchar i=0;
  164. LcdInt();
  165. delay(15);
  166. WriteInstruction(0x01);
  167. delay(5);
  168. WriteAddress(0x00);
  169. delay(5);
  170. while(cdis1[i]!='\0'){
  171.   WriteData(cdis1[i]);
  172.   delay(5);
  173.   i++;
  174. }
  175. WriteAddress(0x40);
  176. delay(5);
  177. i=0;
  178.     while(cdis2[i]!='\0'){
  179.   WriteData(cdis2[i]);
  180.   delay(5);
  181.   i++;
  182. }

  183. delay(3000);
  184. WriteInstruction(0x01);   //清屏
  185. delay(5);
  186. i=0;
  187. WriteAddress(0x00);
  188. delay(5);
  189. while(cdis3[i]!='\0'){
  190.   WriteData(cdis3[i]);
  191.   delay(5);
  192.   i++;
  193. }
  194. WriteAddress(0x40);
  195. delay(5);
  196. i=0;
  197.     while(cdis4[i]!='\0'){
  198.   WriteData(cdis4[i]);
  199.   delay(5);
  200.   i++;
  201. }
  202. WriteAddress(0x0c);
  203. delay(5);
  204. for(i=0;i<2;i++)    //顯示方向符號(hào)
  205.        WriteData(0x3e);

  206. }



  207. /********************************************************/
  208. /*
  209. /*數(shù)據(jù)顯示子程序
  210. /*
  211. /********************************************************/
  212. void  data_dis()
  213. {   
  214.     data_temp = rate;         //顯示速率

  215.     WriteAddress(0x4c);
  216.     WriteData(data_temp+0x30);
  217.     delay(5);

  218. }

  219. /********************************************************
  220. /*
  221. /* 顯示運(yùn)行方向符號(hào)
  222. /*
  223. /********************************************************/
  224. void  dr_dis()
  225. {
  226.    uchar i=0;
  227. WriteAddress(0x0c);      //顯示方向符號(hào)
  228.     if(direction==1)           //正轉(zhuǎn)方向標(biāo)
  229. {
  230.   
  231.   for(i=0;i<2;i++){
  232.             WriteData(0x3e);
  233.    delay(5);
  234.   }
  235.    
  236.     }
  237.     else
  238.     {
  239.   for(i=0;i<2;i++){      //反轉(zhuǎn)方向標(biāo)志   
  240.            WriteData(0x3c);
  241.      delay(5);
  242.   }
  243.    
  244.     }
  245. }

  246. /********************************************************
  247. /*
  248. /* 顯示運(yùn)行狀態(tài)
  249. /*
  250. /********************************************************/
  251. void run_dis()
  252. {
  253. uchar i=0;
  254. if(on_off==1){
  255.   WriteAddress(0x00);
  256.   delay(5);
  257.   for(i=0;cdis5[i]!='\0';i++){
  258.    WriteData(cdis5[i]);
  259.    delay(5);
  260.   }
  261. }
  262. else{
  263.     WriteAddress(0x00);
  264.   delay(5);
  265.   for(i=0;cdis3[i]!='\0';i++){
  266.    WriteData(cdis3[i]);
  267.    delay(5);
  268.   }
  269. }
  270. }

  271. /********************************************************/
  272. /*
  273. /*步進(jìn)電機(jī)正轉(zhuǎn)
  274. /*
  275. /********************************************************/
  276. void  motor_ffw()
  277. {
  278.    unsigned char i;
  279.    unsigned char  j;

  280.    for (j=0; j<8; j++)        
  281.     {              
  282.       for (i=0; i<8; i++)       //一個(gè)周期轉(zhuǎn)45度
  283.         {
  284.           P1 = FFW[i];          //取數(shù)據(jù)
  285.           delay(rate);            //調(diào)節(jié)轉(zhuǎn)速
  286.         }
  287.     }

  288. }

  289. /********************************************************/
  290. /*
  291. /*步進(jìn)電機(jī)反轉(zhuǎn)
  292. /*
  293. /********************************************************/
  294. void  motor_rev()
  295. {
  296.      unsigned char i=0;
  297.   unsigned int  j=0;
  298.   for (j=0; j<8; j++)     
  299.       {

  300.         for (i=0; i<8; i++)     //一個(gè)周期轉(zhuǎn)45度
  301.         {
  302.           P1 = REV[i];          //取數(shù)據(jù)
  303.           delay(rate);            //調(diào)節(jié)轉(zhuǎn)速
  304.         }
  305.       }
  306. }  






  307. /********************************************************
  308. *                                                      
  309. *  主程序                                             
  310. *                                                     
  311. *********************************************************/

  312. void main()
  313. {
  314. LcdInt_Dis();  //液晶初始化
  315. TMOD=0X01;   //定時(shí)器T0工作方式1
  316. EA=1;      //開總中斷
  317. ET0=1;     //開T0中斷
  318. TH0=(65536-1000)/256;
  319. TL0=(65536-1000)%256; //定時(shí)1ms;
  320. TR0=1;     //啟動(dòng)T0
  321. P1=0X0F;    //關(guān)閉電機(jī)
  322. while(1){
  323. /**************************************/
  324.   if(K1==0){
  325.    delay(15);   //延時(shí)消陡
  326.    if(K1==0){
  327.     on_off=~on_off;   //取反
  328.     beep();
  329.     while(K1==0);  //等待按鍵釋放
  330.       }
  331.    
  332.      }
  333.    
  334. /**************************************/
  335.   if(K2==0){
  336.    delay(15);
  337.    if(K2==0){
  338.     rate++;
  339.     if(rate==0x0a)    //若rate=10,則回到2重新開始
  340.      rate=2;
  341.     beep();
  342.     while(K2==0);
  343.    }
  344.   }

  345. /**************************************/  
  346.   if(K3==0){
  347.    delay(15);
  348.    if(K3==0){
  349.     direction=~direction;
  350.     beep();
  351.     while(K3==0);
  352.    }
  353.   }

  354. /**************************************/
  355.      data_dis();
  356.         run_dis();
  357.   dr_dis();  
  358. };

  359. }



  360. /***************************************
  361. 定時(shí)器0中斷
  362. ***************************************/
  363. void t0()   interrupt 1
  364. {
  365.     TH0=(65536-1000)/256;
  366. TL0=(65536-1000)%256;
  367. v++;
  368. if(v<rate)    //每個(gè)脈沖間隔v*1ms
  369.   return;
  370. else{
  371.   v=0;
  372.   if(on_off==1){
  373.    if(u==8)
  374.     u=0;
  375.    if(direction==1) //正轉(zhuǎn)脈沖
  376.        P1=FFW[u];
  377.    else
  378.     P1=REV[u];   //反轉(zhuǎn)脈沖
  379.    u++;
  380.    }
  381. }
  382. }

復(fù)制代碼


相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

ID:74225 發(fā)表于 2015-3-11 20:04 | 顯示全部樓層
請(qǐng)問大神這段程序void delayB(uchar x)    //x*0.14MS{uchar i;   while(x--){for (i=0; i<13; i++)有什么作用呢,   還有那個(gè)TL0=0X33,TH0=0xf5和你的程序里的TH0=(65536-1000)/256;
TL0=(65536-1000)%256是一樣的嗎
回復(fù)

使用道具 舉報(bào)

ID:246189 發(fā)表于 2017-12-5 15:28 | 顯示全部樓層
有proteus電路圖嗎?
回復(fù)

使用道具 舉報(bào)

ID:246189 發(fā)表于 2017-12-5 15:33 | 顯示全部樓層
樓主有proteus電路圖嗎?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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