找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机+lcd12864清屏显示出错?

[复制链接]
跳转到指定楼层
楼主
实验器材:51单片机(实际用的是stc12,51单片机出问题了)、lcd12864夜晶带中文字库、4x4矩阵键盘、蜂鸣器、led(后面两个器材有问题,可忽略)


问题:昨天研究到深夜lcd显示技术,遇到显示后清屏不起作用,前后的数据来回显示。求走过路过的大佬指教指教小弟,留下大佬光辉的足迹
代码奉上:
lcd12864.h

  1. #ifndef __LCD12864_H
  2. #define __LCD12864_H
  3. #include<stc15.h>
  4. #define xchar unsigned char code
  5. #define uchar unsigned char

  6. #endif

  7. #ifndef uint
  8. #define uint unsigned int
  9. #endif
  10. #define LCD12864_DATAPORT P2

  11. sbit LCD12864_RS  =  P0^7;
  12. sbit LCD12864_RW  =  P0^6;
  13. sbit LCD12864_EN  =  P0^5;
  14. sbit LCD12864_PSB =  P0^4;
  15. sbit LCD12864_RST =  P0^3;
  16. void LCD12864_Delay1ms(uint c);
  17. uchar LCD12864_Busy();
  18. void LCD12864_WriteCmd(uchar cmd);
  19. void LCD12864_WriteData(uchar dat);
  20. void LCD12864_Init();
  21. void LCD12864_ClearScreen(void);
  22. void LCD12864_SetWindow(uchar x, uchar y);
  23. void LCD12864_Delay1ms(uint c)
  24. {
  25.     uchar a,b;
  26.         for(; c>0; c--)
  27.         {
  28.             for(b=199; b>0; b--)
  29.                 {
  30.                 for(a=1; a>0; a--);
  31.                 }
  32.         }
  33. }
  34. uchar LCD12864_Busy(void)
  35. {
  36.         uchar i = 0;
  37.         LCD12864_RS = 0;
  38.         LCD12864_RW = 1;
  39.         LCD12864_EN = 1;
  40.         LCD12864_Delay1ms(1);
  41.         while((LCD12864_DATAPORT & 0x80) == 0x80)
  42.         {
  43.                 i++;
  44.                 if(i > 100)
  45.                 {
  46.                         LCD12864_EN = 0;
  47.                         return 0;
  48.                 }
  49.         }
  50.         LCD12864_EN = 0;
  51.         return 1;
  52. }
  53. void LCD12864_WriteCmd(uchar cmd)
  54. {
  55.         uchar i;
  56.         i = 0;
  57.         while( LCD12864_Busy() == 0)
  58.         {
  59.                 LCD12864_Delay1ms(1);
  60.                 i++;
  61.                 if( i>100)
  62.                 {
  63.                         return;
  64.                 }        
  65.         }
  66.         LCD12864_RS = 0;
  67.         LCD12864_RW = 0;
  68.         LCD12864_EN = 0;
  69.         LCD12864_DATAPORT = cmd;
  70.         LCD12864_EN = 1;
  71.         LCD12864_Delay1ms(5);
  72.         LCD12864_EN = 0;                                            
  73. }
  74. void LCD12864_WriteData(uchar dat)
  75. {
  76.         uchar i;
  77.         i = 0;
  78.         while( LCD12864_Busy() == 0)
  79.         {
  80.                 LCD12864_Delay1ms(1);
  81.                 i++;
  82.                 if( i>100)
  83.                 {
  84.                         return;
  85.                 }        
  86.         }
  87.         LCD12864_RS = 1;
  88.         LCD12864_RW = 0;
  89.         LCD12864_EN = 0;
  90.         LCD12864_DATAPORT = dat;
  91.         LCD12864_EN = 1;
  92.         LCD12864_Delay1ms(5);
  93.         LCD12864_EN = 0;                                                                    
  94. }
  95. #ifdef LCD12864_PICTURE
  96. uchar LCD12864_ReadData(void)
  97. {
  98.         uchar i, readValue;
  99.         i = 0;
  100.         while( LCD12864_Busy() == 0)
  101.         {
  102.                 LCD12864_Delay1ms(1);
  103.                 i++;
  104.                 if( i>100)
  105.                 {
  106.                         return 0;
  107.                 }        
  108.         }
  109.         LCD12864_RS = 1;
  110.         LCD12864_RW = 1;
  111.         LCD12864_EN = 0;
  112.         LCD12864_Delay1ms(1);
  113.         LCD12864_EN = 1;
  114.         LCD12864_Delay1ms(1);
  115.         readValue = LCD12864_DATAPORT;
  116.         LCD12864_EN = 0;
  117.         return readValue;
  118. }
  119. #endif
  120. void LCD12864_Init()
  121. {
  122.         LCD12864_PSB = 1;
  123.         LCD12864_RST = 1;
  124.         LCD12864_WriteCmd(0x30);
  125.         LCD12864_WriteCmd(0x0c);
  126.         LCD12864_WriteCmd(0x01);
  127. }
  128. #ifdef LCD12864_PICTURE
  129. void LCD12864_ClearScreen(void)
  130. {
  131.         uchar i,j;
  132.         LCD12864_WriteCmd(0x34);
  133.         for(i=0;i<32;i++)
  134.         {
  135.                 LCD12864_WriteCmd(0x80+i);
  136.                 LCD12864_WriteCmd(0x80);
  137.                 for(j=0;j<32;j++)
  138.                 {
  139.                         LCD12864_WriteData(0x01);        
  140.                 }
  141.         }
  142.         LCD12864_WriteCmd(0x36);
  143.         LCD12864_WriteCmd(0x30);
  144.         
  145.         
  146.         
  147. }
  148. #endif
  149. void LCD12864_SetWindow(uchar x, uchar y)
  150. {
  151.         uchar pos;
  152.         if(x == 0)
  153.         {
  154.                 x = 0x80;
  155.         }
  156.         else if(x == 1)
  157.         {
  158.                 x = 0x90;        
  159.         }
  160.         else if(x == 2)
  161.         {
  162.                 x = 0x88;
  163.         }
  164.         else if(x == 3)
  165.         {
  166.                 x = 0x98;
  167.         }
  168.         pos = x + y;
  169.         LCD12864_WriteCmd(pos);
  170. }
复制代码





main.c



  1. #include<STC15.H>
  2. #include"lcd12864.h"
  3. #include<string.h>
  4. #include<intrins.h>
  5. #include<math.h>
  6. uchar hang;
  7. #define KeyBus P1
  8. sbit fengmingqi=P0^0;
  9. sbit led=P3^0;
  10. xchar mimacuowu[]={"密码错误"};
  11. xchar mimazhengque[]={"密码正确"};
  12. xchar CorpInf1[]={"请设置密码:"};
  13. xchar CorpInf2[]={"请输入密码:"};
  14. xchar CorpInf[]={        
  15.         "lcd12864清屏前后的shuju"
  16. };

  17. unsigned char keyscan(void)
  18. {
  19.     unsigned char temH, temL, key;
  20.     KeyBus = 0x0f;                 
  21.         if(KeyBus!=0x0f)
  22.         {        
  23.                 temL = KeyBus;
  24.             KeyBus = 0xf0;
  25.                 _nop_();_nop_();_nop_();_nop_();
  26.                 temH = KeyBus;        
  27.             switch(temL)
  28.                 {
  29.                         case 0x0e: key = 1; break;
  30.                         case 0x0d: key = 2; break;
  31.                         case 0x0b: key = 3; break;
  32.                         case 0x07: key = 4; break;
  33.                         default: return 0;
  34.             }
  35.             switch(temH)
  36.                 {
  37.                         case 0xe0: return key;break;
  38.                         case 0xd0: return key + 4;break;
  39.                         case 0xb0: return key + 8;break;
  40.                         case 0x70: return key + 12;break;
  41.                         default: return 0;
  42.                 }
  43.         }return 0;
  44. }
  45. void gpio()
  46. {
  47.         P0M1=0;P0M0=0;
  48.         P1M1=0;P1M0=0;
  49.         P2M1=0;P2M0=0;
  50.         P3M1=0;P3M0=0;
  51.         P4M1=0;P4M0=0;
  52.         P5M1=0;P5M0=0;
  53.         P6M1=0;P6M0=0;
  54.         P7M1=0;P7M0=0;
  55. }

  56. void lcd(xchar *CorpInf,uchar n)
  57. {
  58.         unsigned char i=0,m=0;
  59.         switch(n)
  60.         {
  61.                 case 0:LCD12864_SetWindow(0, 0);m=0;break;
  62.                 case 1:LCD12864_SetWindow(1, 0);m=1;break;
  63.                 case 2:LCD12864_SetWindow(2, 0);m=2;break;
  64.                 case 3:LCD12864_SetWindow(3, 0);m=3;break;
  65.         }
  66.         
  67.         while(CorpInf[i]!='\0')
  68.         {               
  69.                 LCD12864_WriteData(CorpInf[i]);
  70.                 i++;
  71.                 switch(m)
  72.                 {
  73.                         case 0:if(i==16){LCD12864_SetWindow(1,0);}if(i==32){LCD12864_SetWindow(2,0);}if(i==48){LCD12864_SetWindow(3,0);}break;
  74.                         case 1:if(i==16)LCD12864_SetWindow(2,0);if(i==32)LCD12864_SetWindow(3,0);break;
  75.                         case 2:if(i==16)LCD12864_SetWindow(3,0);break;
  76.                         default:break;
  77.                 }
  78.         }
  79. }
  80. int mima_shuru()
  81. {
  82.         uint i,ws,shuju=0;uchar string[]={0,0,0,0,0,0,0,0,0,0,0,0,0};//键盘值,位数,输入的数据,写入的数据
  83.         for(ws=0;ws<13;ws++)
  84.         {
  85.                 i=keyscan();
  86.                 while(i==0);                                                                //等待键盘输入
  87.                 led=0;fengmingqi=1;
  88.                 i-=1;
  89.                 if(i==10)break;                                                                                        //10为确定
  90.                 if(i>10)
  91.                 {
  92.                         ws--;continue;                                                                                //大于10为无效键
  93.                 }
  94.                 string[ws]=i*pow(10,ws);                                                //进行10次幂写入数据
  95.         }
  96.         for(;ws>=0;ws--)
  97.         {
  98.                 shuju=shuju+string[ws];                                                        //计算出每次写入的数据之和
  99.         }return shuju;                                                                                                //返回输入的数据
  100. }

  101. void main()
  102. {
  103.         uint xh=1,yzm,mima;                                                                                //对错是否循环,验证码(测试的密码),设定的密码
  104.         gpio();                                                                                                                                //I/O初始化
  105. //        LCD12864_Init();                                                                                        //lcd初始化
  106. //        lcd(CorpInf,0);                                                                                                //lcd显示屏初始界面
  107. //        LCD12864_Delay1ms(5000);
  108. //        
  109. //        //LCD12864_Init();
  110. //        //LCD12864_ClearScreen();
  111. //        LCD12864_WriteData(0x01);
  112. //        LCD12864_Delay1ms(5);
  113. //        
  114. //        
  115. //        
  116. //        //LCD12864_Delay1ms(5000);                                                                //延时显示5s
  117. //        
  118. //        //LCD12864_ClearScreen();
  119. //        
  120. //        lcd(CorpInf1,0);
  121. //        LCD12864_Delay1ms(5000);
  122.                                                                                 //设置密码
  123.         while(xh)
  124.         {
  125.                 mima=mima_shuru();
  126.                 lcd(CorpInf2,0);
  127.                 yzm=mima_shuru();                                                                                //测试的密码
  128.                 if(yzm==mima)
  129.                 {
  130.                         lcd(mimazhengque,2);//密码正确,退出循环
  131.                         xh=2;
  132.                 }else lcd(mimacuowu,3);//密码错误,继续输入密码
  133.         }
  134.         
  135.         //
  136.                

  137. }
复制代码

IMG_20191212_003029.gif (2.37 MB, 下载次数: 44)

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

使用道具 举报

沙发
ID:646787 发表于 2019-12-12 11:45 | 只看该作者
LCD12864_Init()函数的注释忘取消了
回复

使用道具 举报

板凳
ID:646787 发表于 2019-12-12 12:13 | 只看该作者
问题已解觉,是我自己的问题,清屏代码复制成写数据【哭笑】,好像还没审核过,算了当作回忆留在那

评分

参与人数 1黑币 +50 收起 理由
admin + 50 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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