当前位置:网站首页>[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
 Insert picture description here
This is the pin of the key
Receive the input signal
 Insert picture description here
This is the schematic diagram
When you press the key KEY2 When pressed ,BACKUP Just from 1 Turn into 0
 Insert picture description here
According to this principle, we can make a key scan to identify and save the state of the key
 Insert picture description here

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

原网站

版权声明
本文为[It's Beichen bupiacra]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280549162063.html