-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeypad_2.c
More file actions
142 lines (126 loc) · 2.88 KB
/
keypad_2.c
File metadata and controls
142 lines (126 loc) · 2.88 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* @file keypad_2.c
* @author Alessandro Ferrante (github@alessandroferrante.net)
* @brief
* This code implements a 4x4 keyboard with asynchronous scanning using a hardware timer (TIM2).
* The columns are scanned by the timer that periodically triggers the reading of the rows.
* The use of interrupts makes the management of the keys more efficient than the synchronous version.
* @version 0.2
* @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);
}
volatile int current_column;
volatile char current_key;
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);
current_column = 0;
current_key = 255;
activate_col(0);
TIM_init(TIM2);
TIM_config_timebase(TIM2, 84, 10000);
TIM_enable_irq(TIM2, IRQ_UPDATE);
TIM_set(TIM2,0);
TIM_on(TIM2);
}
char keys[4][4] = { {'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
void TIM2_IRQHandler(void)
{
if (TIM_update_check(TIM2)) {
int r0, r1, r2, r3;
if (current_column < 4) {
// leggi la colonna corrente
r0 = GPIO_read(GPIOB, 10);
r1 = GPIO_read(GPIOA, 8);
r2 = GPIO_read(GPIOA, 9);
r3 = GPIO_read(GPIOC, 7);
if (r0 == 0) {
current_key = keys[current_column][0];
}
if (r1 == 0) {
current_key = keys[current_column][1];
}
if (r2 == 0) {
current_key = keys[current_column][2];
}
if (r3 == 0) {
current_key = keys[current_column][3];
}
// attiva la colonna successiva
++current_column;
if (current_column < 4)
activate_col(current_column);
else
activate_col(-1);
}
TIM_update_clear(TIM2);
}
}
char __keypad_read(void)
{
current_key = 255;
activate_col(0);
current_column = 0;
while (current_column < 4) ;
//printf("%d %d\n", current_column, current_key);
return current_key;
}
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);
}
}