OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3788|回复: 8

大神帮忙看看STM32F103单片机差那1234按键能用5678按键不能用

[复制链接]

3

主题

33

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
3529
金钱
3529
注册时间
2020-5-24
在线时间
230 小时
发表于 2020-11-27 08:41:55 | 显示全部楼层 |阅读模式
50金钱
本帖最后由 wfx777888 于 2020-11-29 10:21 编辑

问题已解决

**--------------文件信息-------------------------------------------------------
**文   件   名: Hal_Key.c
**描        述: 按键接口处理
**-----------------------------------------------------------------------------
******************************************************************************/
#define HAL_KEY_GLOBAL
#include "include.h"
#include "wifi.h"

typedef void (* KEY_HANDLE)(void);
typedef struct
{
  uint8_t Status;
  uint32_t Press_Time;
  KEY_HANDLE Short_Press_Handle;
  KEY_HANDLE Long_Press_Handle;
} KEY_STATE_T;

static KEY_STATE_T Key_State[MAX_KEY];
extern TYPE_BUFFER_S FlashBuffer;



/*****************************************************************************
函数名称 : Key1_LongPress_Handle
功能描述 : Key1长按处理函数
输入参数 : 无
返回参数 : 无
使用说明 : 无
*****************************************************************************/
static void Key1_LongPress_Handle(void)
{   

                     kg_ON();
mcu_dp_bool_update(DPID_KG,FlashBuffer.kg);

}

static void Key2_LongPress_Handle(void)
{            
        
        
                    kg1_ON();
mcu_dp_bool_update(DPID_KG1,FlashBuffer.kg1);
        
        
}

static void Key3_LongPress_Handle(void)
{
        
        
               kg2_ON();
mcu_dp_bool_update(DPID_KG2,FlashBuffer.kg2);
        
}
static void Key4_LongPress_Handle(void)
{

        
             kg3_ON();
mcu_dp_bool_update(DPID_KG3,FlashBuffer.kg3);
        
        
}

static void Key5_LongPress_Handle(void)
{

        
             wet_ON();
mcu_dp_bool_update(DPID_WET,FlashBuffer.wet);
}
        static void Key6_LongPress_Handle(void)
{
        
        
               kg2_ON();
mcu_dp_bool_update(DPID_KG2,FlashBuffer.kg2);
        
}
static void Key7_LongPress_Handle(void)
{

        
             kg3_ON();
mcu_dp_bool_update(DPID_KG3,FlashBuffer.kg3);
        
        
}

static void Key8_LongPress_Handle(void)
{

        
             wet_ON();
mcu_dp_bool_update(DPID_WET,FlashBuffer.wet);
        
        

}
/*****************************************************************************
函数名称 : KEY_Init
功能描述 : KEY初始化
输入参数 : 无
返回参数 : 无
使用说明 : 无
*****************************************************************************/
void KEY_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB2PeriphClockCmd(GPIO_KEY1_CLK | GPIO_KEY2_CLK | GPIO_KEY3_CLK | GPIO_KEY4_CLK| GPIO_KEY5_CLK | GPIO_KEY6_CLK | GPIO_KEY7_CLK | GPIO_KEY8_CLK , (FunctionalState)ENABLE);

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

  GPIO_InitStructure.GPIO_Pin = GPIO_KEY1_PIN;
  GPIO_Init(GPIO_KEY1_PORT, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_KEY2_PIN;
  GPIO_Init(GPIO_KEY2_PORT, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_KEY3_PIN;
  GPIO_Init(GPIO_KEY3_PORT, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_KEY4_PIN;
  GPIO_Init(GPIO_KEY4_PORT, &GPIO_InitStructure);
        

        GPIO_InitStructure.GPIO_Pin = GPIO_KEY5_PIN;
  GPIO_Init(GPIO_KEY5_PORT, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_KEY6_PIN;
  GPIO_Init(GPIO_KEY6_PORT, &GPIO_InitStructure);
        
        GPIO_InitStructure.GPIO_Pin = GPIO_KEY7_PIN;
  GPIO_Init(GPIO_KEY7_PORT, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_KEY8_PIN;
  GPIO_Init(GPIO_KEY8_PORT, &GPIO_InitStructure);
  //
  memset(Key_State,0,sizeof(Key_State));
  //
  Key_State[0].Long_Press_Handle = Key1_LongPress_Handle;   //Long_Press_Handle按钮长按
  Key_State[1].Long_Press_Handle = Key2_LongPress_Handle;
        Key_State[2].Long_Press_Handle = Key3_LongPress_Handle;
        Key_State[3].Long_Press_Handle = Key4_LongPress_Handle;
  Key_State[4].Long_Press_Handle = Key5_LongPress_Handle;   //Long_Press_Handle按钮长按
  Key_State[5].Long_Press_Handle = Key6_LongPress_Handle;
        Key_State[6].Long_Press_Handle = Key7_LongPress_Handle;
        Key_State[7].Long_Press_Handle = Key8_LongPress_Handle;
}
/*****************************************************************************
函数名称 : Get_Key
功能描述 : 读取按键
输入参数 : 无
返回参数 : ReadKey:按键值
使用说明 : 无
*****************************************************************************/
static uint8_t Get_Key(void)
{
   uint8_t ReadKey = 0;

  if(!GPIO_ReadInputDataBit(GPIO_KEY1_PORT,GPIO_KEY1_PIN))
  {
    ReadKey |= PRESS_KEY1;
  }

  if(!GPIO_ReadInputDataBit(GPIO_KEY2_PORT,GPIO_KEY2_PIN))
  {
    ReadKey |= PRESS_KEY2;
  }

  if(!GPIO_ReadInputDataBit(GPIO_KEY3_PORT,GPIO_KEY3_PIN))
  {
    ReadKey |= PRESS_KEY3;
  }  

  if(!GPIO_ReadInputDataBit(GPIO_KEY4_PORT,GPIO_KEY4_PIN))
  {
    ReadKey |= PRESS_KEY4;
  }

           if(!GPIO_ReadInputDataBit(GPIO_KEY1_PORT,GPIO_KEY5_PIN))
  {
    ReadKey |= PRESS_KEY5;
  }

  if(!GPIO_ReadInputDataBit(GPIO_KEY2_PORT,GPIO_KEY6_PIN))
  {
    ReadKey |= PRESS_KEY6;
  }

  if(!GPIO_ReadInputDataBit(GPIO_KEY3_PORT,GPIO_KEY7_PIN))
  {
    ReadKey |= PRESS_KEY7;
  }  

  if(!GPIO_ReadInputDataBit(GPIO_KEY4_PORT,GPIO_KEY8_PIN))
  {
    ReadKey |= PRESS_KEY8;
  }

  return ReadKey;
}
/*****************************************************************************
函数名称 : Get_Key_Press_Time
功能描述 : 获取按键按下时间
输入参数 : last_time:上次按下时间
返回参数 : 2次按键之间差值
使用说明 : 无
*****************************************************************************/
static uint32_t Get_Key_Press_Time(uint32_t last_time)
{
  uint32_t time;

  time = Get_Count_Value();
  if(time >= last_time)
  {
    time -= last_time;
  }
  else
  {
    //Time2溢出
    time += ~last_time;
  }

  return time;
}
/*****************************************************************************
函数名称 : Key_Scan
功能描述 : 扫描按键
输入参数 : 无
返回参数 : 无
使用说明 : 无
*****************************************************************************/
void Key_Scan(void)
{
  uint8_t i;
  uint32_t time;
  uint8_t key;

  key = Get_Key();
  for(i = 0; i < MAX_KEY; i ++)
  {
    switch(Key_State.Status)
    {
      case KEY_NO:
        //有按键按下
        if((key >> i) & 0x01)
        {
          Key_State.Status = KEY_DOWN;
          Key_State.Press_Time = Get_Count_Value();
        }
        break;

      case KEY_DOWN:
        if(((key >> i) & 0x01) == 0)
        {
          Key_State.Status = KEY_UP;
        }
        break;

      case KEY_LONG:
        if(((key >> i) & 0x01) == 0)
        {
          Key_State.Press_Time = 0;
          Key_State.Status = KEY_NO;
        }
        break;  
    }

    if((Key_State.Status == KEY_DOWN) || (Key_State.Status == KEY_LONG))
    {
      time = Get_Key_Press_Time(Key_State.Press_Time);
      if(time >= TIME_PRESS_LONG)
      {
        //一直长按Long_Press_Handle
        Key_State.Press_Time = Get_Count_Value();
        Key_State.Status = KEY_LONG;
          Key_State.Press_Time = 0;
          Key_State.Status = KEY_NO;
        if(Key_State.Long_Press_Handle)
        {
          Key_State.Long_Press_Handle();
        }
      }         
    }
    else if(Key_State.Status == KEY_UP)
    {
      //松开
     time = Get_Key_Press_Time(Key_State.Press_Time);

      Key_State.Press_Time = 0;
      Key_State.Status = KEY_NO;
      if((time >= TIME_PRESS_SHORT) && (time < TIME_PRESS_LONG))
      {
        //短按Short_Press_Handle
        if(Key_State.Short_Press_Handle)
        {
          Key_State.Short_Press_Handle();
        }
      }
      else if(time >= TIME_PRESS_LONG)
      {
        //长按Long_Press_Handle
        if(Key_State.Long_Press_Handle)
        {
          Key_State.Long_Press_Handle();
        }
      }
    }
  }
}


ifndef __HAL_KEY_H__
#define __HAL_KEY_H__

#define GPIO_KEY1_CLK    RCC_APB2Periph_GPIOA
#define GPIO_KEY1_PORT   GPIOA
#define GPIO_KEY1_PIN    GPIO_Pin_11

#define GPIO_KEY2_CLK    RCC_APB2Periph_GPIOA
#define GPIO_KEY2_PORT   GPIOA
#define GPIO_KEY2_PIN    GPIO_Pin_12

#define GPIO_KEY3_CLK    RCC_APB2Periph_GPIOA
#define GPIO_KEY3_PORT   GPIOA
#define GPIO_KEY3_PIN    GPIO_Pin_0

#define GPIO_KEY4_CLK    RCC_APB2Periph_GPIOA
#define GPIO_KEY4_PORT   GPIOA
#define GPIO_KEY4_PIN    GPIO_Pin_1

#define GPIO_KEY5_CLK    RCC_APB2Periph_GPIOA
#define GPIO_KEY5_PORT   GPIOA
#define GPIO_KEY5_PIN    GPIO_Pin_11

#define GPIO_KEY6_CLK    RCC_APB2Periph_GPIOA
#define GPIO_KEY6_PORT   GPIOA
#define GPIO_KEY6_PIN    GPIO_Pin_12

#define GPIO_KEY7_CLK    RCC_APB2Periph_GPIOA
#define GPIO_KEY7_PORT   GPIOA
#define GPIO_KEY7_PIN    GPIO_Pin_0

#define GPIO_KEY8_CLK    RCC_APB2Periph_GPIOA
#define GPIO_KEY8_PORT   GPIOA
#define GPIO_KEY8_PIN    GPIO_Pin_1


#define MAX_KEY          8
#define PRESS_KEY1      0x01
#define PRESS_KEY2      0x02
#define PRESS_KEY3      0x04
#define PRESS_KEY4      0x08
#define PRESS_KEY5      0x11
#define PRESS_KEY6      0x22
#define PRESS_KEY7      0x44
#define PRESS_KEY8      0x88


#define KEY_NO          0x00
#define KEY_DOWN        0x10         
#define KEY_UP          0x20   
#define KEY_LONG        0x40

#define TIME_PRESS_LONG         (3 * 1000)
#define TIME_PRESS_SHORT        50


/*****************************************************************************
函数名称 : Get_RGB2_State
功能描述 : RGB2状态
输入参数 : 无
返回参数 : RGB2开关状态
使用说明 : 无
*****************************************************************************/
uint8_t Get_RGB2_State(void);

/*****************************************************************************
函数名称 : KEY_Init
功能描述 : KEY初始化
输入参数 : 无
返回参数 : 无
使用说明 : 无
*****************************************************************************/
void KEY_Init(void);

/*****************************************************************************
函数名称 : Key_Scan
功能描述 : 扫描按键
输入参数 : 无
返回参数 : 无
使用说明 : 无
*****************************************************************************/
void Key_Scan(void);

#endif



差在这里问题

差在这里问题

最佳答案

查看完整内容[请看2#楼]

谢谢朋友问题已解决
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

3

主题

33

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
3529
金钱
3529
注册时间
2020-5-24
在线时间
230 小时
 楼主| 发表于 2020-11-27 08:41:56 | 显示全部楼层
游走在01的海洋 发表于 2020-11-27 16:40
如果是原子的板子,说明一下;如果不是,还要电路原理图(芯片和按键部分的)

谢谢朋友问题已解决
回复

使用道具 举报

6

主题

211

帖子

0

精华

高级会员

Rank: 4

积分
833
金钱
833
注册时间
2019-12-17
在线时间
157 小时
发表于 2020-11-27 16:07:17 | 显示全部楼层
你这样没几个人看的,把工程发出来
回复

使用道具 举报

1

主题

77

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
388
金钱
388
注册时间
2020-11-7
在线时间
44 小时
发表于 2020-11-27 16:37:59 | 显示全部楼层
或者主要配置截图发上来,这样看,会看晕的,最好实验现象也拍张图,将问题描述清楚
回复

使用道具 举报

1

主题

77

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
388
金钱
388
注册时间
2020-11-7
在线时间
44 小时
发表于 2020-11-27 16:40:19 | 显示全部楼层
如果是原子的板子,说明一下;如果不是,还要电路原理图(芯片和按键部分的)
回复

使用道具 举报

6

主题

211

帖子

0

精华

高级会员

Rank: 4

积分
833
金钱
833
注册时间
2019-12-17
在线时间
157 小时
发表于 2020-11-27 17:01:41 | 显示全部楼层
你按键1234和按键5678io口一模一样了
另外问的时候最好说清楚实验现象,把原理图,工程文件都打包到附件,直接贴代码 没几个人会看的。
未命名1606467428.png
回复

使用道具 举报

3

主题

33

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
3529
金钱
3529
注册时间
2020-5-24
在线时间
230 小时
 楼主| 发表于 2020-11-29 10:22:49 | 显示全部楼层
y284858 发表于 2020-11-27 16:07
你这样没几个人看的,把工程发出来

谢谢问题已解决
回复

使用道具 举报

3

主题

33

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
3529
金钱
3529
注册时间
2020-5-24
在线时间
230 小时
 楼主| 发表于 2020-11-29 10:23:11 | 显示全部楼层
y284858 发表于 2020-11-27 17:01
你按键1234和按键5678io口一模一样了,
另外问的时候最好说清楚实验现象,把原理图,工程文件都打包到附 ...

谢谢问题已解决谢谢
回复

使用道具 举报

3

主题

33

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
3529
金钱
3529
注册时间
2020-5-24
在线时间
230 小时
 楼主| 发表于 2020-11-29 10:25:21 | 显示全部楼层
y284858 发表于 2020-11-27 16:07
你这样没几个人看的,把工程发出来

谢谢弄好了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-23 02:00

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表