阵列按键

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
uint8_t Key_GetNum(void) {
uint8_t keyNum = 0;

/*
在Key_init()中
设置所有行为高电平,列为输入下拉模式(IPD)
GPIO_SetBits(GPIOB, Key_ROW1 | Key_ROW2 | Key_ROW3 | Key_ROW4);
GPIO_ResetBits(GPIOA, Key_Pin_A_All);
GPIO_ResetBits(GPIOC, Key_Pin_C_All);
*/
GPIO_SetBits(GPIOB, Key_ROW4);
GPIO_ResetBits(GPIOB, Key_ROW1);
if (GPIO_ReadInputDataBit(GPIOA, Key_COL1) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOA, Key_COL1) == Bit_RESET);
keyNum = 1;
} else if (GPIO_ReadInputDataBit(GPIOA, Key_COL2) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOA, Key_COL2) == Bit_RESET);
keyNum = 5;
} else if (GPIO_ReadInputDataBit(GPIOC, Key_COL3) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOC, Key_COL3) == Bit_RESET);
keyNum = 9;
} else if (GPIO_ReadInputDataBit(GPIOC, Key_COL4) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOC, Key_COL4) == Bit_RESET);
keyNum = 13;
}
// 等待一段时间,避免检测到多个按键同时按下
Delay_ms(10);

GPIO_SetBits(GPIOB, Key_ROW1);
GPIO_ResetBits(GPIOB, Key_ROW2);
if (GPIO_ReadInputDataBit(GPIOA, Key_COL1) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOA, Key_COL1) == Bit_RESET);
keyNum = 2;
} else if (GPIO_ReadInputDataBit(GPIOA, Key_COL2) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOA, Key_COL2) == Bit_RESET);
keyNum = 6;
} else if (GPIO_ReadInputDataBit(GPIOC, Key_COL3) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOC, Key_COL3) == Bit_RESET);
keyNum = 10;
} else if (GPIO_ReadInputDataBit(GPIOC, Key_COL4) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOC, Key_COL4) == Bit_RESET);
keyNum = 14;
}
Delay_ms(10);

GPIO_SetBits(GPIOB, Key_ROW2);
GPIO_ResetBits(GPIOB, Key_ROW3);
if (GPIO_ReadInputDataBit(GPIOA, Key_COL1) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOA, Key_COL1) == Bit_RESET);
keyNum = 3;
} else if (GPIO_ReadInputDataBit(GPIOA, Key_COL2) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOA, Key_COL2) == Bit_RESET);
keyNum = 7;
} else if (GPIO_ReadInputDataBit(GPIOC, Key_COL3) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOC, Key_COL3) == Bit_RESET);
keyNum = 11;
} else if (GPIO_ReadInputDataBit(GPIOC, Key_COL4) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOC, Key_COL4) == Bit_RESET);
keyNum = 15;
}
Delay_ms(10);

GPIO_SetBits(GPIOB, Key_ROW3);
GPIO_ResetBits(GPIOB, Key_ROW4);
if (GPIO_ReadInputDataBit(GPIOA, Key_COL1) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOA, Key_COL1) == Bit_RESET);
keyNum = 4;
} else if (GPIO_ReadInputDataBit(GPIOA, Key_COL2) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOA, Key_COL2) == Bit_RESET);
keyNum = 8;
} else if (GPIO_ReadInputDataBit(GPIOC, Key_COL3) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOC, Key_COL3) == Bit_RESET);
keyNum = 12;
} else if (GPIO_ReadInputDataBit(GPIOC, Key_COL4) == Bit_RESET && keyNum == 0) {Delay_ms(10);
while(GPIO_ReadInputDataBit(GPIOC, Key_COL4) == Bit_RESET);
keyNum = 16;
}
Delay_ms(10);

return keyNum;
}