找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1190|回复: 3
收起左侧

单片机程序无错误但无法输入矩阵键盘值LCD1602上的显示总是一闪而过

[复制链接]
ID:888357 发表于 2021-6-23 21:54 | 显示全部楼层 |阅读模式
  1. #include<reg52.h>

  2. #define uchar unsigned char  
  3. #define uint unsigned int
  4. extern int KeyDown();        
  5. extern void LcdInit();
  6. extern void Lcd1602_Delay1ms(uint c);         
  7. extern void LcdWriteCom(uchar com);
  8. extern void LCD_Write_String(uchar x,uchar y,uchar *s);
  9. extern void LCD_Write_Char(uchar x,uchar y,uchar Data);
  10. uchar code password[]={1,2,3,4,1,1};

  11. sbit led=P3^0;

  12. main()
  13. {
  14.   uchar num,i,j;                  
  15.         uchar passwordtemp[6];        

  16.         uchar passwordlength,PLEN;     
  17.   bit flag;
  18.   PLEN=sizeof(password)/sizeof(password[0]);

  19.   PLEN=6;
  20.         LcdInit()        ;         
  21.   Lcd1602_Delay1ms(100);      
  22.         LcdWriteCom(0x01);      
  23.         LCD_Write_String(0,0,"  Welcome! ");   
  24.   LCD_Write_String(0,1,"Input password!");   

  25. while(1)
  26. {        
  27.                         num=KeyDown();  
  28.                         if(num!=0xff)  
  29.                         {
  30.                                 if(i==0)  
  31.                                 LCD_Write_String(0,0,"                ");
  32.                                 if(i<9)
  33.                                 {
  34.                                         passwordtemp[i]=num;
  35.                                         LCD_Write_Char(i,1,i);
  36.                                 }
  37.                                 i++;   
  38.                                 if(i==6)
  39.                                 {
  40.                                                 passwordlength=i-1;
  41.                                                 i=0;
  42.                                         if(passwordlength==PLEN)
  43.                                         {        
  44.                                                 flag=1;
  45.                                                 for(j=0;j<PLEN;j++)
  46.                                                 flag=flag&&(passwordtemp[j]==password[j]);
  47.                                         }
  48.                                         if(flag)
  49.                                         {
  50.                                                 LCD_Write_String(0,1,"                       ");
  51.                                                 LCD_Write_String(0,1,"Right Open!>>>>");                                       
  52.                                           flag=0;     
  53.                                         }
  54.                                         else
  55.                                         {
  56.                                                 LCD_Write_String(0,1,"                      ");
  57.                                                 LCD_Write_String(0,1,"Wrong! Retry!");
  58.                                        
  59.                                                 while(1);
  60.                                         }

  61.                                 }
  62.                         }        
  63.                 }

  64.         }


  65. #include"reg52.h"
  66. #define LCD1602_DATAPINS P0

  67. #define uchar unsigned char
  68. #define uint unsigned int
  69. uchar x;
  70. sbit LCD1602_RW=P2^5;
  71. sbit LCD1602_RS=P2^6;
  72. sbit LCD1602_E=P2^7;

  73. void Lcd1602_Delay1ms(uint c);   

  74. void LcdWriteCom(uchar com);

  75. void LcdWriteData(uchar dat)        ;
  76.                
  77. void LcdInit();                                                  

  78. void LCD_Write_String(uchar x,uchar y,uchar *s);
  79.          
  80. void LCD_Write_Char(uchar x,uchar y,uchar Data);

  81. void Lcd1602_Delay1ms(uint c)            
  82. {
  83.     uchar a,b;
  84.         for (; c>0; c--)
  85.         {
  86.                  for (b=199;b>0;b--)
  87.                  {
  88.                           for(a=1;a>0;a--);
  89.                  }      
  90.         }
  91.             
  92. }


  93. void LcdWriteCom(uchar com)         
  94. {
  95.         LCD1602_E = 0;   
  96.         LCD1602_RS = 0;           
  97.         LCD1602_RW = 0;           
  98.         
  99.         LCD1602_DATAPINS = com;     
  100.         Lcd1602_Delay1ms(1);               

  101.         LCD1602_E = 1;                  
  102.         Lcd1602_Delay1ms(5);         
  103.         LCD1602_E = 0;
  104. }

  105. void LcdWriteData(uchar dat)                        
  106. {
  107.         LCD1602_E = 0;        
  108.         LCD1602_RS = 1;
  109.         LCD1602_RW = 0;        

  110.         LCD1602_DATAPINS = dat;
  111.         Lcd1602_Delay1ms(1);

  112.         LCD1602_E = 1;   
  113.         Lcd1602_Delay1ms(5);  
  114.         LCD1602_E = 0;
  115. }
  116.            
  117. void LcdInit()                                                  
  118. {
  119.          LcdWriteCom(0x38);   
  120.         LcdWriteCom(0x0c);
  121.         LcdWriteCom(0x06);  
  122.         LcdWriteCom(0x01);
  123.         
  124. }







  125. void LCD_Write_String(uchar x,uchar y,uchar *s)
  126. {         
  127.          while (*s)
  128.          {     
  129.                  LCD_Write_Char(x,y,*s);     
  130.                  s ++;  x++;   
  131.          }
  132. }

  133. void LCD_Write_Char(uchar x,uchar y,uchar Data)
  134. {     
  135.         if (y == 0)
  136.          {     
  137.          LcdWriteCom(0x80 + x);     
  138.          }   
  139.          else
  140.          {     
  141.                  LcdWriteCom(0xC0 + x);     
  142.          }        
  143.          LcdWriteData( Data);  
  144. }


  145. #include<reg52.h>
  146. typedef unsigned int uint;
  147. typedef unsigned char uchar;
  148. #define GPIO_KEY P1
  149. uchar KeyValue;
  150. uchar KeyState;
  151. uchar Disp[]="0,1,2,3,4,5,6,7,8,9,[,]";
  152. void Delay10ms();
  153. int KeyDown();

  154. int KeyDown()
  155. {
  156.    char a;
  157.    GPIO_KEY=0x0f;
  158.    if(GPIO_KEY!=0x0f)
  159.    {
  160.       Delay10ms();
  161.       if(GPIO_KEY!=0x0f)
  162. {
  163.        KeyState=1;
  164.        GPIO_KEY=0X0F;
  165.        Delay10ms();
  166.      switch(GPIO_KEY)  
  167.    {
  168.       case(0x07):KeyValue=0;break;
  169.       case(0X0b):KeyValue=1;break;
  170.       case(0X0d):KeyValue=2;break;
  171.             case(0X0e):KeyValue=3;break;
  172.    }

  173. GPIO_KEY=0XF0;  
  174.                         Delay10ms();
  175.                         switch(GPIO_KEY)
  176.                         {
  177.                                 case(0X70):        KeyValue=KeyValue;break;
  178.                                 case(0Xb0):        KeyValue=KeyValue+4;break;
  179.                                 case(0Xd0): KeyValue=KeyValue+8;break;
  180.                                 case(0Xe0):        KeyValue=KeyValue+12;break;
  181.                                 }
  182.                
  183. while((a<50)&&(GPIO_KEY!=0xf0))         

  184. {
  185.        Delay10ms();
  186.                                 a++;
  187. }

  188. }
  189.       KeyValue= Disp[KeyValue];
  190.    }
  191. return KeyValue;
  192. }
  193. void Delay10ms(void)   
  194. {
  195.     uchar a,b,c;
  196.     for(c=1;c>0;c--)
  197.         for(b=38;b>0;b--)
  198.             for(a=130;a>0;a--);
  199. }
复制代码
回复

使用道具 举报

ID:123289 发表于 2021-6-24 09:27 | 显示全部楼层
一闪而过举例:
先显示了A,当你还未来得及看清A的时候,又显示了空白或其它什么。
这样A就一闪而过了。
查查,你是不是这样呢?
回复

使用道具 举报

ID:920992 发表于 2021-6-24 17:02 | 显示全部楼层
检测按键进去显示完加个while(1)循环试试。
回复

使用道具 举报

ID:888357 发表于 2021-6-24 21:39 | 显示全部楼层
yzwzfyz 发表于 2021-6-24 09:27
一闪而过举例:
先显示了A,当你还未来得及看清A的时候,又显示了空白或其它什么。
这样A就一闪而过了。
...

对就是这样
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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