论坛新人,今天调试出矩阵键盘1-16平方,lcd1602显示,现拿出,请大家指教。以下是程序:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar table[]="0123456789";
uchar num;
sbit lcd1602RS=P2^6;
sbit lcd1602RW=P2^5;
sbit lcd1602EN=P2^7;
void keyscan();
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delay1us()
{
uchar x,y;
for(x=10;x>0;x--)
for(y=110;y>0;y--);
}
void lcd1602write(uchar com) //lcd1602
{
lcd1602RS=0;
lcd1602RW=0;
lcd1602EN=0;
P0=com;
delay(10);
lcd1602EN=1;
delay(10);
lcd1602EN=0;
}
void lcd1602read(uchar date)
{
lcd1602RS=1;
lcd1602RW=0;
lcd1602EN=0;
P0=date;
delay(10);
lcd1602EN=1;
delay(10);
lcd1602EN=0;
}
void lcd1602init()
{
lcd1602write(0x38);
delay(15);
lcd1602write(0x38);
delay(5);
lcd1602write(0x38);
delay(5);
lcd1602write(0x01);
lcd1602write(0x08);
lcd1602write(0x06);
lcd1602write(0x0c);
}
void main()
{
lcd1602init();
while(1)
{
keyscan();
lcd1602write(0x80);
lcd1602read(table[(num*num)/100]);
lcd1602write(0x81);
lcd1602read(table[(num*num)%100/10]);
lcd1602write(0x82);
lcd1602read(table[(num*num)%10]);
}
}
void keyscan() //矩阵键盘扫描
{
if(P1!=0x7f)
{
delay1us();
if(P1!=0x7f)
{
P1=0x7f;
switch(P1)
{
case(0x77):num=1;break;
case(0x7b):num=2;break;
case(0x7d):num=3;break;
case(0x7e):num=4;break;
}
while(P1!=0x7f);
}
}
if(P1!=0xbf)
{
delay1us();
if(P1!=0xbf)
{
P1=0xbf;
switch(P1)
{
case(0xb7):num=5;break;
case(0xbb):num=6;break;
case(0xbd):num=7;break;
case(0xbe):num=8;break;
}
while(P1!=0xbf);
}
}
if(P1!=0xdf)
{
delay1us();
if(P1!=0xdf)
{
P1=0xdf;
switch(P1)
{
case(0xd7):num=9;break;
case(0xdb):num=10;break;
case(0xdd):num=11;break;
case(0xde):num=12;break;
}
while(P1!=0xdf);
}
}
if(P1!=0xef)
{
delay1us();
if(P1!=0xef)
{
P1=0xef;
switch(P1)
{
case(0xe7):num=13;break;
case(0xeb):num=14;break;
case(0xed):num=15;break;
case(0xee):num=16;break;
}
while(P1!=0xef);
}
}
}
|