📄 lcdandkey.lst
字号:
C51 COMPILER V8.15 LCDANDKEY 08/10/2008 23:26:40 PAGE 1
C51 COMPILER V8.15, COMPILATION OF MODULE LCDANDKEY
OBJECT MODULE PLACED IN LCDandKEY.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE LCDandKEY.C BROWSE DEBUG OBJECTEXTEND
line level source
1 #include <reg51.h>
2 #define uchar unsigned char
3 #include <1602lcd.ini>
4 //按键说明:A,B:左右移,C:上/下移,D:切换输入,E:删除,F:确定
5
6 uchar key,x=0,keypre;
7 uchar instate=0; //instate为输入状态 0为大写字母,1为小写字母,2为数字,3为标点
8 uchar selectN,selectK;
9 unsigned int wait_count;
10 bit y=0;
11 bit affirm=0;
12 //------------------------------------------------------------------------------------
13
14 void delay(uchar ms){
15 1 uchar i;
16 1 while(ms--){
17 2 for(i=0;i<125;i++);
18 2 }
19 1 }
20
21 //------------------------------------------------------------------------------------
22
23 //-----------------------------------------------------------------------------------
24 int getkey(){
25 1 return ((~P1)&0xf0);
26 1 }
27 //-----------------------------------------------------------------------------------
28 //判断是否有键按下
29 void chkkey(){
30 1 if (getkey()) {
31 2 delay(0x2); //延时,去抖
32 2 delay(0x02);
33 2 }
34 1 }
35
36 //-----------------------------------------------------------------------------------
37 void findkey(){ //扫描法得出键值
38 1 uchar i,k,j;
39 1 i=(~P1)&0xf0;
40 1 j=0x10;
41 1 for (k=0;k<4;k++) {
42 2 if (i&j) key=k; //得出行号
43 2 j<<=1;
44 2 }
45 1 key=key*4;
46 1 P1=0xfe;
47 1 if ((~P1)&0xf0) key=key; //得出行键值=行号+列号
48 1 P1=0xfd;
49 1 if ((~P1)&0xf0) key=key+1;
50 1 P1=0xfb;
51 1 if ((~P1)&0xf0) key=key+2;
52 1 P1=0xf7;
53 1 if ((~P1)&0xf0) key=key+3;
54 1 //}
55 1 P1=0xf0;
C51 COMPILER V8.15 LCDANDKEY 08/10/2008 23:26:40 PAGE 2
56 1 }
57 //------------------------------------------------------------------------------------
58 void addchar(){
59 1 if (!selectK) return;
60 1 Send_Data_LCM(selectK);
61 1 x++;
62 1 if (x==16) {x=0;y=!y;}
63 1 selectK=0;
64 1 selectN=0;
65 1 affirm=0;
66 1
67 1 }
68 //------------------------------------------------------------------------------------
69 void keyfun(){
70 1 uchar temp1;
71 1 bit temp2;
72 1 if (key<=9) {
73 2 if (key!=keypre) addchar();
74 2 selectN++;
75 2 wait_count=0;
76 2 if (selectN==4) selectN=1;
77 2 if (instate==0) selectK=0x41+selectN+key*3-1;
78 2 if (instate==1) selectK=0x61+selectN+key*3-1;
79 2 if (instate==2) selectK=0x30+key;
80 2 if (instate==3) selectK=0x21+key;
81 2 Send_Data_LCM(selectK);
82 2 affirm=1;
83 2 Set_LCM_XY(x,y);
84 2 keypre=key;
85 2 return;
86 2 }
87 1 if (key==10) {x--;
88 2 if (x==255) x=0;
89 2 Set_LCM_XY(x,y);
90 2 selectN=0;
91 2 return;
92 2 }
93 1 if (key==11) {x++;
94 2 if (x==16) x=0;
95 2 Set_LCM_XY(x,y);
96 2 selectN=0;
97 2 return;
98 2 }
99 1 if (key==12) {
100 2 y=!y;
101 2 Set_LCM_XY(x,y);
102 2 selectN=0;
103 2 return;
104 2 }
105 1 if (key==13) {instate++;
106 2 if (instate==4) instate=0;
107 2 Set_LCM_XY(15,1);
108 2 if (instate==0) Send_Data_LCM(0x41);
109 2 if (instate==1) Send_Data_LCM(0x61);
110 2 if (instate==2) Send_Data_LCM(0x30);
111 2 if (instate==3) Send_Data_LCM(0x3b);
112 2 selectN=0;
113 2 Set_LCM_XY(x,y);
114 2 return;
115 2 }
116 1 if (key==14) {
117 2 if (affirm) { affirm=0;
C51 COMPILER V8.15 LCDANDKEY 08/10/2008 23:26:40 PAGE 3
118 3 Send_Data_LCM(0x00);
119 3 Set_LCM_XY(x,y);
120 3 return;
121 3 }
122 2 temp1=x-1;
123 2 temp2=y;
124 2 if (temp1==255) if (y==1) {temp2=0;temp1=15;}
125 2 else return;
126 2 Set_LCM_XY(temp1,temp2);
127 2 Send_Data_LCM(0x00);
128 2 Set_LCM_XY(temp1,temp2);
129 2 x=temp1;
130 2 y=temp2;
131 2 selectN=0;
132 2 return;
133 2 }
134 1 if (key==15) {
135 2 addchar();
136 2 return;
137 2
138 2
139 2 }
140 1 }
141 //------------------------------------------------------------------------------------
142 main(){
143 1 Init_LCM();
144 1 P1=0xf0;
145 1 Set_LCM_XY(15,1);
146 1 Send_Data_LCM(0x41);
147 1 Set_LCM_XY(0,0);
148 1 x=0;y=0;
149 1 while (1){
150 2 if (!getkey()) P1=0xf0;
151 2 chkkey(); //判断是否有键按下
152 2 if (getkey()) { //若有键按下
153 3 findkey(); //求键值
154 3 keyfun();
155 3 }
156 2 while (getkey()) {}; //Wait for Key UP
157 2 if (affirm) {
158 3 wait_count++;
159 3 if (wait_count==0x2fff) {wait_count=0;addchar(); }
160 3
161 3
162 3 }
163 2
164 2 } //while(1)
165 1
166 1
167 1 } //main()
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 721 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 8 3
IDATA SIZE = ---- ----
BIT SIZE = 2 1
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -