原理图:
矩阵按键原理图:
实验板接口原理图:
得到对应图:
扫描按键原理:
按键的COLUMN1、2、3分别制0,每次只允许其中一个为0其他都是1(POW1和POW2正常状况为上拉),当有一个按键按下POW1和POW2必有一个被制0,这样就是能找到按键的标号
所以很明确COLUMN1、2、3应该制为输出电平,而POW1和POW2应该制输入电平用来读取按键被按下而传入的低电平
CubMX配置:
由于OLED引脚被占用所以用串口输出调试
Keil配置:
Function:
#include "Function.h"
#include "usart.h"
#include "gpio.h"void Function_SendInformation(const char * data, uint16_t len){HAL_UART_Transmit(&huart2, data, len, 0xff);
}void Function_ReciveInformation(char * data, uint16_t len){HAL_UART_Receive(&huart2, data, len, 0xff);
}void Function_OnlyRsetPortPin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin){ // 将其中一个COLUMN制0HAL_GPIO_WritePin(COLUMN1_GPIO_Port, COLUMN1_Pin, GPIO_PIN_SET);HAL_GPIO_WritePin(COLUMN2_GPIO_Port, COLUMN2_Pin, GPIO_PIN_SET);HAL_GPIO_WritePin(COLUMN3_GPIO_Port, COLUMN3_Pin, GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOx, GPIO_Pin, GPIO_PIN_RESET);
}unsigned char Function_ResetPowNumberFind(void){ // 找到哪一根POW线制零了if(HAL_GPIO_ReadPin(POW1_GPIO_Port, POW1_Pin) == GPIO_PIN_RESET){ HAL_Delay(2); // 去抖动if(HAL_GPIO_ReadPin(POW1_GPIO_Port, POW1_Pin) == GPIO_PIN_RESET){ // 有效按下while(HAL_GPIO_ReadPin(POW1_GPIO_Port, POW1_Pin) == GPIO_PIN_RESET); // 按下一直有效return 1; // 松开放回POW1被制0}}if(HAL_GPIO_ReadPin(POW2_GPIO_Port, POW2_Pin) == GPIO_PIN_RESET){ HAL_Delay(2); // 去抖动if(HAL_GPIO_ReadPin(POW2_GPIO_Port, POW2_Pin) == GPIO_PIN_RESET){ // 有效按下while(HAL_GPIO_ReadPin(POW2_GPIO_Port, POW2_Pin) == GPIO_PIN_RESET); // 按下一直有效return 2; // 松开放回POW1被按下}}return 0; // 即POW1和POW2都未读取0
}unsigned char Function_KeyMatrixCheck(void){unsigned char PowFind = 0;Function_OnlyRsetPortPin(COLUMN1_GPIO_Port, COLUMN1_Pin);PowFind = Function_ResetPowNumberFind(); if(PowFind == 1) return 1;else if(PowFind == 2) return 4;PowFind = 0;Function_OnlyRsetPortPin(COLUMN2_GPIO_Port, COLUMN2_Pin);PowFind = Function_ResetPowNumberFind(); if(PowFind == 1) return 2;else if(PowFind == 2) return 5;PowFind = 0;Function_OnlyRsetPortPin(COLUMN3_GPIO_Port, COLUMN3_Pin);PowFind = Function_ResetPowNumberFind(); if(PowFind == 1) return 3;else if(PowFind == 2) return 6;
}
#ifndef __FUNCTION__
#define __FUNCTION__
#include <stdint.h>void Function_SendInformation(const char * data, uint16_t len);
void Function_ReciveInformation(char * data, uint16_t len);
unsigned char Function_KeyMatrixCheck(void);
#endif
main:
#include "main.h"
#include "usart.h"
#include "gpio.h"
#include "Function.h"void SystemClock_Config(void);int main(void)
{unsigned char Rx[1];HAL_Init();SystemClock_Config();MX_GPIO_Init();MX_USART2_UART_Init();while (1){Rx[0] = Function_KeyMatrixCheck();if(Rx[0]){ // 返回不是0才输出Function_SendInformation(Rx, 1);}HAL_Delay(1000);}}void SystemClock_Config(void)
{RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK){Error_Handler();}PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK){Error_Handler();}
}void Error_Handler(void)
{__disable_irq();while (1){}}
运行效果: