探索板,KEY_UP键没反应,应该是代码错了,但是查不出来。

2019-07-20 18:37发布

用了原子提供的demo,KEY_UP键并没有硬件损伤,
但是自己码的代码,KEY_UP键却用不了。其他键能用。我查看硬件原理和初始化好像都没有错,但还不行。求大神帮助!!
谢谢。(抱歉,我选用添加代码不成功,所以就直接粘贴复制代码了 。)
main.c内容:
#include<stm32f4xx.h>
#include<led.h>
#include<delay.h>
#include<key.h>
#include<beep.h>
int main()
{
  u8 key;
LED_Init();
BEEP_Init();
KEY_Init();
delay_init(168);
while(1)
{
  key=KEY_Scan(0);
     switch(key)
   {
    case WKUP_PRES:
     led0=!led0;
    break;
    case KEY0_PRES:
      beep=!beep;
    break;
    case KEY1_PRES:
     led1=!led1;
    break;
    case KEY2_PRES:
     led0=!led0;
       led1=!led1;
       break;   
   }
}
}
key.h文件:
#ifndef __KEY_H
#define __KEY_H

#define WK_UP   GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
#define KEY0    GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)
#define KEY1    GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)
#define KEY2    GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)
#define KEY0_PRES  1
#define KEY1_PRES  2
#define KEY2_PRES  3
#define WKUP_PRES  4
u8 KEY_Scan(u8);
void KEY_Init(void);
#endif

key.c文件:
#include<stm32f4xx.h>
#include<key.h>
#include<delay.h>
void KEY_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType=GPIO_OType_OD;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed=GPIO_Fast_Speed;
  GPIO_Init(GPIOE,&GPIO_InitStructure);

//GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN;//
//GPIO_InitStructure.GPIO_OType=GPIO_OType_OD;//
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_DOWN;
//GPIO_InitStructure.GPIO_Speed=GPIO_Fast_Speed;//
  GPIO_Init(GPIOA,&GPIO_InitStructure);
}
u8 KEY_Scan(u8 mode)
{
  static u8 key_up=1;
if(mode)
  key_up=1;
if(key_up&&(WK_UP==1||KEY0==0||KEY1==0||KEY2==0))
{
   delay_ms(15);
  key_up=0;
  if(WK_UP==1)return 4;
  else if(KEY0==0)return 1;
  else if(KEY1==0)return 2;
  else if(KEY2==0)return 3;
}
  else if(WK_UP==0&&KEY0==1&&KEY1==1&&KEY2==1)
   key_up=1;
return 0;
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
6条回答
zhlmtt
1楼-- · 2019-07-20 19:09
key 初始化里面没有使能A时钟 就是这个问题 我试过了
守候ocean
2楼-- · 2019-07-20 23:14
用仿真器下载程序的话注意设置的时候把Reset and run勾上,要不程序下载完就按一下板子上的复位键,我老犯这个毛病,不知道你的是不是这个问题,看代码没啥毛病啊,我也是菜鸟
喆吉
3楼-- · 2019-07-21 01:40
 精彩回答 2  元偷偷看……
守候ocean
4楼-- · 2019-07-21 02:06
 精彩回答 2  元偷偷看……
喆吉
5楼-- · 2019-07-21 02:33
守候ocean 发表于 2016-11-25 16:50
是有问题 我这也是

是因为没有对IO口A时钟使能,
喆吉
6楼-- · 2019-07-21 07:05
zhlmtt 发表于 2016-11-25 17:45
key 初始化里面没有使能A时钟 就是这个问题 我试过了

哈哈,感恩!!!

一周热门 更多>