期末大作业-增加矩阵键盘
This commit is contained in:
22
FinalHomework/Core/Inc/MatrixKey.h
Normal file
22
FinalHomework/Core/Inc/MatrixKey.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef __MATRIXKEY_H
|
||||
#define __MATRIXKEY_H
|
||||
|
||||
|
||||
#include<stdint.h>
|
||||
enum {State0=0, State1, State2};
|
||||
#define NO_KEY 0xFF
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
uint8_t MatrixKeyScan(void);
|
||||
extern uint8_t const KeyTable[16];
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -69,12 +69,16 @@ void Error_Handler(void);
|
||||
#define OE_GPIO_Port GPIOC
|
||||
#define SegLedData_Pin GPIO_PIN_0
|
||||
#define SegLedData_GPIO_Port GPIOA
|
||||
#define KeyLine_Pin GPIO_PIN_0
|
||||
#define KeyLine_GPIO_Port GPIOB
|
||||
#define LCD_RST_Pin GPIO_PIN_11
|
||||
#define LCD_RST_GPIO_Port GPIOB
|
||||
#define LCD_CS_Pin GPIO_PIN_12
|
||||
#define LCD_CS_GPIO_Port GPIOB
|
||||
#define LCD_RS_Pin GPIO_PIN_14
|
||||
#define LCD_RS_GPIO_Port GPIOB
|
||||
#define KeyRow_Pin GPIO_PIN_4
|
||||
#define KeyRow_GPIO_Port GPIOB
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
|
||||
|
||||
95
FinalHomework/Core/Src/MatrixKey.c
Normal file
95
FinalHomework/Core/Src/MatrixKey.c
Normal file
@@ -0,0 +1,95 @@
|
||||
#include "MatrixKey.h"
|
||||
#include "main.h"
|
||||
|
||||
// EDB7 xx EDB7
|
||||
uint8_t const KeyTable[16] = {
|
||||
0xEE, 0xDE, 0xBE, 0x7E,
|
||||
0xED, 0xDD, 0xBD, 0x7D,
|
||||
0xEB, 0xDB, 0xBB, 0x7B,
|
||||
0xE7, 0xD7, 0xB7, 0x77,
|
||||
};
|
||||
|
||||
void LineIn_RowOut(void) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
/*Configure GPIO pin : PtPin */
|
||||
/*Configure GPIO pins : PCPin PC1 PC2 PC3 */
|
||||
GPIO_InitStruct.Pin = KeyLine_Pin | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
HAL_GPIO_Init(KeyLine_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = KeyRow_Pin | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(KeyRow_GPIO_Port, &GPIO_InitStruct);
|
||||
}
|
||||
|
||||
void LineOut_RowIn(void) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
/*Configure GPIO pin : PtPin */
|
||||
/*Configure GPIO pins : PCPin PC1 PC2 PC3 */
|
||||
GPIO_InitStruct.Pin = KeyLine_Pin | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(KeyLine_GPIO_Port, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = KeyRow_Pin | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
HAL_GPIO_Init(KeyRow_GPIO_Port, &GPIO_InitStruct);
|
||||
}
|
||||
|
||||
uint8_t MatrixKeyScan(void) {
|
||||
static uint8_t Keystate = State0;
|
||||
static uint8_t KeyLine, KeyRow;
|
||||
static uint16_t KeyOld;
|
||||
|
||||
uint8_t KeyValue = NO_KEY;
|
||||
|
||||
LineOut_RowIn();
|
||||
/*Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(KeyLine_GPIO_Port, KeyLine_Pin | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, GPIO_PIN_RESET); // 这四个按位或操作后是0x0F
|
||||
HAL_GPIO_WritePin(KeyLine_GPIO_Port, KeyLine_Pin | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, GPIO_PIN_RESET); // 重复三次使更稳定
|
||||
HAL_GPIO_WritePin(KeyLine_GPIO_Port, KeyLine_Pin | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, GPIO_PIN_RESET); // 刚写入后不能直接读要等稳定了再读
|
||||
KeyRow = KeyRow_GPIO_Port->IDR & 0x00F0;
|
||||
|
||||
LineIn_RowOut();
|
||||
HAL_GPIO_WritePin(KeyRow_GPIO_Port, KeyRow_Pin | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(KeyRow_GPIO_Port, KeyRow_Pin | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7, GPIO_PIN_RESET); // 这里也是重复三次
|
||||
HAL_GPIO_WritePin(KeyRow_GPIO_Port, KeyRow_Pin | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7, GPIO_PIN_RESET);
|
||||
KeyLine = KeyLine_GPIO_Port->IDR & 0x000F;
|
||||
|
||||
switch (Keystate) {
|
||||
case State0:
|
||||
KeyOld = KeyLine | KeyRow;
|
||||
if (KeyOld != NO_KEY) {
|
||||
Keystate = State1;
|
||||
} else {
|
||||
Keystate = State0;
|
||||
}
|
||||
break;
|
||||
|
||||
case State1:
|
||||
if ((KeyLine | KeyRow) == KeyOld) {
|
||||
Keystate = State2;
|
||||
} else {
|
||||
Keystate = State0;
|
||||
}
|
||||
break;
|
||||
|
||||
case State2:
|
||||
if ((KeyLine | KeyRow) == NO_KEY) { // 键已释放
|
||||
Keystate = State0;
|
||||
KeyValue = KeyOld;
|
||||
} else {
|
||||
Keystate = State2;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return KeyValue;
|
||||
}
|
||||
@@ -79,6 +79,14 @@ void MX_GPIO_Init(void)
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : PBPin PB1 PB2 PB3
|
||||
PBPin PB5 PB6 PB7 */
|
||||
GPIO_InitStruct.Pin = KeyLine_Pin|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
|
||||
|KeyRow_Pin|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : PBPin PBPin PBPin */
|
||||
GPIO_InitStruct.Pin = LCD_RST_Pin|LCD_CS_Pin|LCD_RS_Pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
@@ -86,6 +94,30 @@ void MX_GPIO_Init(void)
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : PC6 */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_6;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : PC7 PC8 PC9 */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pin : PA8 */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_8;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/*Configure GPIO pins : PA9 PA10 PA11 */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "lcd.h"
|
||||
#include "test.h"
|
||||
#include "SegLed.h"
|
||||
#include "MatrixKey.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -82,7 +83,7 @@ int main(void)
|
||||
{
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
uint8_t KeyValue = 0;
|
||||
/* USER CODE END 1 */
|
||||
|
||||
/* MCU Configuration--------------------------------------------------------*/
|
||||
@@ -118,8 +119,17 @@ int main(void)
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
if (stTime.bTenMilIsOk) {
|
||||
stTime.bTenMilIsOk = 0;
|
||||
if (sSysTickTimer.bTenMilSecOk) {
|
||||
sSysTickTimer.bTenMilSecOk = 0;
|
||||
KeyValue = MatrixKeyScan();
|
||||
if (KeyValue != NO_KEY) {
|
||||
for (int i = 0; i < 16; i++) {
|
||||
if (KeyValue == KeyTable[i]) {
|
||||
tempValue = i;
|
||||
}
|
||||
}
|
||||
DispToBuff(tempValue);
|
||||
}
|
||||
}
|
||||
if (stTime.bSecondIsOk) {
|
||||
stTime.bSecondIsOk = 0;
|
||||
@@ -141,7 +151,7 @@ int main(void)
|
||||
// Test_Triangle();
|
||||
// English_Font_test();
|
||||
// Chinese_Font_test();
|
||||
Pic_test();
|
||||
// Pic_test();
|
||||
// Rotate_Test();
|
||||
if (sSysTickTimer.bTimeOk) {
|
||||
sSysTickTimer.bTimeOk = 0;
|
||||
|
||||
Reference in New Issue
Block a user