当前位置:网站首页>[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO
[the second day of the actual combat of the smart lock project based on stm32f401ret6 in 10 days] light up with the key ----- input and output of GPIO
2022-06-13 01:51:00 【It's Beichen bupiacra】
Light the lamp with the key ----GPIO Input and output of
This is just an idea to give you a reference on how to do such a thing , The initialization of different types of MCU is different , But the basic principles are the same , If you are interested in this project, you can comment 、 Pay attention to me , If you want to do such a project, you can talk to me in private , I will help you .
If this blog post is helpful to you, please pay attention to it 、 give the thumbs-up 、 Collect it , Thank you for your support !
One 、 Principle explanation
First, let's take a look at the schematic diagram
This is a LED The pins of
Used to output 
This is the pin of the key
Receive the input signal 
This is the schematic diagram
When you press the key KEY2 When pressed ,BACKUP Just from 1 Turn into 0
According to this principle, we can make a key scan to identify and save the state of the key 
Two 、 Code implementation
This is the code for key initialization and key scanning
key.c
#include "key.h"
/* The functionality :KEY initialization Parameters :void Return value :void explain : 1、 Can make GPIOC The clock 2、 initialization GPIOC mouth PC13: Floating input mode */
void Key_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;// Define structure variables
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);// Can make GPIOC The clock
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;// Floating input mode
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;//PC13
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;// No up and down
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;//50Mhz
GPIO_Init(GPIOC, &GPIO_InitStruct);// initialization GPIOC mouth
}
/* The functionality : Key scan Parameters :void Return value :u8 explain : 1、 Identification key 2、 Desquamation 3、 Record key status */
u8 Key_Scan(void)
{
static u8 flag = 0;// The status of pressing a key without a key is 0
if(KEY1 == 0 && flag == 0)
{
Delay_ms(20);
if(KEY1 == 0)
{
flag = 1;// Press the key and the status of the key is 1
return 1;
}
}
else if(KEY1 == 1 && flag == 1)
{
flag = 0;// The status of pressing a key without a key is 0
}
return 0;
}
key.h
#ifndef _KEY_H
#define _KEY_H
#include "stm32f4xx.h"
#include "delay.h"
#include "io_bit.h"
#define KEY1 PCin(13)// Bit band macro definition
void Key_Config(void);
u8 Key_Scan(void);
#endif
main.c
Let's first define a variable to receive the value of the key , If the return value of the key is 1 That's it , That's right LED Reverse it so that you can turn on the light by pressing the button once , Press again to turn off the light ; If the value of the key is 0, There is no key to press .
#include "main.h"
int main()
{
u8 Key_State;
Led_Config();
Key_Config();
while(1)
{
/* Lighten up LED */
// GPIO_SetBits(GPIOB, GPIO_Pin_8);// Turn off
// GPIO_SetBits(GPIOB, GPIO_Pin_9);
// GPIO_ResetBits(GPIOB, GPIO_Pin_8);// open
// GPIO_ResetBits(GPIOB, GPIO_Pin_9);
// LED1 = 1;
// LED2 = 1;
// LED1 = 0;
// LED2 = 0;
/* Key on LED */
Key_State = Key_Scan();
if(Key_State == 1)
{
LED1 = ~LED1;
LED2 = ~LED2;
}
}
}
main.h
#ifndef _MAIN_H
#define _MAIN_H
#include "stm32f4xx.h"
#include "led.h"
#include "key.h"
#endif
边栏推荐
- Magics 23.0如何激活和使用视图工具页的切片预览功能
- How many smart bids does Google have?
- numpy多维数组转置transpose
- Auto commit attribute of MySQL
- Run Presto under docker to access redis and Bi presentation
- MySQL - use field alias after where
- Installing pytorch geometric
- Alertwindowmanager pop up prompt window help (Part 1)
- URI, URL and urn difference, relation and syntax diagram
- Implementation and design of JMeter interface test database assertion for CSDN salary increase technology
猜你喜欢

白噪声的详细理解

URI, URL and urn difference, relation and syntax diagram

三、上传织物图片至SQL Server并提供name进行展示织物照片
![[Andoid][踩坑]CTS 11_r3开始出现的testBootClassPathAndSystemServerClasspath_nonDuplicateClasses FAIL问题分析](/img/b5/7ea603775dc0448368d209de037a43.png)
[Andoid][踩坑]CTS 11_r3开始出现的testBootClassPathAndSystemServerClasspath_nonDuplicateClasses FAIL问题分析

LabVIEW大型项目开发提高质量的工具

五、库存查询功能的完善

Record the VMware installation process of VMware Tools and some problems encountered

What is Google plus large text ads? How to use it?

Combining strings and numbers using ssstream

How to learn C language and share super detailed experience (learning note 1 -- basic data types of C language)
随机推荐
Detailed understanding of white noise
leetcode743. Network latency (medium, Dijkstra)
Restrict cell input type and display format in CXGRID control
Alertwindowmanager pop up prompt window help (Part 1)
Read routing table
机器学习基础 SVM(支持向量机)
[arithmetic, relation, logic, bit, compound assignment, self increasing, self decreasing and other] operators (learning note 4 -- C language operators)
Installing pytorch geometric
Compiling minicom-2.7.1 under msys2
VI keyboard diagram
About tkinter Canvas does not display pictures
MySQL - use field alias after where
Devexpress implementation flow chart
STM32 3*3矩阵按键(寄存器版本)
Pyflink implements custom sourcefunction
[printf function and scanf function] (learning note 5 -- standard i/o function)
白噪声的详细理解
Logging system in chromium
Plumber game
Design of distributed game server