矩阵键盘控制12864显示最经典程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
#define uint unsigned int//键盘扫描的思想是将行设置为低,列设置为高,来读取P3口的值,就能知道是哪个按键按下了
#define uchar unsigned char
#define LCDdata P0
sbit E = P2^7;
sbit RW = P2^6;
sbit RS = P2^5;
void init();
void delayms(uint x);
void displaykey();
void write_com(uchar com);//写命令
void write_data(uchar date);//写数据
uchar temp;
//--------------主函数-----------------
void main()
{
init();// P3=0xfe;//P3=0xfd;//P3=0xfb;//P3=0xf7;
while(1)
{
displaykey();
}
}
//-------------液晶初始化----------------
void init()
{
write_com(0x01);
write_com(0x02);
write_com(0x06);
write_com(0x0e);
}
//------------毫秒延时---------------
void delayms(uint x)
{
uchar i;
while(x--)
{
for(i=0;i<120;i++);
}
}
//------------写命令-----------------
void write_com(uchar com)
{
RS = 0;
RW = 0;
E = 0;
LCDdata = com;
E = 1;
delayms(5);
E = 0;
}
//-------------写数据------------------
void write_data(uchar date)
{
RS = 1;
RW = 0;
E = 0;
LCDdata = date;
E = 1;
delayms(5);
E = 0;
}
//----
void displaykey()
{ //****************************判断第一行的键盘
P3=0xfe; temp=P3; temp=temp&0xf0;
while(temp!=0xf0)//判断是否有键盘按下为真有键盘按下
{ delayms(50); temp=P3; temp=temp&0xf0;
while(temp!=0xf0)//再次判断是否有键盘按下为真有键盘按下
{ temp=P3;
switch(temp)
{
case 0xee:write_com(0x80);write_data('0');write_com(0x81);write_data(' ');break;
case 0xde:write_com(0x80);write_data('1');write_com(0x81);write_data(' ');break;
case 0xbe:write_com(0x80);write_data('2');write_com(0x81);write_data(' ');break;
case 0x7e:write_com(0x80);write_data('3');write_com(0x81);write_data(' ');break;
}break;//这个break很重要不能丢下,若丢下会这这个中退步出来
}
}
//******************************判断第二行的键盘
P3=0xfd; temp=P3; temp=temp&0xf0;
while(temp!=0xf0)
{ delayms(50); temp=P3; temp=temp&0xf0;
while(temp!=0xf0)
{ temp=P3;
switch(temp)
{
case 0xed:write_com(0x80);write_data('4');write_com(0x81);write_data(' ');break;
case 0xdd:write_com(0x80);write_data('5');write_com(0x81);write_data(' ');break;
case 0xbd:write_com(0x80);write_data('6');write_com(0x81);write_data(' ');break;
case 0x7d:write_com(0x80);write_data('7');write_com(0x81);write_data(' ');break;
} break;
}
}
//******************************判断第三行的键盘
P3=0xfb; temp=P3; temp=temp&0xf0;
while(temp!=0xf0)
{ delayms(50); temp=P3; temp=temp&0xf0;
while(temp!=0xf0)
{ temp=P3;
switch(temp)
{
case 0xeb:write_com(0x80);write_data('8');write_com(0x81);write_data(' ');break;
case 0xdb:write_com(0x80);write_data('9');write_com(0x81);write_data(' ');break;
case
0xbb:write_com(0x80);write_data('1');write_com(0x81);write_data('0');break;
case
0x7b:write_com(0x80);write_data('1');write_com(0x81);write_data('1');break;
}break;
}
}
//******************************判断第四行的键盘
P3=0xf7; temp=P3; temp=temp&0xf0;
while(temp!=0xf0)