-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeypad_1.c
More file actions
116 lines (105 loc) · 2.21 KB
/
keypad_1.c
File metadata and controls
116 lines (105 loc) · 2.21 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
/**
* @file keypad_1.c
* @author Alessandro Ferrante (github@alessandroferrante.net)
* @brief
* This code implements a 4x4 keyboard with synchronous scanning.
* Each column is activated one at a time, and for each activation
* all rows are read to determine which key was pressed.
* Scanning is managed synchronously via polling.
* @version 0.1
* @date 2024-12-05
*
* @copyright Copyright (c) 2024
*
*/
#include "stm32_unict_lib.h"
#include <stdio.h>
void activate_col(int col)
{
GPIO_write(GPIOA, 10, col != 0);
GPIO_write(GPIOB, 3, col != 1);
GPIO_write(GPIOB, 5, col != 2);
GPIO_write(GPIOB, 4, col != 3);
}
void keypad_init(void)
{
GPIO_init(GPIOA);
GPIO_init(GPIOB);
GPIO_init(GPIOC);
// cols
// PA10, PB3, PB5, PB4
GPIO_config_output(GPIOA, 10);
GPIO_OPEN_DRAIN(GPIOA, 10);
GPIO_config_output(GPIOB, 3);
GPIO_OPEN_DRAIN(GPIOB, 3);
GPIO_config_output(GPIOB, 5);
GPIO_OPEN_DRAIN(GPIOB, 5);
GPIO_config_output(GPIOB, 4);
GPIO_OPEN_DRAIN(GPIOB, 4);
// rows
// PB10, PA8, PA9, PC7
GPIO_config_input(GPIOB, 10);
GPIO_PULL_UP(GPIOB, 10);
GPIO_config_input(GPIOA, 8);
GPIO_PULL_UP(GPIOA, 8);
GPIO_config_input(GPIOA, 9);
GPIO_PULL_UP(GPIOA, 9);
GPIO_config_input(GPIOC, 7);
GPIO_PULL_UP(GPIOC, 7);
activate_col(-1);
}
char keys[4][4] = { {'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
char __keypad_read(void)
{
int col;
int r0, r1, r2, r3;
for (col = 0; col < 4;col++) {
activate_col(col);
delay_ms(1);
r0 = GPIO_read(GPIOB, 10);
r1 = GPIO_read(GPIOA, 8);
r2 = GPIO_read(GPIOA, 9);
r3 = GPIO_read(GPIOC, 7);
//printf("%d: %d %d %d %d\n", col, r0, r1, r2, r3);
if (r0 == 0) {
return keys[col][0];
}
if (r1 == 0) {
return keys[col][1];
}
if (r2 == 0) {
return keys[col][2];
}
if (r3 == 0) {
return keys[col][3];
}
}
//printf("\n");
return 255;
}
char keypad_read(void)
{
char c;
do {
c = __keypad_read();
} while (c != 255); // attesa rilascio tasto
do {
c = __keypad_read();
} while (c == 255); // attesa tasto premuto
return c;
}
int main(void)
{
ClockConfig();
CONSOLE_init();
printf("Starting\n");
keypad_init();
for (;;) {
char c = keypad_read();
printf("Key = %c\n",c);
}
}