当前位置:网站首页>STM32 extended key scan
STM32 extended key scan
2022-07-01 04:43:00 【Flowers bloom in half】
First , Look at the schematic



Corresponding ADC2 The passage of 13
Take longer to sample
Code
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};
// Key variables
uint8_t ucKey_Val,ucKey_Down,ucKey_Up,ucKey_Old;
__IO uint32_t key_uwTick = 0; // control key_proc() Execution speed of
/* 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);
// Extinguish all LED The lamp
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 */
}边栏推荐
- Dataloader的使用
- Shell之Unix运维常用命令
- Odeint et GPU
- 2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
- 技术分享| 融合调度中的广播功能设计
- 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
- Matters behind the construction of paint testing laboratory
- LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver
- Pytorch(三) —— 函数优化
- LeetCode_53(最大子数组和)
猜你喜欢

The junior college students were angry for 32 days, four rounds of interviews, five hours of soul torture, and won Ali's offer with tears

C -- array

Research on medical knowledge atlas question answering system (I)
![[ue4] event distribution mechanism of reflective event distributor and active call event mechanism](/img/44/6a26ad24d56ddd5156f3a31fa7e0b9.jpg)
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism

How to do the performance pressure test of "Health Code"

尺取法:有效三角形的个数

C#读写应用程序配置文件App.exe.config,并在界面上显示

2022危险化学品生产单位安全生产管理人员题库及答案

One click shell to automatically deploy any version of redis

The index is invalid
随机推荐
The design points of voice dialogue system and the importance of multi round dialogue
How to use maixll dock
如何看待智慧城市建设中的改变和机遇?
OdeInt與GPU
总结全了,低代码还需要解决这4点问题
Openresty rewrites the location of 302
数据加载及预处理
Odeint et GPU
离线安装wireshark2.6.10
C language games (I) -- guessing games
常用的Transforms中的方法
做网站数据采集,怎么选择合适的服务器呢?
Summary of acl2021 information extraction related papers
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
2022年煤气考试题库及在线模拟考试
VIM easy to use tutorial
Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
Pytorch(二) —— 激活函数、损失函数及其梯度
2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
Grey correlation cases and codes