-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.c
97 lines (83 loc) · 2.3 KB
/
keys.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
/*
Author: W Pielage & E Helmond
Date: 13-9-2013
Revision: 2
keys.c:
Keyboard-driver for SpARM-board v1
pin-info:
PD1 - R2
PD2 - R3
PD3 - R4
PD6 - K1
PD7 - K2
PD8 - K3
PD9 - K4
PD11 - R1
To use the keyboard first initialize the ports:
KEYS_init();
After that, you can use polling to read a key with:
KEYS_read();
*/
#include "main.h"
#if KEYS
void KEYS_init(void)
/* Keys initialize
* In this function the ports are set for the keyboard.
* The rows are outputs and the columns are input.
*/
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitTypeDef gpio;
gpio.GPIO_OType = GPIO_OType_PP;
gpio.GPIO_PuPd = GPIO_PuPd_NOPULL;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
gpio.GPIO_Pin = KEY_ROW;
gpio.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(PKEY_ROW, &gpio);
gpio.GPIO_Pin = KEY_COL;
gpio.GPIO_Mode = GPIO_Mode_IN;
gpio.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(PKEY_COL, &gpio);
GPIO_ResetBits(PKEY_ROW, KEY_ROW);
}
unsigned int KEYS_read(void)
/* Keys read
* This function reads which key is pressed.
* It does so by making a row high and check if there is a connection with a column.
* If there is a connection the value is returned.
*/
{
unsigned int key = 0;
unsigned out = 0;
GPIO_ResetBits(PKEY_ROW, KEY_R1);
GPIO_SetBits(PKEY_ROW, KEY_R4);
key = KEYS_kolom();
if( key != 0 ) out= 12+key;
GPIO_ResetBits(PKEY_ROW, KEY_R4);
GPIO_SetBits(PKEY_ROW, KEY_R3);
key = KEYS_kolom();
if( key != 0 ) out = 8+key;
GPIO_ResetBits(PKEY_ROW, KEY_R3);
GPIO_SetBits(PKEY_ROW, KEY_R2);
key = KEYS_kolom();
if( key != 0 ) out = 4+key;
GPIO_ResetBits(PKEY_ROW, KEY_R2);
GPIO_SetBits(PKEY_ROW, KEY_R1);
key = KEYS_kolom();
if( key != 0 ) out =key;
return out;
}
unsigned int KEYS_kolom(void)
/* Keys Column
* In this function the columns are checked if there is a connection.
* If a column has a connection the return value is the column number.
*/
{
unsigned int key = 0;
if (GPIO_ReadInputDataBit(PKEY_COL, KEY_K1) == (uint8_t)Bit_SET) key = 1;
if (GPIO_ReadInputDataBit(PKEY_COL, KEY_K2) == (uint8_t)Bit_SET) key = 2;
if (GPIO_ReadInputDataBit(PKEY_COL, KEY_K3) == (uint8_t)Bit_SET) key = 3;
if (GPIO_ReadInputDataBit(PKEY_COL, KEY_K4) == (uint8_t)Bit_SET) key = 4;
return key;
}
#endif