找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1772|回复: 0
打印 上一主题 下一主题
收起左侧

LC1602水平滚动显示诗歌的单片机代码

[复制链接]
跳转到指定楼层
楼主
ID:416108 发表于 2018-11-18 20:33 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
资料请下载附件

单片机源程序如下:
  1. #include<reg51.h>
  2. #include<string.h>
  3. #include<intrins.h>
  4. #define uchar unsigned char
  5. #define uint unsigned int
  6. void Initialize_LCD();
  7. void DelayMS(uint ms);
  8. void ShowString(uchar,uchar,uchar *);
  9. sbit RS=P2^0;
  10. sbit RW=P2^1;
  11. sbit EN=P2^2;
  12. sbit K1=P3^0;
  13. sbit K2=P3^1;
  14. sbit K3=P3^2;
  15. uchar code Prompt[]="Press K1 - K3 To Start Demo Prog";
  16. uchar const Line_Count=10;       
  17. uchar code Msg[][80]=
  18. {" <The furthest distance in the world> "
  19. " The furthest distance in the world ",
  20. " Is not between life and death ",
  21. " But when I stand in front of you ",
  22. " Yet you do not know that",
  23. " I love you ",
  24. " The furthest distance in the world ",
  25. " Is not when I stand in font of you ",
  26. " Yet you can not see my love ",
  27. " But when undoubtedly knowing the love from bot ",
  28. " Yet cannot ",
  29. " Be together "
  30. };
  31. uchar Disp_Buffer[32];   //显示缓冲(2行)

  32. void DelayMS(uint ms)    //延时
  33. {
  34.         uchar i;
  35.         while(ms--) for(i=0;i<120;i++);
  36. }
  37. uchar Busy_Check()   
  38. {
  39.         uchar LCD_Status;
  40.         RS=0;                               
  41.         RW=1;                               
  42.         EN=1;                               
  43.         DelayMS(1);
  44.         LCD_Status=P0;
  45.         EN=0;
  46.         return LCD_Status;
  47. }
  48. void Write_LCD_Command(uchar cmd)
  49. {
  50.         while((Busy_Check()&0x80)==0x80);       
  51.         RS=0;               
  52.         RW=0;               
  53.         EN=0;       
  54.         P0=cmd;EN=1;DelayMS(1);EN=0;
  55. }
  56. void Write_LCD_Data(uchar dat)
  57. {
  58.         while((Busy_Check()&0x80)==0x80);
  59.         RS=1;RW=0;EN=0;P0=dat;EN=1;DelayMS(1);EN=0;
  60. }
  61. void Initialize_LCD()
  62. {
  63.         Write_LCD_Command(0x38);DelayMS(1);
  64.         Write_LCD_Command(0x01);DelayMS(1);       
  65.         Write_LCD_Command(0x06);DelayMS(1);       
  66.         Write_LCD_Command(0x0c);DelayMS(1);       
  67. }
  68. void ShowString(uchar x,uchar y,uchar *str)
  69. {
  70.         uchar i=0;
  71.         if(y==0) Write_LCD_Command(0x80|x);       
  72.         if(y==1) Write_LCD_Command(0xc0|x);
  73.         for(i=0;i<16;i++)                                       
  74.         {
  75.                 Write_LCD_Data(str[i]);
  76.         }
  77. }

  78. void H_Scroll_Display()
  79. {
  80.         uchar i,j,k=0,L=0;
  81.         uchar *p=Msg[0];
  82.         uchar *q=Msg[Line_Count]+strlen(Msg[Line_Count]);
  83.         for(i=0;i<16;i++) Disp_Buffer[i]=' ';
  84.         while(p<q)
  85.         {
  86.                 if((i==16||i==31)&&*p==' ') p++;
  87.                 for(i=16;i<32&&p<q;i++)
  88.                 {       
  89.                         if(*p!='\0')
  90.                         {
  91.                                 Disp_Buffer[i]=*p++;
  92.                         }
  93.                         else
  94.                         {
  95.                                 if(++k>Line_Count) break;
  96.                                 p=Msg[k];                               
  97.                                 Disp_Buffer[i]=*p++;
  98.                         }
  99.                 }
  100.                 for(j=i;j<32;j++) Disp_Buffer[j]=' ';
  101.                 for(i=0;i<=16;i++)
  102.                 {
  103.                         while(F0) DelayMS(5);
  104.                         ShowString(0,L,Disp_Buffer+i);
  105.                         while(F0) DelayMS(5);
  106.                         DelayMS(30);
  107.                 }
  108.                 L=(L==0)?1:0;
  109.                 DelayMS(300);
  110.         }
  111.         if(L==1) ShowString(0,1,"                 ");       
  112. }
  113. void EX_INT0() interrupt 0
  114. {
  115.         F0=!F0;               
  116. }
  117. void main()
  118. {
  119.         uint Count=0;
  120.         IE=0x81;               
  121.         IT0=1;               
  122.         F0=0;       
  123.         Initialize_LCD();
  124.         ShowString(0,0,Prompt);
  125.         ShowString(0,1,Prompt+16);
  126.         while(1)
  127.         {
  128.                 if(K2==0)
  129.                 {       
  130.                         H_Scroll_Display();
  131.                         DelayMS(300);       
  132.                 }
  133.         }
  134. }
复制代码

所有资料51hei提供下载:
水平滚动.rar (25.88 KB, 下载次数: 15)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表