与开发板连接顺序一样, PE.2-PE.3连接按键KEY2-KEY0, LED0连接PB5,修改程序后不管有无按键,程序总在红色部分循环,灯都不受控制。。求解。。
下面分别是main.c,key.c,key.h函数
int main(void)
{
RCC_Configuration(); //系统时钟设置及外设时钟使能
Led_GPIO_Config();
Key_GPIO_Config();
while (1)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_5);//灯亮
if( Key_Scan(GPIOE,GPIO_Pin_3) == KEY_ON )
{
GPIO_SetBits(GPIOB,GPIO_Pin_5);//灯灭
}
if( Key_Scan(GPIOE,GPIO_Pin_4) == KEY_ON )
{
GPIO_ResetBits(GPIOB,GPIO_Pin_5);//灯亮
}
if( Key_Scan(GPIOE,GPIO_Pin_2) == KEY_ON )
{
GPIO_SetBits(GPIOB,GPIO_Pin_5);//先灭后亮
Delay(1000);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
}
}
}
void Delay(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
void RCC_Configuration(void)
{
SystemInit();
}
key.c函数
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "key.h"
void Key_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_3 |GPIO_Pin_4|GPIO_Pin_2 );
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //输入上拉
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_SetBits(GPIOE, GPIO_Pin_3);
GPIO_SetBits(GPIOE, GPIO_Pin_2); //
GPIO_SetBits(GPIOE, GPIO_Pin_4); //
void Led_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // LED输出引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
u8 Key_Scan(GPIO_TypeDef* GPIOx,u16 GPIO_Pin)
{
if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) ==KEY_ON )
{
Delay(0xffff);//延时去抖
if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON )
{
while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin) == KEY_ON);//松下按键
return KEY_ON ;
}
else
return KEY_OFF;
}
else
return KEY_OFF;
key.h如下
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef _KEY_H
#define _KEY_H
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#define KEY_ON 0
#define KEY_OFF 1
void Key_GPIO_Config(void);
void Led_GPIO_Config(void);
u8 Key_Scan(GPIO_TypeDef* GPIOx,u16 GPIO_Pin);
extern void Delay(__IO uint32_t nCount);
#endif |