标题: 51单片机lcd12864显示程序(资源共享) [打印本页]

作者: ttl12138    时间: 2019-7-24 11:43
标题: 51单片机lcd12864显示程序(资源共享)
  1. #include<reg52.h>
  2. #include<intrins.h>
  3. sbit io_LCD12864_RS=P2^0;
  4. sbit io_LCD12864_RW=P2^1;
  5. sbit io_LCD12864_EN=P2^2;

  6. #define io_LCD12864_DATAPORT P0

  7. #define SET_DATA io_LCD12864_RS=1;
  8. #define SET_INC io_LCD12864_RS=0;
  9. #define SET_READ io_LCD12864_RW=1;
  10. #define SET_WRITE io_LCD12864_RW=0;
  11. #define SET_EN io_LCD12864_EN=1;
  12. #define CLR_EN io_LCD12864_EN=0;


  13. void v_Lcd12864CheckBusy_f(void)
  14. {
  15. unsigned int nTimeOut=0;
  16. SET_INC
  17. SET_READ
  18. SET_EN        
  19. SET_EN
  20.         while((io_LCD12864_DATAPORT&0X80)&&(++nTimeOut!=0));
  21.         CLR_EN
  22.         SET_INC
  23.         SET_READ
  24. }

  25. void v_Lcd12864SendCmd_f(unsigned char byCmd)
  26. {
  27. v_Lcd12864CheckBusy_f();
  28.         SET_INC
  29. SET_WRITE
  30.         CLR_EN
  31.         io_LCD12864_DATAPORT=byCmd;
  32.         _nop_();
  33.         _nop_();
  34.         SET_EN
  35.         _nop_();
  36.         _nop_();
  37.         CLR_EN
  38.         SET_READ
  39.         SET_INC
  40. }
  41. void v_Lcd12864SendData_f(unsigned char byData)
  42. {
  43. v_Lcd12864CheckBusy_f();
  44. SET_DATA
  45.         SET_WRITE
  46.         CLR_EN
  47.         io_LCD12864_DATAPORT=byData;
  48.         _nop_();
  49.         _nop_();
  50.         SET_EN
  51.         _nop_();
  52.         _nop_();
  53.         CLR_EN
  54.         SET_READ
  55.         SET_INC
  56. }
  57. void v_DelayMs_f(unsigned int nDelay)
  58. {
  59.   unsigned int i;
  60.         for(;nDelay>0;nDelay--)
  61.         {
  62.           for(i=125;i>0;i--);
  63.         }
  64. }
  65. void v_Lcd12864Init_f(void)
  66. {
  67.   v_Lcd12864SendCmd_f(0x30);
  68.         v_DelayMs_f(50);
  69.         v_Lcd12864SendCmd_f(0x10);
  70.         v_DelayMs_f(50);
  71.         v_Lcd12864SendCmd_f(0x06);
  72.         v_DelayMs_f(50);
  73.         v_Lcd12864SendCmd_f(0x0c);
  74. }
  75. void v_Lcd12864SetAddress_f(unsigned char x,y)
  76. {
  77.   unsigned char byAddress;
  78.         switch(y)
  79.         {
  80.                 case 0: byAddress=0x80+x;
  81.                    break;
  82.                 case 1: byAddress=0x90+x;
  83.                    break;
  84.                 case 2: byAddress=0x88+x;
  85.                    break;
  86.           case 3: byAddress=0x98+x;
  87.                    break;
  88.                 default:
  89.                    break;        
  90.         }
  91.         v_Lcd12864SendCmd_f(byAddress);
  92. }
  93. void v_Lcd12864PutString_f(unsigned char x,unsigned char y,unsigned char*pData)
  94. {
  95.   v_Lcd12864SetAddress_f(x,y);
  96.         while(*pData!='\0')
  97.         {
  98.         v_Lcd12864SendData_f(*pData++);
  99.         }
  100. }
  101. void main(void)
  102. {
  103.         v_Lcd12864Init_f();
  104.         
  105.         v_Lcd12864PutString_f(0,0,"2019 6 7pm 16:30");
  106. v_Lcd12864PutString_f(0,1,51黑电子");
  107.         v_Lcd12864PutString_f(0,2,"认真学习");
  108. v_Lcd12864PutString_f        (0,3,"资源共享");
  109. while(1);
  110. }

复制代码


816F3B3149CDB7A71EE34DA88CFB0912.jpg (73.39 KB, 下载次数: 27)

lcd12864接线图

lcd12864接线图

作者: _nop_()    时间: 2019-7-25 01:54
为什么我用这代码什么都不能显示呢?是不是没有汉字库还是什么问题,引脚我已经改成我的了
作者: _nop_()    时间: 2019-7-25 01:55
为什么我不能用啊,试了好多代码,都崩溃了
作者: ttl12138    时间: 2019-7-29 11:57
_nop_() 发表于 2019-7-25 01:55
为什么我不能用啊,试了好多代码,都崩溃了

你把LCD显示接的电位器调节一下,电位器合适的时候就会有反应
作者: ttl12138    时间: 2019-7-29 11:58
_nop_() 发表于 2019-7-25 01:54
为什么我用这代码什么都不能显示呢?是不是没有汉字库还是什么问题,引脚我已经改成我的了

可能是电位器的缘故,你把电位器调节一下




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1