標(biāo)題: 51單片機(jī)液晶模塊代碼(12864 1602 5110) [打印本頁]

作者: pdh    時(shí)間: 2016-1-11 11:28
標(biāo)題: 51單片機(jī)液晶模塊代碼(12864 1602 5110)
包含12864 1602 5110


完整代碼(包含頭文件)下載: 液晶.rar (4.73 KB, 下載次數(shù): 42)

以下是文件內(nèi)容預(yù)覽:
12864:

  1. #include"yejing.h"

  2. #define uint unsigned int
  3. #define uchar unsigned char

  4. //液晶
  5. sbit CS=P3^4;
  6. sbit SID=P3^5;
  7. sbit SCLK=P3^6;

  8. uchar yi[]={"歡迎使用"};
  9. uchar er[]={"紅外報(bào)警系統(tǒng)"};
  10. uchar san[]={"發(fā)現(xiàn)物體闖入"};
  11. uchar si[]={"已處于警戒中"};


  12. void delay_1ms(uint x)
  13. {
  14. uint i,j;
  15. for(j=0;j<x;j++)
  16.     for(i=0;i<100;i++);
  17. }
  18.        /*************************/
  19. /******************寫指令*****************/
  20. void send_command(uchar command_data)
  21. {
  22.         uchar i;
  23.         uchar i_data;
  24.         i_data=0xf8;//第一個(gè)字節(jié)確定寫指令
  25.         CS=1;
  26.         SCLK=0;
  27.         /*第一個(gè)字節(jié)設(shè)定為寫指令*/
  28.         for(i=0;i<8;i++)         
  29.         {
  30.                   SID=(bit)(i_data&0x80);
  31.                   SCLK=0;
  32.                   SCLK=1;
  33.                   i_data=i_data<<1;
  34.         }
  35.         /*寫第二個(gè)字節(jié)和第三個(gè)字節(jié)確定指令功能*/
  36.         /******* 寫第二個(gè)字節(jié)********/
  37.         i_data=command_data;
  38.         i_data&=0xf0;          //高4位保留,低四位清零
  39.         for(i=0;i<8;i++)
  40.         {
  41.                   SID=(bit)(i_data&0x80);
  42.                   SCLK=0;
  43.                   SCLK=1;
  44.                   i_data=i_data<<1;
  45.         }
  46.         /********寫第三個(gè)字節(jié)********/
  47.         i_data=command_data;
  48.         i_data<<=4;          //把低四位取代高四位
  49.         for(i=0;i<8;i++)
  50.         {
  51.                   SID=(bit)(i_data&0x80);
  52.                   SCLK=0;
  53.                   SCLK=1;
  54.                   i_data=i_data<<1;  
  55.         }

  56.         CS=0;
  57.         delay_1ms(10);
  58. }


  59.       /*************************/
  60. /******************寫數(shù)據(jù)*****************/
  61. void send_data(uchar command_data)
  62. {
  63.         uchar i;
  64.         uchar i_data;
  65.         i_data=0xfa;//第一個(gè)字節(jié)確定寫數(shù)據(jù)
  66.         CS=1;
  67.         /*第一個(gè)字節(jié)設(shè)定為寫數(shù)據(jù)*/
  68.         for(i=0;i<8;i++)
  69.         {
  70.                   SID=(bit)(i_data&0x80);
  71.                   SCLK=0;
  72.                   SCLK=1;
  73.                   i_data=i_data<<1;  
  74.         }
  75.         /******* 寫第二個(gè)字節(jié)********/
  76.         i_data=command_data;
  77.         i_data&=0xf0;
  78.         for(i=0;i<8;i++)
  79.         {
  80.                   SID=(bit)(i_data&0x80);
  81.                   SCLK=0;
  82.                   SCLK=1;
  83.                   i_data=i_data<<1;  
  84.         }
  85.         /******* 寫第三個(gè)字節(jié)********/
  86.         i_data=command_data;
  87.         i_data<<=4;
  88.           for(i=0;i<8;i++)
  89.         {
  90.                   SID=(bit)(i_data&0x80);
  91.                   SCLK=0;
  92.                   SCLK=1;
  93.                   i_data=i_data<<1;  
  94.         }

  95.         CS=0;
  96.         delay_1ms(10);
  97. }



  98. /********初始化液晶************/
  99. void lcd_init()
  100. {
  101.         delay_1ms(100);
  102.         send_command(0x30);
  103.         send_command(0x02);
  104.         send_command(0x06);
  105.         send_command(0x0c);
  106.         send_command(0x01);
  107.         send_command(0x80);
  108. }






  109. /*******顯示第一行*******/
  110. void display_1()
  111. {
  112.         uchar i;
  113.         send_command(0x82);//發(fā)送指令顯示第一行
  114.         for(i=0;i<8;i++)
  115.         {
  116.                   send_data(yi[i]);
  117.         }
  118. }


  119. /*******顯示測(cè)第二行*******/
  120. void display_2()
  121. {
  122.         uchar i;
  123.         send_command(0x91);//發(fā)送指令顯示第二行
  124.         for(i=0;i<12;i++)
  125.         {
  126.                   send_data(er[i]);
  127.         }
  128. }


  129. /*******顯示第三行*******/
  130. void display_3()
  131. {
  132.         uchar i;
  133.         send_command(0x89);//發(fā)送指令顯示第三行
  134.         for(i=0;i<12;i++)
  135.         {
  136.                   send_data(san[i]);
  137.         }
  138. }

  139. /*******顯示第三行*******/
  140. void display_4()
  141. {
  142.         uchar i;
  143.         send_command(0x89);//發(fā)送指令顯示第三行
  144.         for(i=0;i<12;i++)
  145.         {
  146.                   send_data(si[i]);
  147.         }
  148. }
復(fù)制代碼


5110:
  1. /******************************
  2. 文檔說明:NOKIA5110顯示模塊驅(qū)動(dòng)
  3. 修改日期:9月8日
  4. 版本:        v1

  5. ********************************/
  6. #include "LCD5110.h"
  7. #include  "suzu.h"



  8. /************************************************
  9. 函數(shù)名:    void LCD_init(void)
  10. 功能:      NKIA5110初始化配置
  11. 輸入?yún)?shù):  無
  12. 輸出參數(shù):  無
  13. 修改日期:  9月8日
  14. 特殊說明:  不同模塊所需調(diào)的是28行代碼,大概范圍150-255
  15. ************************************************/
  16. void LCD_init(void)
  17. {
  18.         unsigned char i;
  19.         SRST=0;
  20.         for(i=0;i<150;i++);
  21.     SRST=1;
  22.         LCD_write_byte(0x21,0);
  23.     LCD_write_byte(195,0); //偏置電壓設(shè)置

  24.         LCD_write_byte(0x20,0);
  25.         LCD_clear();
  26.         LCD_write_byte(0x0c,0);
  27.         SCE=0;
  28. }
  29. /************************************************
  30. 函數(shù)名:    void LCD_write_byte(unsigned char dat,unsigned char command)
  31. 功能:      向NKIA5110寫字節(jié)數(shù)據(jù)
  32. 輸入?yún)?shù):  unsigned char dat,unsigned char command
  33. 輸出參數(shù):  無
  34. 修改日期:  9月8日
  35. ************************************************/
  36. void LCD_write_byte(unsigned char dat,unsigned char command)
  37. {
  38.         unsigned char i;                                                                 
  39.         SCE=0;
  40.         if(command==0)
  41.         SDC=0;
  42.         else
  43.         SDC=1;
  44.         for(i=0;i<8;i++)
  45.         {
  46.                   if(dat&0x80)
  47.             SDIN=1;
  48.                    else
  49.             SDIN=0;
  50.             SCLK=0;
  51.             dat=dat<<1;
  52.             SCLK=1;
  53.         }
  54.         SCE=1;
  55. }

  56. /************************************************
  57. 函數(shù)名:    void LCD_set_XY(unsigned char X,unsigned char Y)
  58. 功能:      設(shè)置NKIA5110顯示XY坐標(biāo)位置
  59. 輸入?yún)?shù):  unsigned char X,unsigned char Y
  60. 輸出參數(shù):  無
  61. 修改日期:  9月8日
  62. ************************************************/
  63. void LCD_set_XY(unsigned char X,unsigned char Y)
  64. {
  65.         LCD_write_byte(0x40|Y,0);//列
  66.         LCD_write_byte(0x80|X,0);//行
  67. }
  68. /************************************************
  69. 函數(shù)名:     void LCD_clear(void)
  70. 功能:      NKIA5110 清屏
  71. 輸入?yún)?shù):  無
  72. 輸出參數(shù):  無
  73. 修改日期:  9月8日
  74. ************************************************/
  75. void LCD_clear(void)
  76. {
  77.         unsigned char t;
  78.         unsigned char k;
  79.         LCD_set_XY(0,0);
  80.           for(t=0;t<6;t++)
  81.         {
  82.                for(k=0;k<84;k++)
  83.              {
  84.                      LCD_write_byte(0x00,1);                                          
  85.              }
  86.         }
  87. }       
  88.                           
  89. /************************************************
  90. 函數(shù)名:    void LCD_write_char(unsigned char x,unsigned y,unsigned char c)
  91. 功能:      NKIA5110 顯示字符
  92. 輸入?yún)?shù):  unsigned char x,unsigned y,unsigned char c
  93. 輸出參數(shù):  無
  94. 修改日期:  9月8日
  95. ************************************************/
  96. void LCD_write_char(unsigned char x,unsigned y,unsigned char c)
  97. {         
  98.         unsigned char i;
  99.         c-=32;
  100.         LCD_set_XY(x,y);
  101.         for(i=0;i<6;i++)
  102.         {
  103.                 LCD_write_byte(font6x8[c][i],1);
  104.                 if(i==5)
  105.                 {
  106.                         y++;
  107.                         LCD_set_XY(x,y);
  108.                 }
  109.         }
  110. }
  111. /************************************************
  112. 函數(shù)名:    void LCD_write_Num(unsigned char X,unsigned char Y,unsigned int num)
  113. 功能:      NKIA5110 顯示數(shù)字
  114. 輸入?yún)?shù):  X為一個(gè)位置為6,Y為行取值為0~5,num為要顯示的數(shù)字
  115. 輸出參數(shù):  無
  116. 修改日期:  9月8日
  117. ************************************************/
  118. void LCD_write_Num(unsigned char X,unsigned char Y,unsigned int num)
  119. {
  120.           unsigned char str[8],i=0,len=0;
  121.           unsigned int temp;
  122.           temp = num;
  123.           while(temp)
  124.           {
  125.             temp /=10;
  126.             len++;
  127.           }
  128.         if(!num)
  129.           {
  130.             len++;
  131.             str[0]=0x30;
  132.           }
  133.           str[len] = 0;
  134.           while(num)
  135.           {
  136.             str[len-i-1] = num%10 + 0x30;
  137.             num /=10;
  138.             i++;
  139.           }
  140.           LCD_write_string(X,Y,(char *)str);
  141. }
  142. /************************************************
  143. 函數(shù)名:    void LCD_write_Num(unsigned char X,unsigned char Y,unsigned int num)
  144. 功能:      NKIA5110 顯示字符串
  145. 輸入?yún)?shù):  X為一個(gè)位置為6,Y為行取值為0~5,*s為要顯示的字符串
  146. 輸出參數(shù):  無
  147. 修改日期:  9月8日
  148. ************************************************/
  149. void LCD_write_string(unsigned char X,unsigned char Y,char *s)
  150. {
  151.         while(*s)
  152.         {
  153.                 LCD_write_char(X,Y,*s);
  154.                 s++;
  155.                 X+=6;
  156.         }
  157. }
  158. ///************************************************
  159. // 函數(shù)名:    void disp16_16(unsigned char x,unsigned char y,unsigned char address)  
  160. // 功能:      NKIA5110 顯示16*16的字符
  161. // 輸入?yún)?shù):  unsigned char x,unsigned y,unsigned char c
  162. // 輸出參數(shù):  無
  163. // 修改日期:  9月8日
  164. // ************************************************/
  165. //void disp16_16(unsigned char x,unsigned char y,unsigned char address)
  166. //{
  167. //         unsigned char c;
  168. //         LCD_set_XY(x*8, y);
  169. //         for(c=0;c<16;c++)
  170. //         {   
  171. //                  LCD_write_byte(han[address*32+c],1);
  172. //         }
  173. //         LCD_set_XY(x*8, y+1);
  174. //         for(c=16;c<32;c++)
  175. //         {   
  176. //                  LCD_write_byte(han[address*32+c],1);
  177. //         }
  178. //}
  179. // /************************************************
  180. // 函數(shù)名:    void LCD_write_chinese_string(unsigned char X, unsigned char Y,
  181. //             unsigned char ch_with,unsigned char num,
  182. //             unsigned char line,unsigned char row)         
  183. // 功能:      NKIA5110 顯示中文字符
  184. // 輸入?yún)?shù):  unsigned char X, unsigned char Y,
  185. //             unsigned char ch_with,unsigned char num,
  186. //             unsigned char line,unsigned char row
  187. // 輸出參數(shù):  無
  188. // 修改日期:  9月8日
  189. // ************************************************/
  190. //void LCD_write_chinese_string(unsigned char X, unsigned char Y, unsigned char ch_with,unsigned char num,unsigned char line,unsigned char row)                //x,y為坐標(biāo),ch_with為漢字寬度,num為數(shù)組中第幾個(gè)漢字,line為行,row為列
  191. //{
  192. //        unsigned char i,n;   
  193. //    LCD_set_XY(X,Y);                             //   
  194. //    for (i=0;i<num;)
  195. //    {
  196. //            for (n=0; n<ch_with*2; n++)              //
  197. //              {
  198. //                      if (n==ch_with)                      //
  199. //                  {
  200. //                      if (i==0) LCD_set_XY(X,Y+1);
  201. //                      else
  202. //                      LCD_set_XY((X+(ch_with+row)*i),Y+1);
  203. //            }
  204. //                  LCD_write_byte(write_chinese[line+i][n],1);
  205. //              }
  206. //              i++;
  207. //              LCD_set_XY((X+(ch_with+row)*i),Y);
  208. //    }
  209. //}

  210.                
  211.                
  212.                
  213.                                  
復(fù)制代碼


1602:

  1. #include"yejing.h"
  2. /*液晶*/
  3. #define RS_CLR RS=0
  4. #define RS_SET RS=1
  5. #define RW_CLR RW=0
  6. #define RW_SET RW=1
  7. #define EN_CLR EN=0
  8. #define EN_SET EN=1
  9. #define DataPort P0

  10. sbit RS = P2^4;   //定義端口
  11. sbit RW = P2^5;
  12. sbit EN = P2^6;

  13. /*------------------------------------------------
  14. uS延時(shí)函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值
  15. unsigned char 是定義無符號(hào)字符變量,其值的范圍是
  16. 0~255 這里使用晶振12M,精確延時(shí)請(qǐng)使用匯編,大致延時(shí)
  17. 長(zhǎng)度如下 T=tx2+5 uS
  18. ------------------------------------------------*/
  19. void DelayUs2x(unsigned char t)
  20. {   
  21. while(--t);
  22. }
  23. /*------------------------------------------------
  24. mS延時(shí)函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值
  25. unsigned char 是定義無符號(hào)字符變量,其值的范圍是
  26. 0~255 這里使用晶振12M,精確延時(shí)請(qǐng)使用匯編
  27. ------------------------------------------------*/
  28. void DelayMs(unsigned char t)
  29. {
  30.      
  31. while(t--)
  32. {
  33.      //大致延時(shí)1mS
  34.      DelayUs2x(245);
  35.          DelayUs2x(245);
  36. }
  37. }
  38. /*------------------------------------------------
  39.               判忙函數(shù)
  40. ------------------------------------------------*/
  41. bit LCD_Check_Busy(void)
  42. {
  43. DataPort= 0xFF;
  44. RS_CLR;
  45. RW_SET;
  46. EN_CLR;
  47. _nop_();
  48. EN_SET;
  49. return (bit)(DataPort & 0x80);
  50. }
  51. /*------------------------------------------------
  52.               寫入命令函數(shù)
  53. ------------------------------------------------*/
  54. void LCD_Write_Com(unsigned char com)
  55. {  
  56. while(LCD_Check_Busy());//忙則等待
  57. DelayMs(5);
  58. RS_CLR;
  59. RW_CLR;
  60. EN_SET;
  61. DataPort=com;
  62. _nop_();
  63. EN_CLR;
  64. }
  65. /*------------------------------------------------
  66.               寫入數(shù)據(jù)函數(shù)
  67. ------------------------------------------------*/
  68. void LCD_Write_Data(unsigned char Data)
  69. {

  70. DelayMs(5);
  71. RS_SET;
  72. RW_CLR;
  73. EN_SET;
  74. DataPort= Data;
  75. _nop_();
  76. EN_CLR;
  77. }

  78. /*------------------------------------------------
  79.                 清屏函數(shù)
  80. ------------------------------------------------*/
  81. void LCD_Clear(void)
  82. {
  83. LCD_Write_Com(0x01);
  84. DelayMs(5);
  85. }
  86. /*------------------------------------------------
  87.               寫入字符串函數(shù)
  88. ------------------------------------------------*/
  89. void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
  90. {     
  91. if (y == 0)
  92.         {     
  93.          LCD_Write_Com(0x80 + x);     //表示第一行
  94.         }
  95. else
  96.         {      
  97.         LCD_Write_Com(0xC0 + x);      //表示第二行
  98.         }        
  99. while (*s)
  100.         {     
  101. LCD_Write_Data( *s);     
  102. s ++;     
  103.         }
  104. }
  105. /*------------------------------------------------
  106.               寫入字符函數(shù)
  107. ------------------------------------------------*/
  108. void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
  109. {     
  110. if (y == 0)
  111.         {     
  112.         LCD_Write_Com(0x80 + x);     
  113.         }   
  114. else
  115.         {     
  116.         LCD_Write_Com(0xC0 + x);     
  117.         }        
  118. LCD_Write_Data( Data);  
  119. }
  120. /*------------------------------------------------
  121.               初始化函數(shù)
  122. ------------------------------------------------*/
  123. void LCD_Init(void)
  124. {
  125.    LCD_Write_Com(0x38);    /*顯示模式設(shè)置*/
  126.    DelayMs(5);
  127.    LCD_Write_Com(0x38);
  128.    DelayMs(5);
  129.    LCD_Write_Com(0x38);
  130.    DelayMs(5);
  131.    LCD_Write_Com(0x38);  
  132.    LCD_Write_Com(0x08);    /*顯示關(guān)閉*/
  133.    LCD_Write_Com(0x01);    /*顯示清屏*/
  134.    LCD_Write_Com(0x06);    /*顯示光標(biāo)移動(dòng)設(shè)置*/
  135.    DelayMs(5);
  136.    LCD_Write_Com(0x0C);    /*顯示開及光標(biāo)設(shè)置*/
  137. }
復(fù)制代碼



作者: whengfu    時(shí)間: 2016-1-11 11:39
好東西,菜鳥的福音,收藏了
作者: 愛的步伐XY    時(shí)間: 2016-1-11 15:23
不錯(cuò),下載看看
作者: xql1997    時(shí)間: 2016-7-19 22:10
很好很好   感謝樓主




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1