PropWare
3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Simple-Libraries
Learn
Examples
Devices
Interface
Keypad 4x4 Read Simultaneous Keys.c
1
/*
2
Keypad 4x4 Read Simultaneous Keys.c
3
Demonstrates how to read keys when more than one might be
4
pressed.
5
6
Caution, 2 keys is always correct, but 3 keys is not
7
always dependable. Many combinations of up to 4 keys work
8
fine, but certain combinations can result in ghosting on
9
keypads that do not have built-in diodes to prevent that.
10
11
Ghosting happens when two keys on the same row are pressed
12
and a third key on the same column with one the first two is
13
pressed. For example, pressing 1, 2, and 7 will result in
14
1, 2, 7, and 8 being returned
15
*/
16
17
#include "
simpletools.h
"
// Libraries simpletools & keypad
18
#include "
keypad.h
"
19
20
int
rows[4] = {7, 6, 5, 4};
// Row I/O pins (top to bottom)
21
int
cols[4] = {3, 2, 1, 0};
// Column I/O pins (left to right)
22
23
int
values[16] = { 1, 2, 3,
'A'
,
// Values for each key in the 4x4
24
4, 5, 6,
'B'
,
25
7, 8, 9,
'C'
,
26
'*'
, 0,
'#'
,
'D'
};
27
28
int
main
()
// Main function
29
{
30
keypad_setup
(4, 4, rows, cols, values);
// Setup dimensions, keypad arrays
31
32
while
(1)
// Main loop
33
{
34
int
key =
keypad_read
();
// Get pressed key (or -1 for none)
35
36
if
(key <= 9)
// Display key value
37
print
(
"key = %d\r"
, key);
// If <= 9, dispaly as decimal
38
else
39
print
(
"key = %c\r"
, key);
// Otherwise, display as character
40
41
while
(key != -1)
// Loop getting more pressed keys
42
{
43
key =
keypad_readFrom
(key);
// Use readFrom previous key
44
if
(key <= 9)
// Process the same way as read
45
print
(
"key = %d\r"
, key);
46
else
47
print
(
"key = %c\r"
, key);
48
}
49
50
// Extra carriage return and delay for the terminal.
51
print
(
"\r"
);
52
pause
(500);
53
}
54
}
keypad_setup
void keypad_setup(int rowCount, int columnCount, int *rowPinCons, int *columnPinCons, int *buttonValues)
Set up the row and column dimensions, pins, and key values.
Definition:
keypad.c:24
pause
void pause(int time)
Delay cog from moving on to the next statement for a certain length of time.
Definition:
libws2812.c:125
simpletools.h
This library provides convenient functions for a variety of microcontroller I/O, timing,...
keypad_read
int keypad_read(void)
Returns the first key pressed, or -1 if no key is pressed.
Definition:
keypad.c:42
keypad_readFrom
int keypad_readFrom(int button)
If more than one key might be pressed and held at one time, use this keyapd_readFrom function to get ...
Definition:
keypad.c:48
print
int print(const char *format,...) __attribute__((format(printf
Print format "..." args to the default simple terminal device. The output is limited to 256 bytes.
keypad.h
Scans matrix keypad and returns a unique value for each key that is pressed. For an example circuit a...
main
int main(void)
Definition:
GraphicsTest.c:20
Generated on Fri May 20 2022 16:48:04 for PropWare by
1.8.17