-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKEYPAD_program.c
104 lines (62 loc) · 2.4 KB
/
KEYPAD_program.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Keypad_program.c
*
* Created on: Oct 7, 2022
* Author: MSAEED99
*/
#include "STD_TYPES.h"
#include "BIT_MATH.h"
// MCAL
#include "DIO_interface.h"
// HAL
#include "KEYPAD_config.h"
#include "KEYPAD_interface.h"
#include "KEYPAD_private.h"
void KEYPAD_voidPinInit(void)
{
// Set keypad columns pins as OUTPUT
DIO_voidSetPinDirection(KEYPAD_PORT, KEY_PAD_COLUMN0_PIN, DIO_u8PIN_OUTPUT);
DIO_voidSetPinDirection(KEYPAD_PORT, KEY_PAD_COLUMN1_PIN, DIO_u8PIN_OUTPUT);
DIO_voidSetPinDirection(KEYPAD_PORT, KEY_PAD_COLUMN2_PIN, DIO_u8PIN_OUTPUT);
DIO_voidSetPinDirection(KEYPAD_PORT, KEY_PAD_COLUMN3_PIN, DIO_u8PIN_OUTPUT);
// Set keypad rows pins as INPUT
DIO_voidSetPinDirection(KEYPAD_PORT, KEY_PAD_ROW0_PIN, DIO_u8PIN_INPUT);
DIO_voidSetPinDirection(KEYPAD_PORT, KEY_PAD_ROW1_PIN, DIO_u8PIN_INPUT);
DIO_voidSetPinDirection(KEYPAD_PORT, KEY_PAD_ROW2_PIN, DIO_u8PIN_INPUT);
DIO_voidSetPinDirection(KEYPAD_PORT, KEY_PAD_ROW3_PIN, DIO_u8PIN_INPUT);
/* Set keypad port value to HIGH (to enable pull-up in rows pins
and output HIGH signal on columns pins) */
DIO_voidSetPortValue(KEYPAD_PORT, DIO_u8PORT_HIGH);
}
u8 KEYPAD_u8GetPressedKey(void)
{
u8 local_u8PressedKey = KEYPAD_DEFAULT_KEY;
// Keypad characters array
u8 KeypadArr [KEYPAD_ROWS_NUM][KEYPAD_COLS_NUM] = KEYPAD_ARR_VAL;
// Rows and columns arrays
u8 ColumnsArr[KEYPAD_COLS_NUM] = {KEY_PAD_COLUMN0_PIN, KEY_PAD_COLUMN1_PIN, KEY_PAD_COLUMN2_PIN, KEY_PAD_COLUMN3_PIN};
u8 RowsArr[KEYPAD_ROWS_NUM] = {KEY_PAD_ROW0_PIN, KEY_PAD_ROW1_PIN, KEY_PAD_ROW2_PIN, KEY_PAD_ROW3_PIN};
// Loop over each column
for(u8 i = 0; i < KEYPAD_COLS_NUM; i++)
{
// Activate current column (Set to PIN_LOW)
DIO_voidSetPinValue(KEYPAD_PORT, ColumnsArr[i], DIO_u8PIN_LOW);
// Loop over rows to get the pressed key
for(u8 j = 0; j < KEYPAD_ROWS_NUM; j++)
{
if(DIO_u8GetPinValue(KEYPAD_PORT, RowsArr[j]) == DIO_u8PIN_LOW)
{
local_u8PressedKey = KeypadArr[j][i];
// Polling till switch released (to avoid bouncing)
while(DIO_u8GetPinValue(KEYPAD_PORT, RowsArr[j]) == DIO_u8PIN_LOW)
{
local_u8PressedKey = KeypadArr[j][i];
}
return local_u8PressedKey;
}
}
// Activate current column (Set to PIN_LOW)
DIO_voidSetPinValue(KEYPAD_PORT, ColumnsArr[i], DIO_u8PIN_HIGH);
}
return local_u8PressedKey;
}