📄 keypad.lst
字号:
C51 COMPILER V6.21 KEYPAD 01/23/2002 18:10:45 PAGE 1
C51 COMPILER V6.21, COMPILATION OF MODULE KEYPAD
OBJECT MODULE PLACED IN KEYPAD.OBJ
COMPILER INVOKED BY: C:\KEIL\C51\BIN\C51.EXE KEYPAD.C OPTIMIZE(6,SPEED) BROWSE DEBUG OBJECTEXTEND
stmt level source
1 /*------------------------------------------------------------------*-
2
3 Keypad.C (v1.00)
4
5 ------------------------------------------------------------------
6
7 Simple library, for a switch-based "keypad".
8
9 COPYRIGHT
10 ---------
11
12 This code is associated with the book:
13
14 EMBEDDED C by Michael J. Pont
15 [Pearson Education, 2002: ISBN: 0-201-79523-X].
16
17 This code is copyright (c) 2001 by Michael J. Pont.
18
19 See book for copyright details and other information.
20
21 -*------------------------------------------------------------------*/
22
23 #include "Main.h"
24 #include "Port.h"
25
26 #include "Keypad.h"
27
28 // ------ Private function prototypes ------------------------------
29
30 bit KEYPAD_Scan(char* const);
31
32 // ------ Private constants ----------------------------------------
33
34 #define KEYPAD_RECV_BUFFER_LENGTH 6
35
36 // Any valid character will do - must not match anything on keypad
37 #define KEYPAD_NO_NEW_DATA (char) '-'
38
39 // ------ Private variables ----------------------------------------
40
41 static char KEYPAD_recv_buffer[KEYPAD_RECV_BUFFER_LENGTH+1];
42
43 static tByte KEYPAD_in_read_index; // Data in buffer that has been read
44 static tByte KEYPAD_in_waiting_index; // Data in buffer not yet read
45
46 static char Last_valid_key_G = KEYPAD_NO_NEW_DATA;
47
48 static data char Old_key_G;
49
50 /*------------------------------------------------------------------*-
51
52 KEYPAD_Init()
53
54 Init the keypad.
55
C51 COMPILER V6.21 KEYPAD 01/23/2002 18:10:45 PAGE 2
56 -*------------------------------------------------------------------*/
57 void KEYPAD_Init(void)
58 {
59 1 KEYPAD_in_read_index = 0;
60 1 KEYPAD_in_waiting_index = 0;
61 1 }
62
63 /*------------------------------------------------------------------*-
64
65 KEYPAD_Update()
66
67 The main 'update' function for the keypad library.
68
69 Must call this function approx every 50 - 200 ms.
70
71 -*------------------------------------------------------------------*/
72 void KEYPAD_Update(void)
73 {
74 1 char Key;
75 1
76 1 // Scan keypad here...
77 1 if (KEYPAD_Scan(&Key) == 0)
78 1 {
79 2 // No new key data - just return
80 2 return;
81 2 }
82 1
83 1 // Want to read into index 0, if old data has been read
84 1 // (simple ~circular buffer)
85 1 if (KEYPAD_in_waiting_index == KEYPAD_in_read_index)
86 1 {
87 2 KEYPAD_in_waiting_index = 0;
88 2 KEYPAD_in_read_index = 0;
89 2 }
90 1
91 1 // Load keypad data into buffer
92 1 KEYPAD_recv_buffer[KEYPAD_in_waiting_index] = Key;
93 1
94 1 if (KEYPAD_in_waiting_index < KEYPAD_RECV_BUFFER_LENGTH)
95 1 {
96 2 // Increment without overflowing buffer
97 2 KEYPAD_in_waiting_index++;
98 2 }
99 1 }
100
101
102 /*------------------------------------------------------------------*-
103
104 KEYPAD_Get_Char_From_Buffer()
105
106 The Update function copies data into the keypad buffer.
107 This function extracts data from the buffer.
108
109 -*------------------------------------------------------------------*/
110
111 bit KEYPAD_Get_Data_From_Buffer(char* const pKey)
112 {
113 1 // If there are new data in the buffer
114 1 if (KEYPAD_in_read_index < KEYPAD_in_waiting_index)
115 1 {
116 2 *pKey = KEYPAD_recv_buffer[KEYPAD_in_read_index];
117 2
C51 COMPILER V6.21 KEYPAD 01/23/2002 18:10:45 PAGE 3
118 2 KEYPAD_in_read_index++;
119 2
120 2 return 1;
121 2 }
122 1
123 1 return 0;
124 1 }
125
126
127 /*------------------------------------------------------------------*-
128
129 KEYPAD_Clear_Buffer()
130
131 -*------------------------------------------------------------------*/
132 void KEYPAD_Clear_Buffer(void)
133 {
134 1 KEYPAD_in_waiting_index = 0;
135 1 KEYPAD_in_read_index = 0;
136 1 }
137
138 /*------------------------------------------------------------------*-
139
140 KEYPAD_Scan()
141
142 This function is called from scheduled keypad function.
143
144 Must be edited as required to match your key labels.
145
146 Adapt as required!
147
148 -*------------------------------------------------------------------*/
149 bit KEYPAD_Scan(char* const pKey)
150 {
151 1 char Key = KEYPAD_NO_NEW_DATA;
152 1
153 1 if (K0 == 0) { Key = '0'; }
154 1 if (K1 == 0) { Key = '1'; }
155 1 if (K2 == 0) { Key = '2'; }
156 1 if (K3 == 0) { Key = '3'; }
157 1 if (K4 == 0) { Key = '4'; }
158 1 if (K5 == 0) { Key = '5'; }
159 1 if (K6 == 0) { Key = '6'; }
160 1 if (K7 == 0) { Key = '7'; }
161 1
162 1 if (Key == KEYPAD_NO_NEW_DATA)
163 1 {
164 2 // No key pressed
165 2 Old_key_G = KEYPAD_NO_NEW_DATA;
166 2 Last_valid_key_G = KEYPAD_NO_NEW_DATA;
167 2
168 2 return 0; // No new data
169 2 }
170 1
171 1 // A key has been pressed: debounce by checking twice
172 1 if (Key == Old_key_G)
173 1 {
174 2 // A valid (debounced) key press has been detected
175 2
176 2 // Must be a new key to be valid - no 'auto repeat'
177 2 if (Key != Last_valid_key_G)
178 2 {
179 3 // New key!
C51 COMPILER V6.21 KEYPAD 01/23/2002 18:10:45 PAGE 4
180 3 *pKey = Key;
181 3 Last_valid_key_G = Key;
182 3
183 3 return 1;
184 3 }
185 2 }
186 1
187 1 // No new data
188 1 Old_key_G = Key;
189 1 return 0;
190 1 }
191
192 /*------------------------------------------------------------------*-
193 ---- END OF FILE -------------------------------------------------
194 -*------------------------------------------------------------------*/
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 145 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 11 1
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -