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

QQ登錄

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

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

步進(jìn)電機(jī)+1602顯示C源代碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:154660 發(fā)表于 2016-12-14 12:41 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
步進(jìn)電機(jī)+1602顯示C源代碼
下載:
步進(jìn)電機(jī) LCD1602顯示.doc (3.72 KB, 下載次數(shù): 17)


  1. /*此程序在SP-518USB開(kāi)發(fā)板上測(cè)試過(guò)    laosong  */

  2. /******************************步進(jìn)電機(jī)的驅(qū)動(dòng)*************************************
  3. 程序名稱:  按鍵控制電機(jī)正反轉(zhuǎn),LCD1602顯示正反轉(zhuǎn)
  4. 說(shuō)明:使用本程序你必須把 SE5設(shè)置為ON(2-3)短接
  5. ;FOSC = 12MHz
  6. ;---------------------------------------------------------------------------------
  7. ; 步進(jìn)電機(jī)的驅(qū)動(dòng)信號(hào)必須為脈沖信號(hào)!!! 轉(zhuǎn)動(dòng)的速度和脈沖的頻率成正比!!!
  8. ; 本步進(jìn)電機(jī)步進(jìn)角為 5.625度 . 一圈 360 度 , 需要64個(gè)脈沖完成!!!
  9. ;---------------------------------------------------------------------------------
  10. ; A組線圈對(duì)應(yīng) P1.4
  11. ; B組線圈對(duì)應(yīng) P1.5
  12. ; C組線圈對(duì)應(yīng) P1.6
  13. ; D組線圈對(duì)應(yīng) P1.7
  14. ; 正轉(zhuǎn)次序: AB組--BC組--CD組--DA組 (即一個(gè)脈沖,正轉(zhuǎn)5.625度)
  15. ;----------------------------------------------------------------------------------
  16. **********************************************************************************/



  17. #include <reg51.h>
  18. #include <intrins.h>
  19. #define uint unsigned int
  20. #define uchar unsigned char


  21. sbit lcd_rs_port = P2^7;   /*定義LCD控制端口*/
  22. sbit lcd_rw_port = P2^6;   /*定義LCD控制端口*/
  23. sbit lcd_en_port = P2^5;   /*定義LCD控制端口*/
  24. #define lcd_data_port P0   /*定義LCD控制端口*/
  25. #define motor P1  /*步進(jìn)電機(jī)接口*/
  26. sbit P34 = P3^4;  /*控制正轉(zhuǎn)按鍵*/
  27. sbit P33 = P3^3;  /*控制電機(jī)停止*/
  28. sbit P11 = P1^1;  /*控制電機(jī)反轉(zhuǎn)*/



  29. uchar code motor_table[][4]={{0x0f,0x0f,0x0f,0x0f},{0x3f,0x6f,0xcf,0x9f},{0x3f,0x9f,0xcf,0x6f}};  /*正反轉(zhuǎn)表*/
  30. uchar code lcd_table1[]  =   {"Motor Direction "};
  31. uchar code lcd_table2[][16]={{"      stop      "},
  32.                                                          {"    >>>>>>>>    "},
  33.                              {"    <<<<<<<<    "}};

  34. void lcd_delay(uchar ms) /*LCD1602 延時(shí)*/
  35. {
  36.     uchar j;
  37.     while(ms--){
  38.         for(j=0;j<250;j++)
  39.             {;}
  40.         }   
  41. }

  42. void lcd_busy_wait() /*LCD1602 忙等待*/
  43. {
  44.     lcd_rs_port = 0;
  45.     lcd_rw_port = 1;
  46.     lcd_en_port = 1;
  47.     lcd_data_port = 0xff;
  48.     while (lcd_data_port&0x80);
  49.     lcd_en_port = 0;

  50. }

  51. void lcd_command_write(uchar command) /*LCD1602 命令字寫(xiě)入*/
  52. {
  53.     lcd_busy_wait();
  54.     lcd_rs_port = 0;
  55.     lcd_rw_port = 0;
  56.     lcd_en_port = 0;
  57.     lcd_data_port = command;
  58.     lcd_en_port = 1;
  59.     lcd_en_port = 0;     
  60. }

  61. void lcd_system_reset() /*LCD1602 初始化*/
  62. {
  63.     lcd_delay(20);
  64.     lcd_command_write(0x38);
  65.     lcd_delay(100);
  66.     lcd_command_write(0x38);
  67.     lcd_delay(50);
  68.     lcd_command_write(0x38);
  69.     lcd_delay(10);
  70.     lcd_command_write(0x08);
  71.     lcd_command_write(0x01);
  72.     lcd_command_write(0x06);
  73.     lcd_command_write(0x0c);
  74. }

  75. void lcd_char_write(uchar x_pos,y_pos,lcd_dat) /*LCD1602 字符寫(xiě)入*/
  76. {
  77.     x_pos &= 0x0f; /* X位置范圍 0~15 */
  78.     y_pos &= 0x01; /* Y位置范圍 0~ 1 */
  79.     if(y_pos==1) x_pos += 0x40;
  80.     x_pos += 0x80;
  81.     lcd_command_write(x_pos);
  82.     lcd_busy_wait();
  83.     lcd_rs_port = 1;
  84.     lcd_rw_port = 0;
  85.     lcd_en_port = 0;
  86.     lcd_data_port = lcd_dat;
  87.     lcd_en_port = 1;
  88.     lcd_en_port = 0;
  89. }

  90. /*1MS為單位的延時(shí)程序*/
  91. void delay_1ms(uchar x)
  92. {
  93.     uchar j;
  94.     while(x--) { for(j=0;j<125;j++); }   
  95. }


  96. void main()
  97. {
  98.         uchar i;
  99.         uchar yunzhuang=0; /*運(yùn)轉(zhuǎn)狀況 0停止 1正轉(zhuǎn) 2反轉(zhuǎn)*/
  100.         uchar count=0; /*電機(jī)轉(zhuǎn)動(dòng)步數(shù)*/
  101.         motor = 0x0f; /*電機(jī)停止*/
  102.         lcd_system_reset(); /*LCD1602 初始化*/
  103.         for(i=0;i<16;i++) lcd_char_write(i,0,lcd_table1[i]);
  104.         while(1){
  105.                 motor = motor_table[yunzhuang][count]; /*電機(jī)轉(zhuǎn)動(dòng)*/
  106.                 delay_1ms(5);
  107.                 count++; /*電機(jī)步數(shù)加1*/
  108.                 if(count>=4) count = 0; /*完成一圈轉(zhuǎn)動(dòng)*/
  109.                 for(i=0;i<16;i++) lcd_char_write(i,1,lcd_table2[yunzhuang][i]);
  110.                 if(P33==0){
  111.                         delay_1ms(1);
  112.                         if(P33==0){
  113.                                 yunzhuang = 0;
  114.                                 }
  115.                         }
  116.                 else if(P34==0){
  117.                         delay_1ms(5);
  118.                         if(P34==0){
  119.                                 yunzhuang = 1;
  120.                                 count = 0;
  121.                                 }
  122.                         }
  123.                 else if(P11==0){
  124.                         delay_1ms(1);
  125.                         if(P11==0){
  126.                                 yunzhuang = 2;

  127.                                 }
  128.                         }
  129.                 }
  130. }


復(fù)制代碼


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

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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