由原理圖可以得知:四個按鍵k1,k2,k3,k4分别對應GPIOA的引腳0和8,GPIOB的引腳1和2,而按鍵是向開發闆輸入資訊的,是以GPIO模式應該選擇輸入的方式,由原理圖,我們發現,應該是上拉輸入的方式,而且由于沒有電容存在,不存在硬體消抖,是以,需要使用軟體消抖的方式,如延時讀取
(看不懂去看野火視訊,對照着學。野火視訊講的很詳細,就是開發闆不配套。。。)
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHL0kkaNBTSE5UeRpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLwMjMwITMwEjM2IjMxkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
代碼如下:
(具體描述看代碼的注釋)
key.c
#include"key.h"
void KEY_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct1;
GPIO_InitTypeDef GPIO_InitStruct2;
GPIO_InitTypeDef GPIO_InitStruct3;
GPIO_InitTypeDef GPIO_InitStruct4;
//時鐘使能
RCC_APB2PeriphClockCmd(KEY1_2_GPIO_CLK, ENABLE);
RCC_APB2PeriphClockCmd(KEY3_4_GPIO_CLK, ENABLE);
//配置輸入模式不需要使用速率,是以隻配置倆個結構體成員
//按鍵K1的GPIO模式配置
GPIO_InitStruct1.GPIO_Pin= KEY1_GPIO_Pin;
GPIO_InitStruct1.GPIO_Mode= GPIO_Mode_IPU;
GPIO_Init(KEY1_GPIO_PORT,&GPIO_InitStruct1);
//按鍵K2的GPIO模式配置
GPIO_InitStruct2.GPIO_Pin= KEY2_GPIO_Pin;
GPIO_InitStruct2.GPIO_Mode= GPIO_Mode_IPU;
GPIO_Init(KEY2_GPIO_PORT,&GPIO_InitStruct2);
//按鍵K3的GPIO模式配置
GPIO_InitStruct3.GPIO_Pin= KEY3_GPIO_Pin;
GPIO_InitStruct3.GPIO_Mode= GPIO_Mode_IPU;
GPIO_Init(KEY3_GPIO_PORT,&GPIO_InitStruct3);
//按鍵K4的GPIO模式配置
GPIO_InitStruct4.GPIO_Pin= KEY4_GPIO_Pin;
GPIO_InitStruct4.GPIO_Mode= GPIO_Mode_IPU;
GPIO_Init(KEY4_GPIO_PORT,&GPIO_InitStruct4);
}
uint8_t Key_Scan(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin)//按鍵檢測
{
if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin)==KEY_ON)//如果按鍵按下
{
while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin)==KEY_ON)//直到按鍵松開才結束循環
;
return KEY_ON; //認為一次按下和松開是一次按鍵
}
else return KEY_OFF;//沒有按下鍵
}
key.h
#ifndef _KEY_H
#define _KEY_H
#include "stm32f10x.h"
#define KEY_ON 0
#define KEY_OFF 1
#define KEY1_GPIO_Pin GPIO_Pin_0
#define KEY2_GPIO_Pin GPIO_Pin_8
#define KEY3_GPIO_Pin GPIO_Pin_1
#define KEY4_GPIO_Pin GPIO_Pin_2
#define KEY1_GPIO_PORT GPIOA
#define KEY2_GPIO_PORT GPIOA
#define KEY3_GPIO_PORT GPIOB
#define KEY4_GPIO_PORT GPIOB
#define KEY1_2_GPIO_CLK RCC_APB2Periph_GPIOA
#define KEY3_4_GPIO_CLK RCC_APB2Periph_GPIOB
led.c
#include "led.h"
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct1;
GPIO_InitTypeDef GPIO_InitStruct2;
GPIO_InitTypeDef GPIO_InitStruct3;
GPIO_InitTypeDef GPIO_InitStruct4;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStruct1.GPIO_Pin= LED0_GPIO_Pin;
GPIO_InitStruct1.GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_InitStruct1.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(LED0_GPIO_PORT,&GPIO_InitStruct1);
GPIO_InitStruct2.GPIO_Pin= LED1_GPIO_Pin;
GPIO_InitStruct2.GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_InitStruct2.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(LED1_GPIO_PORT,&GPIO_InitStruct2);
GPIO_InitStruct3.GPIO_Pin= LED2_GPIO_Pin;
GPIO_InitStruct3.GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_InitStruct3.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(LED2_GPIO_PORT,&GPIO_InitStruct3);
GPIO_InitStruct4.GPIO_Pin= LED3_GPIO_Pin;
GPIO_InitStruct4.GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_InitStruct4.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(LED3_GPIO_PORT,&GPIO_InitStruct4);
}
led.h
#ifndef _LED_H
#define _LED_H
#include "stm32f10x.h"
#define ON 0
#define OFF 1
#define LED_G(a) if(a) \
GPIO_SetBits(LED0_GPIO_PORT,LED0_GPIO_Pin);\
else GPIO_ResetBits(LED0_GPIO_PORT,LED0_GPIO_Pin);
#define LED0_GPIO_Pin GPIO_Pin_8
#define LED1_GPIO_Pin GPIO_Pin_9
#define LED2_GPIO_Pin GPIO_Pin_10
#define LED3_GPIO_Pin GPIO_Pin_11
#define LED0_GPIO_PORT GPIOC
#define LED1_GPIO_PORT GPIOC
#define LED2_GPIO_PORT GPIOC
#define LED3_GPIO_PORT GPIOC
//異或操作:相異為1,相同為0.由于LED的GPIOC的ODR複位值為0x00000000;LED0_GPIO_Pin的值為0x00000001,是以每一次的異或都相當于是把本位取反。如果其他的按鍵按下,導緻ODR的其他位變成了1,和0異或仍然是1,保持不變,是以不論其他的引腳取值如何,異或以後,仍然隻是本位的引腳取值取反。相當于打開變關閉,關閉變打開。
#define LED_GH1 (LED0_GPIO_PORT->ODR^=LED0_GPIO_Pin)
#define LED_GH2 (LED1_GPIO_PORT->ODR^=LED1_GPIO_Pin)
#define LED_GH3 (LED2_GPIO_PORT->ODR^=LED2_GPIO_Pin)
#define LED_GH4 (LED3_GPIO_PORT->ODR^=LED3_GPIO_Pin)
#define LED0_GPIO_CLK RCC_APB2Periph_GPIOC
void LED_GPIO_Config(void);
#endif
main.c
#include "stm32f10x.h"
#include "led.h"
#include "key.h"
int main()
{
LED_GPIO_Config();
KEY_GPIO_Config();
while(1)
{
if(Key_Scan(KEY1_GPIO_PORT,KEY1_GPIO_Pin)==KEY_ON) //判斷按鍵是否按下
LED_GH1;
if(Key_Scan(KEY2_GPIO_PORT,KEY2_GPIO_Pin)==KEY_ON)
LED_GH2;
if(Key_Scan(KEY3_GPIO_PORT,KEY3_GPIO_Pin)==KEY_ON)
LED_GH3;
if(Key_Scan(KEY4_GPIO_PORT,KEY4_GPIO_Pin)==KEY_ON)
LED_GH4;