当前位置:网站首页>STM32扩展版 按键扫描
STM32扩展版 按键扫描
2022-07-01 04:40:00 【花开半朵ོ】
首先,看原理图



对应着ADC2的通道13
采样时间让它久一点
代码
adc_key.c
#include "adc_key.h"
#define BTN_BUFF_LEN 50
uint32_t Get_ADC2(void)
{
uint32_t adc_value;
HAL_ADC_Start(&hadc2);
if(HAL_ADC_PollForConversion(&hadc2,10) == HAL_OK)
{
adc_value = HAL_ADC_GetValue(&hadc2);
}
return adc_value;
}
uint16_t Read_Btn(void)
{
uint16_t tmp;
uint8_t i = 0, j = 0;
uint16_t btn_buf[BTN_BUFF_LEN];
for(i = 0; i < BTN_BUFF_LEN; i++)
{
btn_buf[i] = Get_ADC2();
}
for(i = 0; i <= BTN_BUFF_LEN/2; i++)
{
for(j = 0; j < BTN_BUFF_LEN - i - 1;j++)
{
if(btn_buf[j+1] < btn_buf[j])
{
tmp = btn_buf[j+1];
btn_buf[j+1] = btn_buf[j];
btn_buf[j] = tmp;
}
}
}
if(BTN_BUFF_LEN % 2 == 0)
{
return ((btn_buf[BTN_BUFF_LEN/2-1] + btn_buf[BTN_BUFF_LEN/2])/2);
}
else
{
return (btn_buf[BTN_BUFF_LEN/2]);
}
}
uint8_t Scan_Key(void)
{
uint16_t btn_tmp = 0;
btn_tmp = Read_Btn();
if(btn_tmp < 0x0fff / 14)
return 1;
else if((btn_tmp > 0x0fff/14)&&(btn_tmp < 0x0fff/14*3))
return 2;
else if((btn_tmp > 0x0fff/14*3)&&(btn_tmp < 0x0fff/14*5))
return 3;
else if((btn_tmp > 0x0fff/14*5)&&(btn_tmp < 0x0fff/14*7))
return 4;
else if((btn_tmp > 0x0fff/14*7)&&(btn_tmp < 0x0fff/14*9))
return 5;
else if((btn_tmp > 0x0fff/14*9)&&(btn_tmp < 0x0fff/14*11))
return 6;
else if((btn_tmp > 0x0fff/14*11)&&(btn_tmp < 0x0fff/14*13))
return 7;
else if((btn_tmp > 0x0fff/14*13)&&(btn_tmp <0x0f6f))
return 8;
else
{
return 0;
}
}
adc_key.h
#include "main.h"
#include "adc.h"
uint32_t Get_ADC2(void);
uint16_t Read_Btn(void);
uint8_t Scan_Key(void);
main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2022 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "adc_key.h"
#include "lcd.h"
#include "stdio.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
__IO uint32_t lcd_uwTick;
uint8_t lcd_string[64] = {0};
//按键变量
uint8_t ucKey_Val,ucKey_Down,ucKey_Up,ucKey_Old;
__IO uint32_t key_uwTick = 0; //控制key_proc()的执行速度
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void LedDisp(uint8_t dsLED)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8
|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12,GPIO_PIN_SET);
//熄灭所有LED灯
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC,dsLED<<8,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
void Lcd_Proc()
{
if(uwTick - lcd_uwTick < 200)
return;
lcd_uwTick = uwTick;
// sprintf((char *)lcd_string,"%5.3fV",Get_ADC2()*3.3/4096);
// LCD_DisplayStringLine(Line1,lcd_string);
sprintf((char *)lcd_string,"%04X,%1d",Get_ADC2(),(uint32_t)Scan_Key());
LCD_DisplayStringLine(Line2,lcd_string);
}
void key_proc()
{
if(uwTick - key_uwTick < 50)
return;
key_uwTick = uwTick;
ucKey_Val = Scan_Key();
ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val);
ucKey_Old = ucKey_Val;
switch(ucKey_Down)
{
case 1: LedDisp(0X01); break;
case 2: LedDisp(0X02); break;
case 3: LedDisp(0XFF); break;
case 4: LedDisp(0X00); break;
case 5: break;
case 6: break;
case 7: break;
case 8: break;
default:
break;
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC2_Init();
/* USER CODE BEGIN 2 */
LCD_Init();
LCD_Clear(White);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
Lcd_Proc();
key_proc();
}
/* USER CODE END 3 */
}边栏推荐
猜你喜欢

Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom

C - detailed explanation of operators and summary of use cases

2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank

2022年聚合工艺考试题及模拟考试

Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor

LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机

Extension fragment

Daily question - line 10

2022 gas examination question bank and online simulation examination

pytorch中常用数据集的使用方法
随机推荐
Leecode records the number of good segmentation of 1525 strings
Use winmtr software to simply analyze, track and detect network routing
Web server: how to choose a good web server these five aspects should be paid attention to
Shell之分析服务器日志命令集锦
Basic exercise of test questions hexadecimal to decimal
C language games (I) -- guessing games
Leecode record 1351 negative numbers in statistical ordered matrix
Ten wastes of software research and development: the other side of research and development efficiency
All in all, the low code still needs to solve these four problems
Day 52 - tree problem
Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)
Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
[2020 overview] overview of link prediction based on knowledge map embedding
LeetCode_66(加一)
数据加载及预处理
扩展-Fragment
2022年上海市安全员C证考试题模拟考试题库及答案
The design points of voice dialogue system and the importance of multi round dialogue
总结全了,低代码还需要解决这4点问题
Dataloader的使用