当前位置:网站首页>[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
边栏推荐
- Using OpenCV in go
- [wsl2]wsl2 migrate virtual disk file ext4 vhdx
- Developer contributions amd Xilinx Chinese Forum sharing - wisdom of questioning
- Installing pytorch geometric
- Record the VMware installation process of VMware Tools and some problems encountered
- D template instance does not match declaration
- leetcode743. Network latency (medium, Dijkstra)
- 他山之石:a16z 的 Web3 投资版图
- Jeux de plombiers
- Torch. Distributions. Normal
猜你喜欢

Workspace for ROS

Matplotlib drawing Chinese garbled code
![[Andoid][踩坑]CTS 11_r3开始出现的testBootClassPathAndSystemServerClasspath_nonDuplicateClasses FAIL问题分析](/img/b5/7ea603775dc0448368d209de037a43.png)
[Andoid][踩坑]CTS 11_r3开始出现的testBootClassPathAndSystemServerClasspath_nonDuplicateClasses FAIL问题分析

Using OpenCV in go

csdn涨薪技术之Jmeter接口测试数据库断言的实现与设计

Restful interface specification annotation of pringboot (2)

matplotlib画图中文乱码

What is the path field—— Competitive advertising
![[andoid][step pit]cts 11_ Testbootclasspathandsystemserverclasspath at the beginning of R3_ Analysis of nonduplicateclasses fail](/img/b5/7ea603775dc0448368d209de037a43.png)
[andoid][step pit]cts 11_ Testbootclasspathandsystemserverclasspath at the beginning of R3_ Analysis of nonduplicateclasses fail

Design of distributed game server
随机推荐
Get started quickly cmake
Devaxpress Chinese description --tcxpropertiesstore (property store recovery control)
深度学习调参技巧详解
Using atexit to realize automatic destruct of singleton mode
兴趣相似的受众群体
Implementation and design of JMeter interface test database assertion for CSDN salary increase technology
Alertwindowmanager pop up prompt window help (Part 1)
一种不带CPU的DPU架构:Hyperion
Rsync transport exclusion directory
[arithmetic, relation, logic, bit, compound assignment, self increasing, self decreasing and other] operators (learning note 4 -- C language operators)
微服务开发环境搭建
Delphi7 compressed pictures (BMP, JPG, PNG)
ng-tv-focusable
dfs与bfs解决宝岛探险
Workspace for ROS
rsync 傳輸排除目錄
Use mediapipe+opencv to make a simple virtual keyboard
STM32 3*3矩阵按键(寄存器版本)
如何通过受众群体定位解决实际问题?
Calculation of accuracy, recall rate, F1 value and accuracy rate of pytorch prediction results (simple implementation)