当前位置:网站首页>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 */
}边栏推荐
- 无器械健身
- 总结全了,低代码还需要解决这4点问题
- CUDA development and debugging tool
- What are permissions? What are roles? What are users?
- Shell之分析服务器日志命令集锦
- Basic usage, principle and details of session
- Software testing needs more and more talents. Why do you still not want to take this path?
- Applications and features of VR online exhibition
- Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
- Openresty rewrites the location of 302
猜你喜欢

Ten wastes of software research and development: the other side of research and development efficiency

无器械健身

神经网络-卷积层

C -- array

The index is invalid

2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities

Kodori tree board

Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions

【硬十宝典】——1.【基础知识】电源的分类

神经网络-非线性激活
随机推荐
LeetCode_35(搜索插入位置)
网站服务器:好用的网站服务器怎么选这五方面要关注
Maixll dock quick start
Odeint et GPU
Threejs opening
Advanced application of ES6 modular and asynchronous programming
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism
Some small knowledge points
Basic usage, principle and details of session
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
[godot] unity's animator is different from Godot's animplayer
selenium打开chrome浏览器时弹出设置页面:Mircrosoft Defender 防病毒要重置您的设置
2022危险化学品生产单位安全生产管理人员题库及答案
[2020 overview] overview of link prediction based on knowledge map embedding
Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)
How to view the changes and opportunities in the construction of smart cities?
LeetCode_66(加一)
How do I sort a list of strings in dart- How can I sort a list of strings in Dart?
神经网络-最大池化的使用
2022 gas examination question bank and online simulation examination