标题:
单片机程序无错误但无法输入矩阵键盘值LCD1602上的显示总是一闪而过
[打印本页]
作者:
hdfc
时间:
2021-6-23 21:54
标题:
单片机程序无错误但无法输入矩阵键盘值LCD1602上的显示总是一闪而过
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
extern int KeyDown();
extern void LcdInit();
extern void Lcd1602_Delay1ms(uint c);
extern void LcdWriteCom(uchar com);
extern void LCD_Write_String(uchar x,uchar y,uchar *s);
extern void LCD_Write_Char(uchar x,uchar y,uchar Data);
uchar code password[]={1,2,3,4,1,1};
sbit led=P3^0;
main()
{
uchar num,i,j;
uchar passwordtemp[6];
uchar passwordlength,PLEN;
bit flag;
PLEN=sizeof(password)/sizeof(password[0]);
PLEN=6;
LcdInit() ;
Lcd1602_Delay1ms(100);
LcdWriteCom(0x01);
LCD_Write_String(0,0," Welcome! ");
LCD_Write_String(0,1,"Input password!");
while(1)
{
num=KeyDown();
if(num!=0xff)
{
if(i==0)
LCD_Write_String(0,0," ");
if(i<9)
{
passwordtemp[i]=num;
LCD_Write_Char(i,1,i);
}
i++;
if(i==6)
{
passwordlength=i-1;
i=0;
if(passwordlength==PLEN)
{
flag=1;
for(j=0;j<PLEN;j++)
flag=flag&&(passwordtemp[j]==password[j]);
}
if(flag)
{
LCD_Write_String(0,1," ");
LCD_Write_String(0,1,"Right Open!>>>>");
flag=0;
}
else
{
LCD_Write_String(0,1," ");
LCD_Write_String(0,1,"Wrong! Retry!");
while(1);
}
}
}
}
}
#include"reg52.h"
#define LCD1602_DATAPINS P0
#define uchar unsigned char
#define uint unsigned int
uchar x;
sbit LCD1602_RW=P2^5;
sbit LCD1602_RS=P2^6;
sbit LCD1602_E=P2^7;
void Lcd1602_Delay1ms(uint c);
void LcdWriteCom(uchar com);
void LcdWriteData(uchar dat) ;
void LcdInit();
void LCD_Write_String(uchar x,uchar y,uchar *s);
void LCD_Write_Char(uchar x,uchar y,uchar Data);
void Lcd1602_Delay1ms(uint c)
{
uchar a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
void LcdWriteCom(uchar com)
{
LCD1602_E = 0;
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DATAPINS = com;
Lcd1602_Delay1ms(1);
LCD1602_E = 1;
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
void LcdWriteData(uchar dat)
{
LCD1602_E = 0;
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DATAPINS = dat;
Lcd1602_Delay1ms(1);
LCD1602_E = 1;
Lcd1602_Delay1ms(5);
LCD1602_E = 0;
}
void LcdInit()
{
LcdWriteCom(0x38);
LcdWriteCom(0x0c);
LcdWriteCom(0x06);
LcdWriteCom(0x01);
}
void LCD_Write_String(uchar x,uchar y,uchar *s)
{
while (*s)
{
LCD_Write_Char(x,y,*s);
s ++; x++;
}
}
void LCD_Write_Char(uchar x,uchar y,uchar Data)
{
if (y == 0)
{
LcdWriteCom(0x80 + x);
}
else
{
LcdWriteCom(0xC0 + x);
}
LcdWriteData( Data);
}
#include<reg52.h>
typedef unsigned int uint;
typedef unsigned char uchar;
#define GPIO_KEY P1
uchar KeyValue;
uchar KeyState;
uchar Disp[]="0,1,2,3,4,5,6,7,8,9,[,]";
void Delay10ms();
int KeyDown();
int KeyDown()
{
char a;
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f)
{
Delay10ms();
if(GPIO_KEY!=0x0f)
{
KeyState=1;
GPIO_KEY=0X0F;
Delay10ms();
switch(GPIO_KEY)
{
case(0x07):KeyValue=0;break;
case(0X0b):KeyValue=1;break;
case(0X0d):KeyValue=2;break;
case(0X0e):KeyValue=3;break;
}
GPIO_KEY=0XF0;
Delay10ms();
switch(GPIO_KEY)
{
case(0X70): KeyValue=KeyValue;break;
case(0Xb0): KeyValue=KeyValue+4;break;
case(0Xd0): KeyValue=KeyValue+8;break;
case(0Xe0): KeyValue=KeyValue+12;break;
}
while((a<50)&&(GPIO_KEY!=0xf0))
{
Delay10ms();
a++;
}
}
KeyValue= Disp[KeyValue];
}
return KeyValue;
}
void Delay10ms(void)
{
uchar a,b,c;
for(c=1;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
复制代码
作者:
yzwzfyz
时间:
2021-6-24 09:27
一闪而过举例:
先显示了A,当你还未来得及看清A的时候,又显示了空白或其它什么。
这样A就一闪而过了。
查查,你是不是这样呢?
作者:
ljq180
时间:
2021-6-24 17:02
检测按键进去显示完加个while(1)循环试试。
作者:
hdfc
时间:
2021-6-24 21:39
yzwzfyz 发表于 2021-6-24 09:27
一闪而过举例:
先显示了A,当你还未来得及看清A的时候,又显示了空白或其它什么。
这样A就一闪而过了。
...
对就是这样
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1