当前位置:网站首页>[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
边栏推荐
- 万字讲清 synchronized 和 ReentrantLock 实现并发中的锁
- [wsl2]wsl2 migrate virtual disk file ext4 vhdx
- 谷歌加大型文字广告是什么?怎么用?
- leetcode743. Network latency (medium, Dijkstra)
- Should the audience choose observation mode or positioning mode?
- 机器学习基础 SVM(支持向量机)
- LabVIEW large project development tools to improve quality
- URI, URL and urn difference, relation and syntax diagram
- 如何利用您的自有数据来实现营销目标?
- Wildcard usage of go standard library FMT
猜你喜欢

How to learn C language and share super detailed experience (learning note 1 -- basic data types of C language)

Detailed explanation of maxpooling corresponding to conv1d, conv2d and conv3d machines of tensorflow2

移动IPv6光猫登录的一般ip地址账号与密码,移动光猫变桥接模式

Alertwindowmanager pop up prompt window help (Part 1)

关于tkinter.Canvas 不显示图片的问题

(no plug-in) summary of vim basic shortcut keys

Use koa to mock data and set cross domain issues

Using OpenCV in go

Use mediapipe+opencv to make a simple virtual keyboard

Run Presto under docker to access redis and Bi presentation
随机推荐
TensorFlow 2.x 多显卡分布式训练
Uuid/guid introduction, generation rules and generation codes
About retrieving ignored files in cornerstone
Restful interface specification annotation of pringboot (2)
Developer contributions amd Xilinx Chinese Forum sharing - wisdom of questioning
Introduction to Google unit testing tools GTEST and gmoke
Calculation of accuracy, recall rate, F1 value and accuracy rate of pytorch prediction results (simple implementation)
[WSL2]WSL2迁移虚拟磁盘文件ext4.vhdx
VI keyboard diagram
Auto commit attribute of MySQL
Rsync transport exclusion directory
三、上传织物图片至SQL Server并提供name进行展示织物照片
Magics 23.0如何激活和使用视图工具页的切片预览功能
rsync 傳輸排除目錄
Establishment of microservice development environment
兴趣相似的受众群体
CXGRID keeps the original display position after refreshing the data
Use of Arduino series pressure sensors and detected data displayed by OLED (detailed tutorial)
STM32 3*3矩阵按键(寄存器版本)
MySQL download and installation