当前位置:网站首页>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 */
}
边栏推荐
- Grey correlation cases and codes
- Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
- 2022年化工自动化控制仪表操作证考试题库及答案
- 技术分享| 融合调度中的广播功能设计
- 2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
- pytorch神经网络搭建 模板
- Difference between cookie and session
- 软件研发的十大浪费:研发效能的另一面
- 【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点
- 2022 polymerization process test questions and simulation test
猜你喜欢
LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
Maixll-Dock 使用方法
2022年上海市安全员C证考试题模拟考试题库及答案
技术分享| 融合调度中的广播功能设计
Kodori tree board
软件研发的十大浪费:研发效能的另一面
Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
Offline installation of Wireshark 2.6.10
【硬十宝典】——1.【基础知识】电源的分类
Why is Internet thinking not suitable for AI products?
随机推荐
Summary of acl2021 information extraction related papers
About the transmission pipeline of stage in spark
RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
What are permissions? What are roles? What are users?
Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
做网站数据采集,怎么选择合适的服务器呢?
Summary of testing experience - Testing Theory
js解决浮点数相乘精度丢失问题
pytorch中常用数据集的使用方法
LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
Caijing 365 stock internal reference | the first IPO of Beijing stock exchange; the subsidiary of the recommended securities firm for gambling and gambling, with a 40% discount
Rule method: number of effective triangles
Difference between cookie and session
Research on medical knowledge atlas question answering system (I)
pytorch神经网络搭建 模板
为什么香港服务器最适合海外建站使用
Pytorch(四) —— 可视化工具 Visdom
2022年T电梯修理题库及模拟考试
LeetCode_58(最后一个单词的长度)
LeetCode_35(搜索插入位置)