由原理图可以得知:四个按键k1,k2,k3,k4分别对应GPIOA的引脚0和8,GPIOB的引脚1和2,而按键是向开发板输入信息的,因此GPIO模式应该选择输入的方式,由原理图,我们发现,应该是上拉输入的方式,而且由于没有电容存在,不存在硬件消抖,因此,需要使用软件消抖的方式,如延时读取
(看不懂去看野火视频,对照着学。野火视频讲的很详细,就是开发板不配套。。。)
代码如下:
(具体描述看代码的注释)
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;