硬件连接

main.c
#include "stm32f10x.h"
#include "bsp_led.h"
#include "bsp_key.h"
int main(void)
{
LED_GPIO_Config();
KEY_GPIO_Config();
while(1)
{
if ( KEY_Scan(KEY1_GPIO_PORT, KEY1_GPIO_PIN) == KEY_ON )
{
LED_G_TOGGLE;
}
}
}
bsp_key.h
#ifndef __BSP_KEY_H
#define __BSP_KEY_H
#include "stm32f10x.h"
#define KEY1_GPIO_PORT GPIOA
#define KEY1_GPIO_PIN GPIO_Pin_0
#define KEY1_GPIO_CLK RCC_APB2Periph_GPIOA
#define KEY_ON 1
#define KEY_OFF 0
void KEY_GPIO_Config(void);
uint8_t KEY_Scan(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
#endif
bsp_key.c
#include "bsp_key.h"
void KEY_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK, ENABLE);
GPIO_InitStruct.GPIO_Pin = KEY1_GPIO_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(KEY1_GPIO_PORT , &GPIO_InitStruct);
}
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;
}
bsp_led.h 及 bsp_led.c
点亮LED灯(固件库版)
代码参考野火教程,有所修改