一个STM32的入门,按键功能使用例子,GPIOB13连接LED,GPIOB1按键长按3秒进行软件复位,GPIOB15按键控制LED的开关。
#include "stm32f10x.h"
void GPIO_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_13);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//上拉输入
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void RCC_Config()
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
}
void delay_ms(u16 time)
{
u16 i=0;
while(time--)
{
i=12000;
while(i--);
}
}
int main(void)
{
u16 cont=0;
u16 time=0;
SystemInit();
RCC_Config();
GPIO_Config();
while(1)
{
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)
{
delay_ms(10);
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)
cont++;
if(cont%2)
GPIO_SetBits(GPIOB,GPIO_Pin_13);
else
GPIO_ResetBits(GPIOB,GPIO_Pin_13);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0);
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==0)
{
delay_ms(10);
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==0)
{
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==0)
{
delay_ms(10);
time++;
}
if(time>300)
{
__set_FAULTMASK(1);//关闭中断
NVIC_SystemReset();//软件复位
}
}
time=0;
}
}
}