版权声明:如需转载请标注 https://blog.csdn.net/weixin_40973138/article/details/85156127
本工程板级支持包文件适用于野火stm32f429 开发板。
本工程中涉及bsp_led.c, bsp_led.h 等文件,请前往STM32F429 >> 4. 使用固件库点亮LED进行查看
bsp_key.c
/**
******************************************************************************
* @file bsp_key.c
* @author Waao
* @version V1.0.0
* @date 20-Dec-2018
* @brief This file contains some board support package's functions for the KEY.
*
******************************************************************************
* @attention
*
* None
*
******************************************************************************
*/
#include <bsp_key.h>
/**
* @brief Initialize the key.
* @note None
* @param None
* @retval None
*/
void KEY_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_Structure;
RCC_AHB1PeriphClockCmd(KEY1_GPIO_CLK, ENABLE);
RCC_AHB1PeriphClockCmd(KEY2_GPIO_CLK, ENABLE);
GPIO_Structure.GPIO_Mode = GPIO_Mode_IN;
GPIO_Structure.GPIO_Speed = GPIO_Medium_Speed;
GPIO_Structure.GPIO_OType = GPIO_OType_PP;
GPIO_Structure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Structure.GPIO_Pin = KEY1_PIN;
GPIO_Init(KEY1_GPIO_PORT, &GPIO_Structure);
GPIO_Structure.GPIO_Pin = KEY2_PIN;
GPIO_Init(KEY2_GPIO_PORT, &GPIO_Structure);
}
/**
* @brief Detect the key whether be pressed down actually.
* @note None
* @param GPIOx: where x can be (A..K) to select the GPIO peripheral.
* GPIO_Pin: specifies the port bit to read. This parameter can be GPIO_Pin_x where x can be (0..15).
* @retval The status of the input port.
*/
int 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;
}
bsp_key.h
/**
******************************************************************************
* @file bsp_key.h
* @author Waao
* @version V1.0.0
* @date 20-Dec-2018
* @brief This file contains some board support package's definition for the KEY.
*
******************************************************************************
* @attention
*
* None
*
******************************************************************************
*/
#ifndef __BSP_KEY_H_
#define __BSP_KEY_H_
#include <stm32f4xx.h>
#define KEY1_PIN GPIO_Pin_0
#define KEY1_GPIO_PORT GPIOA
#define KEY1_GPIO_CLK RCC_AHB1Periph_GPIOA
#define KEY2_PIN GPIO_Pin_13
#define KEY2_GPIO_PORT GPIOC
#define KEY2_GPIO_CLK RCC_AHB1Periph_GPIOC
#define KEY_ON 1
#define KEY_OFF 0
void KEY_GPIO_Config(void);
int Key_Scan(GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin);
#endif
main.c
#include <stm32f4xx.h>
#include <bsp_key.h>
#include "bsp_led.h"
int main(void)
{
LED_GPIO_Config();
KEY_GPIO_Config();
LED_BLUE;
while(1)
{
if(Key_Scan(KEY2_GPIO_PORT, KEY2_PIN) == KEY_ON)
{
LED3_TOGGLE;
}
if(Key_Scan(KEY1_GPIO_PORT, KEY1_PIN) == KEY_ON)
{
LED3_TOGGLE;
}
}
}