(基于奋斗stm32_v5开发板)
#include"stm32f10x.h"
#define ON 1
#define OFF 0
void LED_GPIO_Config()
{
/*定义一个GPIO_InitTypeDef类型的结构体*/
GPIO_InitTypeDef GPIO_InitStructure;
/*开启GPIOB的外设时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/*选择要控制的GPIOB引脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
/*设置引脚模式为通用推挽输出*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
/*设置引脚速率为50MHz*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/*调用库函数,初始化GPIOB*/
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*关闭LED灯*/
GPIO_SetBits(GPIOB, GPIO_Pin_5);
}
void Key_GPIO_Config()
{
/*定义一个GPIO_InitTypeDef类型的结构体*/
GPIO_InitTypeDef GPIO_InitStructure;
/*开启按键端口PC5的时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
/*选择要控制的GPIOC引脚*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
/*设置引脚模式为上拉输入*/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
//GPIO被设置为输入模式,不需要设置GPIO端口的最大输出速度。若设置了也没关系,GPIO_Init()函数会自动忽略//
/*调用库函数,初始化GPIOC*/
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
u8 Key_Scan(GPIO_TypeDef* GPIOx, u16 GPIO_Pin)
{
void Delay();
/*检测是否有按键按下*/
if(GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == 0)
{
/*消抖*/
Delay(1000);
if(GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == 0)
{
while(GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == 0);
return 0;
}
else
return 1;
}
else
return 1;
}
void Delay(uint32_t uCount)
{
for(;uCount>0;uCount--);
}
int main(void)
{
LED_GPIO_Config();
Key_GPIO_Config();
GPIO_ResetBits(GPIOB, GPIO_Pin_5);
while(1)
{
if(Key_Scan(GPIOC, GPIO_Pin_5) == 0)
{
/*LED反转*/
GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)((1-GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_5))));
}
}
}