找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

單片機(jī)LCD1602顯示器循環(huán)顯示字符程序+仿真

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主


單片機(jī)源程序如下:
  1. //用LCD循環(huán)右移顯示" Welcome to Heifei Normal University "
  2. #include<reg51.h>    //包含單片機(jī)寄存器的頭文件
  3. #include<intrins.h>  //包含_nop_()函數(shù)定義的頭文件
  4. sbit RS=P2^0;    //寄存器選擇位,將RS位定義為P2.0引腳
  5. sbit RW=P2^1;    //讀寫選擇位,將RW位定義為P2.1引腳
  6. sbit E=P2^2;     //使能信號位,將E位定義為P2.2引腳
  7. sbit BF=P0^7;    //忙碌標(biāo)志位,,將BF位定義為P0.7引腳
  8. unsigned char code string[ ]={" hefei normal university "};
  9. unsigned char code string1[ ]={" zhangfei1610411024 "};
  10. /*****************************************************
  11. 函數(shù)功能:延時1ms
  12. (3j+2)*i=(3×33+2)×10=1010(微秒),可以認(rèn)為是1毫秒
  13. ***************************************************/
  14. void delay1ms()
  15. {
  16.    unsigned char i,j;        
  17.          for(i=0;i<10;i++)
  18.           for(j=0;j<33;j++)
  19.            ;                 
  20. }
  21. /*****************************************************
  22. 函數(shù)功能:延時若干毫秒
  23. 入口參數(shù):n
  24. ***************************************************/
  25. void delay(unsigned char n)
  26. {
  27.    unsigned char i;
  28.         for(i=0;i<n;i++)
  29.            delay1ms();
  30. }
  31. /*****************************************************
  32. 函數(shù)功能:判斷液晶模塊的忙碌狀態(tài)
  33. 返回值:result。result=1,忙碌;result=0,不忙
  34. ***************************************************/
  35. unsigned char BusyTest(void)
  36.   {
  37.     bit result;
  38.         RS=0;       //根據(jù)規(guī)定,RS為低電平,RW為高電平時,可以讀狀態(tài)
  39.     RW=1;
  40.     E=1;        //E=1,才允許讀寫
  41.     _nop_();   //空操作
  42.     _nop_();
  43.     _nop_();
  44.     _nop_();   //空操作四個機(jī)器周期,給硬件反應(yīng)時間        
  45.     result=BF;  //將忙碌標(biāo)志電平賦給result
  46.         E=0;
  47.     return result;
  48.   }
  49. /*****************************************************
  50. 函數(shù)功能:將模式設(shè)置指令或顯示地址寫入液晶模塊
  51. 入口參數(shù):dictate
  52. ***************************************************/
  53. void WriteInstruction (unsigned char dictate)
  54. {   
  55.     while(BusyTest()==1); //如果忙就等待
  56.          RS=0;                  //根據(jù)規(guī)定,RS和R/W同時為低電平時,可以寫入指令
  57.          RW=0;   
  58.          E=0;                   //E置低電平(根據(jù)表8-6,寫指令時,E為高脈沖,
  59.                              // 就是讓E從0到1發(fā)生正跳變,所以應(yīng)先置"0"
  60.          _nop_();
  61.          _nop_();             //空操作兩個機(jī)器周期,給硬件反應(yīng)時間
  62.          P0=dictate;            //將數(shù)據(jù)送入P0口,即寫入指令或地址
  63.          _nop_();
  64.          _nop_();
  65.          _nop_();
  66.          _nop_();               //空操作四個機(jī)器周期,給硬件反應(yīng)時間
  67.          E=1;                   //E置高電平
  68.          _nop_();
  69.          _nop_();
  70.          _nop_();
  71.          _nop_();               //空操作四個機(jī)器周期,給硬件反應(yīng)時間
  72.           E=0;                  //當(dāng)E由高電平跳變成低電平時,液晶模塊開始執(zhí)行命令
  73. }
  74. /*****************************************************
  75. 函數(shù)功能:指定字符顯示的實(shí)際地址
  76. 入口參數(shù):x
  77. ***************************************************/
  78. void WriteAddress(unsigned char x)
  79. {
  80.      WriteInstruction(x|0x80); //顯示位置的確定方法規(guī)定為"80H+地址碼x"
  81. }

  82. void WriteAddress1(unsigned char x)
  83. {
  84.      WriteInstruction(x|0xc0);
  85. }
  86. /*****************************************************
  87. 函數(shù)功能:將數(shù)據(jù)(字符的標(biāo)準(zhǔn)ASCII碼)寫入液晶模塊
  88. 入口參數(shù):y(為字符常量)
  89. ***************************************************/
  90. void WriteData(unsigned char y)
  91. {
  92.     while(BusyTest()==1);  
  93.           RS=1;           //RS為高電平,RW為低電平時,可以寫入數(shù)據(jù)
  94.           RW=0;
  95.           E=0;            //E置低電平(根據(jù)表8-6,寫指令時,E為高脈沖,
  96.                        // 就是讓E從0到1發(fā)生正跳變,所以應(yīng)先置"0"
  97.           P0=y;           //將數(shù)據(jù)送入P0口,即將數(shù)據(jù)寫入液晶模塊
  98.           _nop_();
  99.           _nop_();
  100.            _nop_();
  101.      _nop_();       //空操作四個機(jī)器周期,給硬件反應(yīng)時間
  102.           E=1;          //E置高電平
  103.           _nop_();
  104.           _nop_();
  105.           _nop_();
  106.          _nop_();        //空操作四個機(jī)器周期,給硬件反應(yīng)時間
  107.          E=0;            //當(dāng)E由高電平跳變成低電平時,液晶模塊開始執(zhí)行命令
  108. }
  109. /*****************************************************
  110. 函數(shù)功能:對LCD的顯示模式進(jìn)行初始化設(shè)置
  111. ***************************************************/
  112. void LcdInitiate(void)
  113. {
  114.    delay(15);             //延時15ms,首次寫指令時應(yīng)給LCD一段較長的反應(yīng)時間
  115.    WriteInstruction(0x38);  //顯示模式設(shè)置:16×2顯示,5×7點(diǎn)陣,8位數(shù)據(jù)接口
  116.         delay(5);   //延時5ms 
  117.         WriteInstruction(0x38);
  118.         delay(5);
  119.         WriteInstruction(0x38);
  120.         delay(5);
  121.         WriteInstruction(0x0f);  //顯示模式設(shè)置:顯示開,有光標(biāo),光標(biāo)閃爍
  122.         delay(5);
  123.         WriteInstruction(0x06);  //顯示模式設(shè)置:光標(biāo)右移,字符不移
  124.         delay(5);
  125.         WriteInstruction(0x01);  //清屏幕指令,將以前的顯示內(nèi)容清除
  126.         delay(5);
  127. }
  128. void main(void)            //主函數(shù)
  129. {
  130.   unsigned char i;
  131.   LcdInitiate();         //調(diào)用LCD初始化函數(shù)  
  132.   delay(10);
  133.   while(1)
  134.      {
  135.         WriteInstruction(0x01);//清顯示:清屏幕指令
  136.                                 WriteAddress(0x00);  // 設(shè)置顯示位置為第一行的第5個字
  137.                        i = 0;
  138.                 while(string[i] != '\0' )
  139.                         {                                                // 顯示字符
  140.                         
  141.                                         WriteData(string[i]);
  142.                                         i++;
  143.                                         delay(150);
  144.                                         /*if(i == 15)
  145.                                         {
  146.                                                 WriteInstruction(0x07);
  147.                                                 delay(5);
  148.                                         }*/
  149.                         }
  150.                         

  151.                         //WriteInstruction(0x06);
  152.                          delay(5);
  153.                         //WriteInstruction(0x07);
  154.                         WriteAddress1(0xc0);  // 設(shè)置顯示位置為第一行的第5個字
  155.                        i = 0;
  156.                 while(string1[i] != '\0')
  157.                         {                                                // 顯示字符
  158.                         
  159.                                         WriteData(string1[i]);
  160.                                         i++;
  161.                                         delay(150);
  162.                                         if(i == 10){
  163.                                        
  164.                                         WriteInstruction(0x07);
  165.                                         delay(5);
  166.                                         }
  167.                         }        WriteInstruction(0x06);
  168.         for(i=0;i<4;i++)
  169.                     delay(250);
  170.         
  171.                 }
  172.   

  173. }
復(fù)制代碼

所有資料51hei提供下載:
實(shí)驗(yàn)六.rar (96.71 KB, 下載次數(shù): 55)


評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

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

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

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