找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3563|回復(fù): 0
收起左側(cè)

簡單的LCD1602顯示代碼

[復(fù)制鏈接]
ID:297346 發(fā)表于 2018-3-26 16:52 | 顯示全部樓層 |閱讀模式
這個(gè)是用C寫的LCD1602顯示代碼,通過AT89c52單片機(jī)驅(qū)動。經(jīng)過本人檢測,用proteus仿真和實(shí)物都可以顯示。需要三個(gè)代碼編譯成.hex文件再放入芯片中。
  1. //1.  
  2. //filename:glocal.h
  3. #define uchar unsigned char  
  4. #define uint unsigned int  
  5. //2.  
  6. //filename:ks0066.H
  7. #define DATA_PORT P1  
  8. //-------------------------------------------  
  9. //              管腳定義     
  10. //------------------------------------------  
  11. sbit RS=P2^0;     //數(shù)據(jù)(L)/命令(H)選擇  
  12. sbit LCDRW=P2^1;     //寫,低電平有效  
  13. sbit EN=P2^2;     //使能,低電平有效  
  14. //===========================================  
  15. //              函數(shù)聲明  
  16. //-------------------------------------------  
  17. void delay_lcd(char);  
  18. void write_data(char);  
  19. void write_com(char);  
  20. void lcd_init();  
  21. void delay_ms(char);  
  22. void print_string(char*);  
  23. //--------------------------------------------  
  24. //              寫數(shù)據(jù)函數(shù)  
  25. //--------------------------------------------  
  26. void write_data(char dat){  
  27. RS=1;          //數(shù)據(jù)  
  28. LCDRW=0;        //寫  
  29. DATA_PORT=dat; //把數(shù)據(jù)送到P口  
  30. delay_ms(5);      //當(dāng)晶振較高時(shí)加延時(shí)  
  31. EN=1;  
  32. delay_ms(5);    //當(dāng)晶振較高時(shí)加延時(shí)  
  33. EN=0;          //關(guān)使能  
  34. }  
  35. //--------------------------------------------  
  36. //              寫命令函數(shù)  
  37. //--------------------------------------------  
  38. void write_com(char com){  
  39. RS=0;          //命令  
  40. LCDRW=0;        //寫  
  41. DATA_PORT=com;  
  42. delay_ms(5);    //當(dāng)晶振較高時(shí)加延時(shí)  
  43. EN=1;  
  44. delay_ms(5);    //當(dāng)晶振較高時(shí)加延時(shí)  
  45. EN=0;  
  46. }  
  47. //--------------------------------------------  
  48. //        1602初始化函數(shù)  
  49. //--------------------------------------------  
  50. void lcd_init(){  
  51.     LCDRW=0;  
  52.     RS=0;  
  53. write_com(0x36);     //set text home address  
  54. write_com(0x0f);     //display switch,display on,cursor on,blink on.  
  55. write_com(0x38);  
  56. write_com(0x06);     //input set.  
  57. write_com(0x01);     //screen clear  
  58. write_com(0x80);    //DDRAM AD set to 00h,  
  59. }  
  60. void print_string(char* str){  
  61.     int i;  
  62.     for(i=0;str[i]!=0;i++)  
  63.     {  
  64.          
  65.         write_data(str[i]);  
  66.     }  
  67. }  
  68. //--------------------------------------------  
  69. //              延時(shí)函數(shù)  
  70. //--------------------------------------------  
  71. void delay_ms(uchar t){  
  72.     int j;  
  73. for(;t!=0; t--)  
  74.     for (j=0;j<255;j++);  
  75.       
  76. }  
  77. //3.
  78. #include<reg51.h>  
  79. #include"global.h"  
  80. #include "ks0066.h" // for lcd 1602  
  81. main(){   
  82.     lcd_init();  
  83.       
  84.     print_string("Hello World!");  
  85.     write_com(0xc0);    //DDRAM AD set to 00h,  
  86.     print_string("This is Alex!");  
  87.     //write_com(0xc0);    //DDRAM AD set to 00h,  
  88.     //print_string("XXX!");  
  89. while(1);  
  90. }  
復(fù)制代碼


回復(fù)

使用道具 舉報(bào)

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

本版積分規(guī)則

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

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

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