4*4矩阵键盘输入数字在1602上显示出来
时间:10-02
整理:3721RD
点击:
- #include<reg51.h>
- #include<absacc.h>
- #define XX P1
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
-
- unsigned char code table[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
- /* 液晶1602口地址 */
- #define wr_com XBYTE[0xC000] //写命令
- #define wr_data XBYTE[0xC100] //写数据
- #define rd_com XBYTE[0xC200] //读命令
- #define rd_data XBYTE[0xC300] //读数据
- void Delay(WORD n)
- {
- WORD x;
- while(n--)
- {
- x=500;
- while(x--);
- }
- }
- /************************************************************************
- Function name: write_cmd
- Descriptions: 向lcd输入指令
- ************************************************************************/
- void write_cmd(BYTE cmd)
- {
- BYTE dl;
- do {
- dl=rd_com;
- } while((dl&0x80)!=0); //判忙
- wr_com= cmd;
- Delay(1);
- }
- /**********************************************************************
- Function name: write_data
- Descriptions: 写入数据
- **********************************************************************/
- void write_data(BYTE dat)
- {
- BYTE dl;
- do {
- dl=rd_com;
- } while((dl&0x80)!=0); //判忙
- wr_data= dat;
- Delay(1);
- }
- /***************************************************
- ************液晶模块初始化******************************
- ****************************************************/
- void lcd_init(void)
- {
- write_cmd(0x38);
- write_cmd(0x0e);
- write_cmd(0x06);
- write_cmd(0x01);
- }
- unsigned char KeyScan()
- {
- unsigned char KeyData;
- unsigned char KeyNum;
- XX=0xfe;//检测第一行
- KeyData=XX;
- KeyData=KeyData&0xf0;
- if(KeyData!=0xf0)
- {
- Delay(2);
- if(KeyData!=0xf0)
- {
- KeyData=XX;
- switch(KeyData)
- {
- case 0xee:KeyNum=1;break;
- case 0xde:KeyNum=2;break;
- case 0xbe:KeyNum=3;break;
- case 0x7e:KeyNum=4;break;
- }
- while(KeyData!=0xf0)
- {
- KeyData=XX;
- KeyData=KeyData&0xf0;
- }
- }
- }
- XX=0xfd;//检测第二行
- KeyData=XX;
- KeyData=KeyData&0xf0;
- if(KeyData!=0xf0)
- {
- Delay(2);
- if(KeyData!=0xf0)
- {
- KeyData=XX;
- switch(KeyData)
- {
- case 0xed:KeyNum=5;break;
- case 0xdd:KeyNum=6;break;
- case 0xbd:KeyNum=7;break;
- case 0x7d:KeyNum=8;break;
- }
- while(KeyData!=0xf0)
- {
- KeyData=XX;
- KeyData=KeyData&0xf0;
- }
- }
- }
- XX=0xfb;//检测第三行
- KeyData=XX;
- KeyData=KeyData&0xf0;
- if(KeyData!=0xf0)
- {
- Delay(2);
- if(KeyData!=0xf0)
- {
- KeyData=XX;
- switch(KeyData)
- {
- case 0xeb:KeyNum=9;break;
- case 0xdb:KeyNum=10;break;
- case 0xbb:KeyNum=11;break;
- case 0x7b:KeyNum=12;break;
- }
- while(KeyData!=0xf0)
- {
- KeyData=XX;
- KeyData=KeyData&0xf0;
- }
- }
- }
- XX=0xf7;//检测第四行
- KeyData=XX;
- KeyData=KeyData&0xf0;
- if(KeyData!=0xf0)
- {
- Delay(2);
- if(KeyData!=0xf0)
- {
- KeyData=XX;
- switch(KeyData)
- {
- case 0xe7:KeyNum=13;break;
- case 0xd7:KeyNum=14;break;
- case 0xb7:KeyNum=15;break;
- case 0x77:KeyNum=16;break;
- }
- while(KeyData!=0xf0)
- {
- KeyData=XX;
- KeyData=KeyData&0xf0;
- }
- }
- }
- return KeyNum;
- }
- void main()
- {
- unsigned char i;
- lcd_init();
- while(1)
- {
- i=KeyScan();
- write_cmd(0x80+i-1);
- write_data(table[KeyScan()-1]);
- // write_cmd(0x01);
- }
- }
根本
你把这个absacc.h头文件也发出来看看
sorry,我还以为这个头文件是定义LCD1602端口的