当前位置:网站首页>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 */
}
边栏推荐
- [pat (basic level) practice] - [simple simulation] 1064 friends
- js解决浮点数相乘精度丢失问题
- All in all, the low code still needs to solve these four problems
- MySQL winter vacation self-study 2022 12 (5)
- 神经网络-卷积层
- Difficulties in the development of knowledge map & the importance of building industry knowledge map
- Seven crimes of counting software R & D Efficiency
- 2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers
- 2022年煤气考试题库及在线模拟考试
- I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
猜你喜欢
How to do the performance pressure test of "Health Code"
VIM easy to use tutorial
C#读写应用程序配置文件App.exe.config,并在界面上显示
Grey correlation cases and codes
AssertionError assert I.ndim == 4 and I.shape[1] == 3
Dual contractual learning: text classification via label aware data augmentation reading notes
Difficulties in the development of knowledge map & the importance of building industry knowledge map
神经网络-非线性激活
2022年T电梯修理题库及模拟考试
Internet winter, how to spend three months to make a comeback
随机推荐
CUDA development and debugging tool
扩展-Fragment
Shell analysis server log command collection
(12) Somersault cloud case (navigation bar highlights follow)
Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)
C#读写应用程序配置文件App.exe.config,并在界面上显示
Odeint and GPU
【FTP】FTP连接时出现“227 Entering Passive Mode”的解决方法
This sideline workload is small, 10-15k, free unlimited massage
LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers
pytorch神经网络搭建 模板
LeetCode_53(最大子数组和)
2022危险化学品生产单位安全生产管理人员题库及答案
C -- array
Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
OdeInt与GPU
JS rotation chart
JS image path conversion Base64 format
神经网络-使用Sequential搭建神经网络