PropWare
3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Simple-Libraries
Learn
Examples
Devices
Interface
Keypad 4x4 Add Subtract.c
1
/*
2
Keypad 4x4 Add Subtract.c
3
Keypad addition and subtraction using the A button for add, and the B button
4
for subtract.
5
6
IMPORTANT: In the values array, the 'A' key was reassigned the value '+',
7
the B key was reassigned '-', and the '#' key was reassigned '='.
8
*/
9
10
#include "
simpletools.h
"
// Libraries simpletools & keypad
11
#include "
keypad.h
"
12
13
int
key,
x
, y, z, op;
// Application variables
14
15
int
rows[4] = {7, 6, 5, 4};
// Row I/O pins (top to bottom)
16
int
cols[4] = {3, 2, 1, 0};
// Column I/O pins (left to right)
17
18
int
values[16] = { 1, 2, 3,
'+'
,
// Values for each key in the 4x4
19
4, 5, 6,
'-'
,
// Note that A is now +, B is now
20
7, 8, 9,
'C'
,
// -, nad # is now =
21
'*'
, 0,
'='
,
'D'
};
22
23
int
main
()
// Main function
24
{
25
keypad_setup
(4, 4, rows, cols, values);
// Setup dimensions, keypad arrays
26
27
while
(1)
// Main loop
28
{
29
print
(
"Type number, (A or B), "
);
// User prompt
30
print
(
"number #\r"
);
31
32
x
=
keypad_getNumber
();
// First keypad number -> x
33
34
op =
keypad_getNumberEndKey
();
// Get operator that ended number
35
36
y =
keypad_getNumber
();
// Second keypad number -> x
37
38
switch
(op)
// Operation based on op var
39
{
40
case
'+'
:
// if +, add
41
{
42
z =
x
+ y;
43
break
;
44
}
45
case
'-'
:
// if -, subtract
46
{
47
z =
x
- y;
48
break
;
49
}
50
}
51
print
(
"%d\r"
, z);
// Display the result
52
}
53
}
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
simpletools.h
This library provides convenient functions for a variety of microcontroller I/O, timing,...
keypad_getNumber
int keypad_getNumber(void)
Get a number from the keypad. The number will be retured as soon as a non-numeric key is pressed and ...
Definition:
getNumber.c:18
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
keypad_getNumberEndKey
int keypad_getNumberEndKey(void)
Get the key that terminated number received by keypad_getNumber.
Definition:
getNumber.c:49
x
int x
Definition:
07 Box and Lines.c:13
Generated on Fri May 20 2022 16:48:04 for PropWare by
1.8.17