当前位置:网站首页>Blue Bridge Cup embedded_ STM32 learning_ Key_ Explain in detail
Blue Bridge Cup embedded_ STM32 learning_ Key_ Explain in detail
2022-07-06 01:44:00 【Moqim Flourite.】
Code
source : Blue Bridge Cup Official ;
Configuration code
#include "key.h"
// KEY Interface initialization
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;// Set structural variables
// allow GPIOA and GPIOB The clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);// Turn on the clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// PA0 and PA8 Floating input ( Reset state , It can be omitted )
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_8;// Select pin
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;// Choice mode ,key Recommended pull-up GPIO_Mode_IPU
GPIO_Init(GPIOA, &GPIO_InitStructure);// Initialize pins GPIOA
// PB1 and PB2 Floating input ( Reset state , It can be omitted )
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;// Select pin
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;// Choice mode
GPIO_Init(GPIOB, &GPIO_InitStructure);// Initialize pins GPIOB
}
// KEY scanning
unsigned char KEY_Scan(void)
{
unsigned char ucKey_Val = 0;
// Judge B1 and B2 Whether to press 1111 1110 1111 1111
if(~GPIO_ReadInputData(GPIOA) & 0x101)// Take the opposite first and then ,0000 0001 0000 0001, as long as GPIOA_0 and GPIOA_8 There is a press
{
Delay_KEY(10); // Time delay 10ms Desquamation
if(!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0))// Determine which press
//ucKey_Val = 1;
return 1;// return 1, And jump out of the function
if(!GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_8))
//ucKey_Val = 2;
return 2;
}
// Judge B3 and B4 Whether to press
else if(~GPIO_ReadInputData(GPIOB) & 6)
{
//0000 0000 0000 0110
Delay_KEY(10); // Time delay 10ms Desquamation
if(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1))
ucKey_Val = 3;
if(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_2))
ucKey_Val = 4;
}
return ucKey_Val;
}
void Delay_KEY(unsigned int ms)// Delay chattering
{
unsigned int i, j;
for(i=0; i<ms; i++)
for(j=0; j<7992; j++); // SYSCLK = 72MHz
// for(j=0; j<1598; j++); // SYSCLK = 8MHz
}
Application code
#include "key.h"
#include "led.h"
unsigned char ucLed = 1;//LED Display parameters
unsigned char ucKey_Long;// Last key value
unsigned long ulTick_ms, ulKey_Time;// current time , Last time
void KEY_Proc(void);// Statement KEY_Proc function
int main(void)
{
SysTick_Config(72000); // timing 1ms(HCLK = 72MHz)
KEY_Init();// Key initialization
LED_Init();//LED initialization
BUZ_Init();// Buzzer initialization
while(1)
{
KEY_Proc();
LED_Disp(ucLed);
}
}
void KEY_Proc(void)
{
unsigned char ucKey_Val;
ucKey_Val = KEY_Scan();// Get the key value
if(ucKey_Val != ucKey_Long)// When the current key value is not equal to the last key value
{
ucKey_Long = ucKey_Val;// Update key value
ulKey_Time = ulTick_ms;// Update time
}
else
ucKey_Val = 0;// Otherwise, the current key value is cleared
if(ucKey_Val == 1) // B1 Short press
{
ucLed <<= 1;// Every time you press , Running water lights up the next light
if(ucLed == 0) ucLed = 1;
}
if(ucKey_Val == 2) // B2 Short press
{
ucLed >>= 1;
if(ucLed == 0) ucLed = 0x80;
}
if(ucKey_Long == 1) // B1 Long press
{
if(ulTick_ms-ulKey_Time > 800)// When the current time is greater than the last time 800ms when
{
ulKey_Time = ulTick_ms;// Update time
ucLed <<= 2;
if(ucLed == 0) ucLed = 1;
}
}
if(ucKey_Long == 2) // B2 Long press
{
if(ulTick_ms-ulKey_Time > 800)
{
// ulKey_Time = ulTick_ms;
ucLed >>= 2;
if(ucLed == 0) ucLed = 0x80;
}
}
if(ucKey_Long == 3) // B3 Long press
GPIO_ResetBits(GPIOB, GPIO_Pin_4);// Turn on the buzzer
else
GPIO_SetBits(GPIOB, GPIO_Pin_4);
}
// SysTick Interrupt handling
void SysTick_Handler(void)
{
ulTick_ms++;// Every time 1ms Current time plus 1
}
Quick configuration
Open the official peripheral routine in the library file , find GPIO——
stay IOToggle" Under the folder , open “main.c”——
Copy and paste the basic configuration directly , Then modify the pin ;
Just recite the rest by yourself hhhh.
边栏推荐
- How does the crystal oscillator vibrate?
- Spir - V premier aperçu
- 阿里测开面试题
- 【Flask】静态文件与模板渲染
- 【Flask】官方教程(Tutorial)-part3:blog蓝图、项目可安装化
- NiO related knowledge (II)
- Huawei converged VLAN principle and configuration
- [network attack and defense training exercises]
- Leetcode3, implémenter strstr ()
- Basic process and testing idea of interface automation
猜你喜欢
Idea sets the default line break for global newly created files
【SSRF-01】服务器端请求伪造漏洞原理及利用实例
Redis如何实现多可用区?
Ordinary people end up in Global trade, and a new round of structural opportunities emerge
National intangible cultural heritage inheritor HD Wang's shadow digital collection of "Four Beauties" made an amazing debut!
Initialize MySQL database when docker container starts
Folio.ink 免费、快速、易用的图片分享工具
leetcode刷题_验证回文字符串 Ⅱ
Basic operations of database and table ----- delete data table
Folio. Ink is a free, fast and easy-to-use image sharing tool
随机推荐
Ali test open-ended questions
Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
干货!通过软硬件协同设计加速稀疏神经网络
leetcode-两数之和
A picture to understand! Why did the school teach you coding but still not
D22:indeterminate equation (indefinite equation, translation + problem solution)
Open source | Ctrip ticket BDD UI testing framework flybirds
Selenium element positioning (2)
Folio. Ink is a free, fast and easy-to-use image sharing tool
2022 Guangxi Autonomous Region secondary vocational group "Cyberspace Security" competition and its analysis (super detailed)
Paddle框架:PaddleNLP概述【飛槳自然語言處理開發庫】
Thinking about the best practice of dynamics 365 development collaboration
Basic operations of databases and tables ----- primary key constraints
Luo Gu P1170 Bugs Bunny and Hunter
[Yu Yue education] Liaoning Vocational College of Architecture Web server application development reference
selenium 等待方式
Competition question 2022-6-26
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
Sword finger offer 12 Path in matrix
leetcode刷题_反转字符串中的元音字母