From 618ae827f1396b5a2e63f16bc9f97c96b50155fb Mon Sep 17 00:00:00 2001 From: 423A35C7 <609514299@qq.com> Date: Tue, 7 Jan 2025 13:14:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=9F=E6=9C=AB=E5=A4=A7=E4=BD=9C=E4=B8=9A-?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=9F=A9=E9=98=B5=E9=94=AE=E7=9B=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FinalHomework/Core/Inc/MatrixKey.h | 22 +++++ FinalHomework/Core/Inc/main.h | 4 + FinalHomework/Core/Src/MatrixKey.c | 95 ++++++++++++++++++ FinalHomework/Core/Src/gpio.c | 32 ++++++ FinalHomework/Core/Src/main.c | 18 +++- FinalHomework/FinalHomework.ioc | 102 +++++++++++++++++--- FinalHomework/MDK-ARM/FinalHomework.uvprojx | 30 ++++-- 7 files changed, 277 insertions(+), 26 deletions(-) create mode 100644 FinalHomework/Core/Inc/MatrixKey.h create mode 100644 FinalHomework/Core/Src/MatrixKey.c diff --git a/FinalHomework/Core/Inc/MatrixKey.h b/FinalHomework/Core/Inc/MatrixKey.h new file mode 100644 index 0000000..2d0adff --- /dev/null +++ b/FinalHomework/Core/Inc/MatrixKey.h @@ -0,0 +1,22 @@ +#ifndef __MATRIXKEY_H +#define __MATRIXKEY_H + + +#include +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 \ No newline at end of file diff --git a/FinalHomework/Core/Inc/main.h b/FinalHomework/Core/Inc/main.h index aa5491c..1dceb7e 100644 --- a/FinalHomework/Core/Inc/main.h +++ b/FinalHomework/Core/Inc/main.h @@ -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 */ diff --git a/FinalHomework/Core/Src/MatrixKey.c b/FinalHomework/Core/Src/MatrixKey.c new file mode 100644 index 0000000..9dbee05 --- /dev/null +++ b/FinalHomework/Core/Src/MatrixKey.c @@ -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; +} \ No newline at end of file diff --git a/FinalHomework/Core/Src/gpio.c b/FinalHomework/Core/Src/gpio.c index 9e8c3e8..dd883f7 100644 --- a/FinalHomework/Core/Src/gpio.c +++ b/FinalHomework/Core/Src/gpio.c @@ -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 */ diff --git a/FinalHomework/Core/Src/main.c b/FinalHomework/Core/Src/main.c index 277d373..d30e0d6 100644 --- a/FinalHomework/Core/Src/main.c +++ b/FinalHomework/Core/Src/main.c @@ -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; diff --git a/FinalHomework/FinalHomework.ioc b/FinalHomework/FinalHomework.ioc index 57328f3..068cd4f 100644 --- a/FinalHomework/FinalHomework.ioc +++ b/FinalHomework/FinalHomework.ioc @@ -22,25 +22,41 @@ Mcu.Pin11=PA4 Mcu.Pin12=PA5 Mcu.Pin13=PA6 Mcu.Pin14=PA7 -Mcu.Pin15=PB11 -Mcu.Pin16=PB12 -Mcu.Pin17=PB13 -Mcu.Pin18=PB14 -Mcu.Pin19=PB15 +Mcu.Pin15=PB0 +Mcu.Pin16=PB1 +Mcu.Pin17=PB2 +Mcu.Pin18=PB11 +Mcu.Pin19=PB12 Mcu.Pin2=PF1-OSC_OUT -Mcu.Pin20=PA13 -Mcu.Pin21=PA14 -Mcu.Pin22=VP_SYS_VS_Systick -Mcu.Pin23=VP_SYS_VS_DBSignals -Mcu.Pin24=VP_TIM3_VS_ClockSourceINT +Mcu.Pin20=PB13 +Mcu.Pin21=PB14 +Mcu.Pin22=PB15 +Mcu.Pin23=PC6 +Mcu.Pin24=PC7 +Mcu.Pin25=PC8 +Mcu.Pin26=PC9 +Mcu.Pin27=PA8 +Mcu.Pin28=PA9 +Mcu.Pin29=PA10 Mcu.Pin3=PC0 +Mcu.Pin30=PA11 +Mcu.Pin31=PA13 +Mcu.Pin32=PA14 +Mcu.Pin33=PB3 +Mcu.Pin34=PB4 +Mcu.Pin35=PB5 +Mcu.Pin36=PB6 +Mcu.Pin37=PB7 +Mcu.Pin38=VP_SYS_VS_Systick +Mcu.Pin39=VP_SYS_VS_DBSignals Mcu.Pin4=PC1 +Mcu.Pin40=VP_TIM3_VS_ClockSourceINT Mcu.Pin5=PC2 Mcu.Pin6=PC3 Mcu.Pin7=PA0 Mcu.Pin8=PA1 Mcu.Pin9=PA2 -Mcu.PinsNb=25 +Mcu.PinsNb=41 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32G473RCTx @@ -69,6 +85,14 @@ PA1.GPIO_PuPd=GPIO_PULLUP PA1.Locked=true PA1.PinState=GPIO_PIN_SET PA1.Signal=GPIO_Output +PA10.GPIOParameters=GPIO_PuPd +PA10.GPIO_PuPd=GPIO_PULLUP +PA10.Locked=true +PA10.Signal=GPIO_Input +PA11.GPIOParameters=GPIO_PuPd +PA11.GPIO_PuPd=GPIO_PULLUP +PA11.Locked=true +PA11.Signal=GPIO_Input PA13.Mode=Serial_Wire PA13.Signal=SYS_JTMS-SWDIO PA14.Mode=Serial_Wire @@ -103,6 +127,21 @@ PA7.GPIO_PuPd=GPIO_PULLUP PA7.Locked=true PA7.PinState=GPIO_PIN_SET PA7.Signal=GPIO_Output +PA8.Locked=true +PA8.Signal=GPIO_Input +PA9.GPIOParameters=GPIO_PuPd +PA9.GPIO_PuPd=GPIO_PULLUP +PA9.Locked=true +PA9.Signal=GPIO_Input +PB0.GPIOParameters=GPIO_PuPd,GPIO_Label +PB0.GPIO_Label=KeyLine +PB0.GPIO_PuPd=GPIO_PULLUP +PB0.Locked=true +PB0.Signal=GPIO_Input +PB1.GPIOParameters=GPIO_PuPd +PB1.GPIO_PuPd=GPIO_PULLUP +PB1.Locked=true +PB1.Signal=GPIO_Input PB11.GPIOParameters=GPIO_Speed,GPIO_PuPd,GPIO_Label PB11.GPIO_Label=LCD_RST PB11.GPIO_PuPd=GPIO_PULLUP @@ -125,6 +164,31 @@ PB14.Locked=true PB14.Signal=GPIO_Output PB15.Mode=TX_Only_Simplex_Unidirect_Master PB15.Signal=SPI2_MOSI +PB2.GPIOParameters=GPIO_PuPd +PB2.GPIO_PuPd=GPIO_PULLUP +PB2.Locked=true +PB2.Signal=GPIO_Input +PB3.GPIOParameters=GPIO_PuPd +PB3.GPIO_PuPd=GPIO_PULLUP +PB3.Locked=true +PB3.Signal=GPIO_Input +PB4.GPIOParameters=GPIO_PuPd,GPIO_Label +PB4.GPIO_Label=KeyRow +PB4.GPIO_PuPd=GPIO_PULLUP +PB4.Locked=true +PB4.Signal=GPIO_Input +PB5.GPIOParameters=GPIO_PuPd +PB5.GPIO_PuPd=GPIO_PULLUP +PB5.Locked=true +PB5.Signal=GPIO_Input +PB6.GPIOParameters=GPIO_PuPd +PB6.GPIO_PuPd=GPIO_PULLUP +PB6.Locked=true +PB6.Signal=GPIO_Input +PB7.GPIOParameters=GPIO_PuPd +PB7.GPIO_PuPd=GPIO_PULLUP +PB7.Locked=true +PB7.Signal=GPIO_Input PC0.GPIOParameters=GPIO_PuPd,GPIO_Label PC0.GPIO_Label=AddrA PC0.GPIO_PuPd=GPIO_PULLUP @@ -150,6 +214,20 @@ PC3.GPIO_Label=OE PC3.GPIO_PuPd=GPIO_PULLUP PC3.Locked=true PC3.Signal=GPIO_Output +PC6.Locked=true +PC6.Signal=GPIO_Input +PC7.GPIOParameters=GPIO_PuPd +PC7.GPIO_PuPd=GPIO_PULLUP +PC7.Locked=true +PC7.Signal=GPIO_Input +PC8.GPIOParameters=GPIO_PuPd +PC8.GPIO_PuPd=GPIO_PULLUP +PC8.Locked=true +PC8.Signal=GPIO_Input +PC9.GPIOParameters=GPIO_PuPd +PC9.GPIO_PuPd=GPIO_PULLUP +PC9.Locked=true +PC9.Signal=GPIO_Input PF0-OSC_IN.Mode=HSE-External-Oscillator PF0-OSC_IN.Signal=RCC_OSC_IN PF1-OSC_OUT.Mode=HSE-External-Oscillator @@ -185,7 +263,7 @@ ProjectManager.ToolChainLocation= ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptBeforePath= ProjectManager.UnderRoot=false -ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_SPI2_Init-SPI2-false-HAL-true +ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_SPI2_Init-SPI2-false-HAL-true,4-MX_TIM3_Init-TIM3-false-HAL-true RCC.ADC12Freq_Value=170000000 RCC.ADC345Freq_Value=170000000 RCC.AHBFreq_Value=170000000 diff --git a/FinalHomework/MDK-ARM/FinalHomework.uvprojx b/FinalHomework/MDK-ARM/FinalHomework.uvprojx index 81b5fb8..201cb2b 100644 --- a/FinalHomework/MDK-ARM/FinalHomework.uvprojx +++ b/FinalHomework/MDK-ARM/FinalHomework.uvprojx @@ -438,6 +438,26 @@ 5 ..\Core\Inc\variable.h + + SegLed.h + 5 + ..\Core\Inc\SegLed.h + + + SegLed.c + 1 + ..\Core\Src\SegLed.c + + + MatrixKey.c + 1 + ..\Core\Src\MatrixKey.c + + + MatrixKey.h + 5 + ..\Core\Inc\MatrixKey.h + main.c 1 @@ -519,16 +539,6 @@ 1 ../Core/Src/stm32g4xx_hal_msp.c - - SegLed.h - 5 - ..\Core\Inc\SegLed.h - - - SegLed.c - 1 - ..\Core\Src\SegLed.c -