蓝桥杯物联网竞赛_STM32L071_9_按键矩阵扩展模块

原理图:

矩阵按键原理图:


实验板接口原理图:


得到对应图:

扫描按键原理:

按键的COLUMN1、2、3分别制0,每次只允许其中一个为0其他都是1(POW1和POW2正常状况为上拉),当有一个按键按下POW1和POW2必有一个被制0,这样就是能找到按键的标号
所以很明确COLUMN1、2、3应该制为输出电平,而POW1和POW2应该制输入电平用来读取按键被按下而传入的低电平

CubMX配置:

由于OLED引脚被占用所以用串口输出调试

Keil配置:

Function:

c 复制代码
#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制0
	HAL_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;	
}
c 复制代码
#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:

c 复制代码
 
#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)
  {
  }
   
}

运行效果:


相关推荐
Hello_Embed5 小时前
STM32HAL 快速入门(二十):UART 中断改进 —— 环形缓冲区解决数据丢失
笔记·stm32·单片机·学习·嵌入式软件
亿坊电商6 小时前
物联网领域中PHP框架的最佳选择有哪些?
物联网·struts·php
御控工业物联网7 小时前
灌区泵站远程监控物联网网关解决方案
物联网·远程监控·物联网网关·泵站·灌区泵站
清风6666668 小时前
基于STM32的APP遥控视频水泵小车设计
stm32·单片机·mongodb·毕业设计·音视频·课程设计
前端小超超8 小时前
capacitor配置ios应用图标不同尺寸
ios·蓝桥杯·cocoa
BAGAE11 小时前
MODBUS 通信协议详细介绍
linux·嵌入式硬件·物联网·硬件架构·iot·嵌入式实时数据库·rtdbs
小莞尔16 小时前
【51单片机】【protues仿真】基于51单片机数控直流稳压电源系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔16 小时前
【51单片机】【protues仿真】基于51单片机密码锁系统
c语言·stm32·单片机·嵌入式硬件·51单片机
黑不拉几的小白兔16 小时前
stm32教程:USART串口通信
stm32·单片机·嵌入式硬件
田甲21 小时前
【STM32】串口的阻塞、中断、DMA收发
stm32·单片机·嵌入式硬件