找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

LCD12864驅(qū)動文件(帶字庫+可反白)程序分享

[復制鏈接]
ID:111517 發(fā)表于 2018-12-17 21:39 | 顯示全部樓層 |閱讀模式
壓縮包里面12864.H包含相應的函數(shù)聲明,可以根據(jù)自己情況來選擇是否反白,是否采用串口操作12864.C是函數(shù)實現(xiàn)
PORT.H包含12864里面的IO口聲明
SYS.H和sys.c里面包括12864里面所需的延時函數(shù),針對不同的平臺可自己做優(yōu)化。

單片機源程序如下:
  1. #include<LCD12864.H>
  2. #if TRANSMISSION_MODE
  3. //以下為串行傳輸方式
  4. void LCD12864_PortInit()
  5. {

  6. }
  7. void Lcd12864_SendByte(u8 byte)
  8. {
  9.     u8 i;
  10.     for(i=0;i<8;i++)
  11.     {
  12.         if((byte&0x80) == 0x80)
  13.                 LCD_SDA_SET();
  14.         else
  15.                 LCD_SDA_CLR();
  16.         LCD_SCL_SET();
  17.         byte <<=1;
  18.         LCD_SCL_CLR();
  19.     }

  20. }

  21. void Lcd12864_Write_Com(unsigned char dataa)
  22. {
  23.         u8 Hdata,Ldata;
  24.         Hdata=dataa&0xf0;
  25.         Ldata=(dataa<<4)&0xf0;
  26.         Lcd12864_SendByte(0xf8);
  27.         Lcd12864_SendByte(Hdata);
  28.         Lcd12864_SendByte(Ldata);
  29. }
  30. void Lcd12864_Write_Data(unsigned char dataa)
  31. {
  32.         u8 Hdata,Ldata;
  33.         Hdata=dataa&0xf0;
  34.         Ldata=(dataa<<4)&0xf0;
  35.         Lcd12864_SendByte(0xfa);
  36.         Lcd12864_SendByte(Hdata);
  37.         Lcd12864_SendByte(Ldata);
  38. }
  39. void Lcd12864_Init(void)
  40. {
  41.         LCD12864_PortInit();
  42.         Lcd12864_Write_Com(0x30);
  43.         delay_ms(2);
  44.         Lcd12864_Write_Com(0x0c);
  45.         delay_ms(2);
  46.         Lcd12864_Write_Com(0x01);
  47.         delay_ms(2);
  48.         delay_ms(2);
  49.         delay_ms(2);
  50.         delay_ms(2);
  51.         delay_ms(2);
  52. }

  53. void Lcd12864_Set_Address(u8 x,u8 y)
  54. {
  55.         switch(x)
  56.         {
  57.                 case 0:Lcd12864_Write_Com(0x80+y);break;
  58.                 case 1:Lcd12864_Write_Com(0x90+y);break;
  59.                 case 2:Lcd12864_Write_Com(0x88+y);break;
  60.                 case 3:Lcd12864_Write_Com(0x98+y);break;               
  61.         }
  62. }

  63. void Lcd12864_Write_Sring(char *p)
  64. {
  65.         while(*p!='\0')
  66.         {
  67.                 Lcd12864_Write_Data(*p);
  68.                 p++;
  69.         }
  70. }


  71. #if SET_WHITE
  72. /******************************************************                        
  73. * 功能:反白初始化                                     *
  74. * 輸入:無                                            *
  75. * 返回:無                                            *
  76. ******************************************************/
  77. void  Lcd12864_SetWhite_Init(void)
  78. {
  79.     u8 i,j;
  80.     Lcd12864_Write_Com(0x36);   //寫GDRAM是擴展操作指令
  81.     for(i=0;i<32;i++)  //對GDRAM進行清屏操作
  82.     {
  83.         Lcd12864_Write_Com(0x80+i);  //先寫入水平做坐標值
  84.         Lcd12864_Write_Com(0x80);  //寫入垂直坐標值
  85.         for(j=0;j<16;j++)            //寫入兩個8位元的數(shù)據(jù)
  86.         {
  87.             Lcd12864_Write_Data(0x00);
  88.             Lcd12864_Write_Data(0x00);
  89.         }
  90.     }   
  91.     Lcd12864_Write_Com(0x36);  
  92.     Lcd12864_Write_Com(0x32);  //開GPRAM顯示、關(guān)基本指令
  93. }
  94. /******************************************************                        
  95. * 功能:設置一行16*16字符反白                          *               
  96. * 輸入:   --> y     反白行                                                          *                                       
  97. *                  --> x    開始反白列                          *
  98. *                  --> endx 結(jié)束反白列                                                  *
  99. * 返回:無                                            *
  100. ******************************************************/
  101. void Lcd12864_Set1616White(u8  y,u8 x,u8 endx)
  102. {
  103.         u8 i,j,white_x,white_y,white_endx;
  104.         white_endx = endx-x+1;
  105.         switch(y)
  106.         {
  107.                 case 0:
  108.                         white_x = 0x80+x;
  109.                         white_y = 0x80;
  110.                         break;
  111.                 case 1:
  112.                         white_x = 0x80+x;
  113.                         white_y = 0x90;
  114.                         break;
  115.                 case 2:
  116.                         white_x = 0x88+x;
  117.                         white_y = 0x80;
  118.                         break;
  119.                 case 3:
  120.                         white_x = 0x88+x;
  121.                         white_y = 0x90;
  122.                         break;
  123.         }
  124.         Lcd12864_Write_Com(0x36);  //GDRAM--ON EXTEND--ON
  125.         for(i=0;i<16;i++)
  126.         {
  127.                 Lcd12864_Write_Com(white_y+i);
  128.                 Lcd12864_Write_Com(white_x);
  129.                 for(j=0;j<white_endx;j++)
  130.                 {
  131.                         Lcd12864_Write_Data(0xff);
  132.                         Lcd12864_Write_Data(0xff);
  133.                 }
  134.         }
  135.         Lcd12864_Write_Com(0x36);
  136.         Lcd12864_Write_Com(0x32);
  137. }
  138. /******************************************************                        
  139. * 功能:設置一行16*08字符反白                           *               
  140. * 輸入:   --> y     反白行                                                          *                                       
  141. *                  --> x    開始反白列                          *
  142. *                  --> endx 結(jié)束反白列                                                  *
  143. * 返回:無                                            *
  144. ******************************************************/
  145. void Lcd12864_Set1608White(u8 y,u8 x,u8 endx)
  146. {
  147.         u8 i,j,white_x,white_y,white_endx;
  148.         x=x/2;
  149.         endx=endx/2;
  150.         white_endx = endx-x+1;
  151.         switch(y)
  152.         {
  153.                 case 0:
  154.                         white_x = 0x80+x;
  155.                         white_y = 0x80;
  156.                         break;
  157.                 case 1:
  158.                         white_x = 0x80+x;
  159.                         white_y = 0x90;
  160.                         break;
  161.                 case 2:
  162.                         white_x = 0x88+x;
  163.                         white_y = 0x80;
  164.                         break;
  165.                 case 3:
  166.                         white_x = 0x88+x;
  167.                         white_y = 0x90;
  168.                         break;
  169.         }
  170.         Lcd12864_Write_Com(0x36);  //開繪圖顯示
  171.         for(i=0;i<16;i++)
  172.         {
  173.               Lcd12864_Write_Com(white_y+i);
  174.               Lcd12864_Write_Com(white_x);
  175.               for(j=0;j<white_endx;j++)
  176.               {
  177.                       if(x%2)  //如果開始被2整除,就說明他開始是從0開始,可以寫入0xff,否則寫0x00
  178.                               Lcd12864_Write_Data(0x00);
  179.                       else
  180.                               Lcd12864_Write_Data(0xff);
  181.                       if((endx+1)%2)  //如果結(jié)尾被2整除,就說明他正好是一個字,就可以寫入0xff ,否則寫0x00
  182.                               Lcd12864_Write_Data(0x00);
  183.                       else
  184.                               Lcd12864_Write_Data(0xff);
  185.               }
  186.         }
  187.         Lcd12864_Write_Com(0x36);
  188.         Lcd12864_Write_Com(0x32);
  189. }
  190. /******************************************************                        
  191. * 功能:設置一個ASCII字符反白                           *               
  192. * 輸入:   --> y   反白行                                                               *                                       
  193. *                 --> x    反白列                              *
  194. * 返回:無                                            *
  195. ******************************************************/
  196. void Lcd12864_SetAscWhite(u8 y,u8 x)
  197. {
  198.         u8 i,white_x,white_y,white_endx;
  199.         white_endx=x;
  200.         x=x/2;
  201.         switch(y)
  202.         {
  203.                 case 0:
  204.                         white_x = 0x80+x;
  205.                         white_y = 0x80;
  206.                         break;
  207.                 case 1:
  208.                         white_x = 0x80+x;
  209.                         white_y = 0x90;
  210.                         break;
  211.                 case 2:
  212.                         white_x = 0x88+x;
  213.                         white_y = 0x80;
  214.                         break;
  215.                 case 3:
  216.                         white_x = 0x88+x;
  217.                         white_y = 0x90;
  218.                         break;
  219.         }
  220.         Lcd12864_Write_Com(0x36);  //開繪圖顯示
  221.         for(i=0;i<16;i++)
  222.         {
  223.                 Lcd12864_Write_Com(white_y+i);
  224.                 Lcd12864_Write_Com(white_x);
  225.                 if(white_endx%2)  //如果開始被2整除,就說明他開始是從0開始,可以寫入0xff,否則寫0x00
  226.                 {
  227.                       Lcd12864_Write_Data(0x00);                       
  228.                       Lcd12864_Write_Data(0xff);
  229.                 }
  230.                 else
  231.                 {
  232.                       Lcd12864_Write_Data(0xff);
  233.                       Lcd12864_Write_Data(0x00);
  234.                 }       
  235.         }
  236.         Lcd12864_Write_Com(0x36);  
  237.         Lcd12864_Write_Com(0x32);  //開GPRAM顯示、關(guān)基本指令
  238. }
  239. /******************************************************                        
  240. * 功能:清楚一行16*16字符反白                          *               
  241. * 輸入:   --> y     反白行                                                          *                                       
  242. *                  --> x    開始反白列                          *
  243. *                  --> endx 結(jié)束反白列                                                  *
  244. * 返回:無                                            *
  245. ******************************************************/
  246. void Lcd12864_Clr1616White(u8 y,u8 x,u8 endx)
  247. {
  248.         u8 i,j,white_x,white_y,white_endx;
  249.         white_endx = endx-x+1;
  250.         switch(y)
  251.         {
  252.                 case 0:
  253.                         white_x = 0x80+x;
  254.                         white_y = 0x80;
  255.                         break;
  256.                 case 1:
  257.                         white_x = 0x80+x;
  258.                         white_y = 0x90;
  259.                         break;
  260.                 case 2:
  261.                         white_x = 0x88+x;
  262.                         white_y = 0x80;
  263.                         break;
  264.                 case 3:
  265.                         white_x = 0x88+x;
  266.                         white_y = 0x90;
  267.                         break;
  268.         }
  269.         Lcd12864_Write_Com(0x36);  //GDRAM--ON EXTEND--ON
  270.         for(i=0;i<16;i++)
  271.         {
  272.                 Lcd12864_Write_Com(white_y+i);
  273.                 Lcd12864_Write_Com(white_x);
  274.                 for(j=0;j<white_endx;j++)
  275.                 {
  276.                         Lcd12864_Write_Data(0x00);
  277.                         Lcd12864_Write_Data(0x00);
  278.                 }
  279.         }
  280.         Lcd12864_Write_Com(0x36);
  281.         Lcd12864_Write_Com(0x32);
  282. }
  283. /******************************************************                        
  284. * 功能:清除一行16*08字符反白                           *               
  285. * 輸入:   --> y     反白行                                                          *                                       
  286. *                  --> x    開始反白列                          *
  287. *                  --> endx 結(jié)束反白列                                                  *
  288. * 返回:無                                            *
  289. ******************************************************/
  290. void Lcd12864_Clr1608White(u8 y,u8 x,u8 endx)
  291. {
  292.         u8 i,j,white_x,white_y,white_endx;
  293.         x=x/2;
  294.         endx=endx/2;
  295.         white_endx = endx-x+1;
  296.         switch(y)
  297.         {
  298.                 case 0:
  299.                         white_x = 0x80+x;
  300.                         white_y = 0x80;
  301.                         break;
  302.                 case 1:
  303.                         white_x = 0x80+x;
  304.                         white_y = 0x90;
  305.                         break;
  306.                 case 2:
  307.                         white_x = 0x88+x;
  308.                         white_y = 0x80;
  309.                         break;
  310.                 case 3:
  311.                         white_x = 0x88+x;
  312.                         white_y = 0x90;
  313.                         break;
  314.         }
  315.         Lcd12864_Write_Com(0x36);  //開繪圖顯示
  316.         for(i=0;i<16;i++)
  317.         {
  318.               Lcd12864_Write_Com(white_y+i);
  319.               Lcd12864_Write_Com(white_x);
  320.               for(j=0;j<white_endx;j++)
  321.               {
  322.                       if(x%2)  //如果開始被2整除,就說明他開始是從0開始,可以寫入0xff,否則寫0x00
  323.                               Lcd12864_Write_Data(0xff);
  324.                       else
  325.                               Lcd12864_Write_Data(0x00);
  326.                       if((endx+1)%2)  //如果結(jié)尾被2整除,就說明他正好是一個字,就可以寫入0xff ,否則寫0x00
  327.                               Lcd12864_Write_Data(0xff);
  328.                       else
  329.                               Lcd12864_Write_Data(0x00);
  330.               }
  331.         }
  332.         Lcd12864_Write_Com(0x36);  
  333.         Lcd12864_Write_Com(0x32);  //開GPRAM顯示、關(guān)基本指令
  334. }
  335. /******************************************************                        
  336. * 功能:清除一行ASCII字符反白                           *               
  337. * 輸入:   --> y     反白行                                                          *                                       
  338. *                  --> x    開始反白列                          *
  339. *                  --> endx 結(jié)束反白列                                                  *

  340. ……………………

  341. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼

所有資料51hei提供下載:
驅(qū)動程序.zip (4.72 KB, 下載次數(shù): 137)


評分

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

查看全部評分

回復

使用道具 舉報

ID:136633 發(fā)表于 2019-8-26 17:26 | 顯示全部樓層
謝謝分享
回復

使用道具 舉報

ID:204105 發(fā)表于 2019-9-9 10:02 | 顯示全部樓層
樓主可否發(fā)個完整例程?
回復

使用道具 舉報

ID:443931 發(fā)表于 2021-2-1 16:12 | 顯示全部樓層
樓主,試了一下這個程序,發(fā)現(xiàn)16*16的反白效果正常,但是16*8的反白效果不對,如果我讓它從第三個數(shù)字到第五個數(shù)字反白,第四個就不能正常反白,怎么處理,有建議嗎,謝謝
回復

使用道具 舉報

ID:443931 發(fā)表于 2021-2-23 14:31 | 顯示全部樓層
lrf32535604 發(fā)表于 2021-2-1 16:12
樓主,試了一下這個程序,發(fā)現(xiàn)16*16的反白效果正常,但是16*8的反白效果不對,如果我讓它從第三個數(shù)字到第 ...

函數(shù)處理后正常了
回復

使用道具 舉報

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

本版積分規(guī)則

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

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

快速回復 返回頂部 返回列表