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

QQ登錄

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

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

12864顯示漢字c51程序 單片機(jī):STC12C5A60S2

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:79544 發(fā)表于 2015-7-25 21:25 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
  1. /**************************************************
  2.         功能:12864顯示漢字
  3.         單片機(jī):STC12C5A60S2
  4.         晶振:11.0592M
  5.         作者:蘇義江
  6.         時(shí)間:2015-7-25修改
  7.                         并行輸出
  8. *****************************************************/
  9. #include<reg52.h>
  10. #define uchar unsigned char
  11. #define uint unsigned int


  12. sbit E=P3^7;//脈沖使能
  13. sbit RW=P3^6;//讀寫(xiě)選擇
  14. sbit RS=P3^5;//數(shù)據(jù)命令選擇
  15. sbit rst=P1^0;//12864復(fù)位
  16. uchar code table3[]="欲窮千里目";
  17. uchar code table4[]="更上一層樓";
  18. uchar code table1[]="白日依山盡";
  19. uchar code table2[]="黃河入海流";
  20. // 延時(shí)ms函數(shù):
  21. void Delayms(uint t)//STC12C5A60S2  1ms
  22. {
  23.     uint i,j;
  24.     for(i=t;i>0;i--)
  25.              for(j=0;j<840;j++);
  26. }

  27. // 12864寫(xiě)命令函數(shù):
  28. void Write12864Command(uchar com)
  29. {
  30.                 Delayms(10);
  31.        P0=com;//賦值
  32.        E=0;//寫(xiě)命令前三控制線的狀態(tài)
  33.                 Delayms(10);
  34.        RS=0;
  35.        RW=0;
  36.        E=1;//拉高,寫(xiě)命令
  37.                 Delayms(10);//延時(shí)必須加
  38.        E=0;//寫(xiě)命令后三控制線的狀態(tài)
  39.                 Delayms(10);//延時(shí)必須加
  40.        RS=1;
  41.        RW=1;
  42. }
  43. //12864寫(xiě)數(shù)據(jù)函數(shù):
  44. void Write12864Data( uchar dat)
  45. {
  46.            Delayms(10);
  47.        P0=dat;//賦值
  48.        E=0;//寫(xiě)數(shù)據(jù)前三控制線的狀態(tài)
  49.                 Delayms(10);
  50.        RS=1;
  51.        RW=0;   
  52.        E=1;//拉高,寫(xiě)數(shù)據(jù)
  53.            Delayms(10);//延時(shí)必須加
  54.        E=0;//寫(xiě)數(shù)據(jù)后三控制線的狀態(tài)
  55.                 Delayms(10);//延時(shí)必須加
  56.        RS=0;
  57.        RW=1;
  58. }   
  59. //12864初始化函數(shù):
  60. void Initialize12864()
  61. {
  62.        rst=0;//復(fù)位12864
  63.        Delayms(30);
  64.        rst=1;
  65.        Delayms(20);
  66.        Write12864Command(0x30);//功能設(shè)定:8位控制方式,使用基本指令
  67.        Write12864Command(0x08);//顯示關(guān)
  68.        Write12864Command(0x01);//清屏
  69.        Write12864Command(0x06);//地址計(jì)數(shù)器加一、光標(biāo)右移
  70.        Write12864Command(0x0c);//顯示開(kāi)
  71. }
  72. //在指定行和列顯示漢字
  73. void LCD12864DisplayString( uchar y, uchar x, uchar *pstr)
  74.   //y-行數(shù)值0-3,x-列數(shù)值0-7,pstr-字符串指針
  75. //12864可以顯示32個(gè)漢字(四行每行8個(gè)),一個(gè)地址對(duì)應(yīng)一個(gè)漢字
  76. //可以顯示64個(gè)ASCII碼字符(四行每行16個(gè)),一個(gè)地址對(duì)應(yīng)兩個(gè)字符
  77.   //為了實(shí)現(xiàn)自動(dòng)換行功能,這個(gè)函數(shù)比較繁瑣
  78. {
  79.      uchar row,n=0;
  80.      Write12864Command(0x30);//基本指令
  81.      Write12864Command(0x06);//地址計(jì)數(shù)器自動(dòng)加以,光標(biāo)右移
  82.      switch(y)//根據(jù)行號(hào)選擇行地址
  83.      {
  84.                  case 0:row=0x80;break;//第一行首地址
  85.                  case 1:row=0x90;break;//第二行首地址
  86.                  case 2:row=0x88;break;//第三行首地址
  87.                  case 3:row=0x98;break;//第四行首地址
  88.                  default:;
  89.      }
  90.         Write12864Command(row+x);//寫(xiě)地址
  91.      while(*pstr!='\0')
  92.      {
  93.                 Write12864Data(*pstr);//寫(xiě)字符
  94.                 pstr++;
  95.                 n++;//計(jì)數(shù)
  96.                 if((n+x*2)==16)//如果一行寫(xiě)完,繼續(xù)寫(xiě)第二行
  97.                 {
  98.                        if(y==0) Write12864Command(0x90);//寫(xiě)下一行地址
  99.                        else if(y==1) Write12864Command(0x88);//寫(xiě)下一行地址
  100.                        else if(y==2) Write12864Command(0x98);//寫(xiě)下一行地址
  101.                        else ;
  102.                 }
  103.                 else if((n+x*2)==32)//如果第二行寫(xiě)完,繼續(xù)寫(xiě)第三行
  104.                 {
  105.                         if(y==0) Write12864Command(0x88);//寫(xiě)下一行地址
  106.                         else if(y==1) Write12864Command(0x98);//寫(xiě)下一行地址
  107.                         else ;
  108.                 }
  109.                 else if((n+x*2)==48)//如果第三行寫(xiě)完,繼續(xù)寫(xiě)第四行
  110.                 {
  111.                         if(y==0) Write12864Command(0x98);//寫(xiě)下一行地址
  112.                         else ;
  113.                 }
  114.                 else ;
  115.     }
  116. }   
  117. //清屏
  118. void Clear12864Screen()
  119. {
  120.         Write12864Command(0x01);//清屏
  121.         Write12864Data(0x00);//清屏
  122. }
  123. void main()
  124. {
  125.         uchar i;
  126.         Delayms(100);
  127.         Initialize12864();
  128.         Clear12864Screen();
  129.         while(1)
  130. {
  131.         Write12864Command(0x80);
  132.         for(i=0;i<11;i++)
  133.         {
  134.                 Write12864Data(table1[i]);
  135.                 Delayms(200);
  136.         }
  137. Clear12864Screen();
  138.         Write12864Command(0x90);
  139.         for(i=0;i<11;i++)
  140.         {
  141.                 Write12864Data(table2[i]);
  142.                 Delayms(200);
  143.         }
  144. Clear12864Screen();
  145.         Write12864Command(0x88);
  146.         for(i=0;i<12;i++)
  147.         {
  148.                 Write12864Data(table3[i]);
  149.                 Delayms(200);
  150.         }
  151. Clear12864Screen();
  152.         Write12864Command(0x98);
  153.         for(i=0;i<12;i++)
  154.         {
  155.                 Write12864Data(table4[i]);
  156.                 Delayms(200);
  157.         }
  158. Clear12864Screen();
  159.         LCD12864DisplayString(0, 0, "--蘇義江成心歡迎大家來(lái)到單片機(jī)世界。沒(méi)有想不到只有做不到!");
  160.         Delayms(4000);
  161. Clear12864Screen();
  162.         }
  163.                
  164. }
復(fù)制代碼


評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的積分獎(jiǎng)勵(lì)!

查看全部評(píng)分

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

使用道具 舉報(bào)

沙發(fā)
ID:193210 發(fā)表于 2017-9-19 11:28 | 只看該作者
不錯(cuò)不錯(cuò)不錯(cuò)
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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