找回密码
 立即注册

QQ登录

只需一步,快速开始

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

简单的LCD1602显示代码

[复制链接]
跳转到指定楼层
楼主
ID:297346 发表于 2018-3-26 16:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这个是用C写的LCD1602显示代码,通过AT89c52单片机驱动。经过本人检测,用proteus仿真和实物都可以显示。需要三个代码编译成.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;     //数据(L)/命令(H)选择  
  12. sbit LCDRW=P2^1;     //写,低电平有效  
  13. sbit EN=P2^2;     //使能,低电平有效  
  14. //===========================================  
  15. //              函数声明  
  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. //              写数据函数  
  25. //--------------------------------------------  
  26. void write_data(char dat){  
  27. RS=1;          //数据  
  28. LCDRW=0;        //写  
  29. DATA_PORT=dat; //把数据送到P口  
  30. delay_ms(5);      //当晶振较高时加延时  
  31. EN=1;  
  32. delay_ms(5);    //当晶振较高时加延时  
  33. EN=0;          //关使能  
  34. }  
  35. //--------------------------------------------  
  36. //              写命令函数  
  37. //--------------------------------------------  
  38. void write_com(char com){  
  39. RS=0;          //命令  
  40. LCDRW=0;        //写  
  41. DATA_PORT=com;  
  42. delay_ms(5);    //当晶振较高时加延时  
  43. EN=1;  
  44. delay_ms(5);    //当晶振较高时加延时  
  45. EN=0;  
  46. }  
  47. //--------------------------------------------  
  48. //        1602初始化函数  
  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. //              延时函数  
  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. }  
复制代码


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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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