找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1375|回复: 1
打印 上一主题 下一主题
收起左侧

问题已解决STM32F103单片机差那1234按键能用5678按键不能用

[复制链接]
跳转到指定楼层
楼主
ID:63317 发表于 2020-11-27 08:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 WFX777888 于 2020-11-29 10:38 编辑

**文   件   名: 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)//L
{            
        
        
                    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)//O


        
             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[ i].Status)[ i]
    {
      case KEY_NO:
        //有按键按下
        if((key >> i) & 0x01)
        {
          Key_State[ i].Status = KEY_DOWN;[ i]
          Key_State[ i].Press_Time = Get_Count_Value();[ i]
        }
        break;
        
      case KEY_DOWN:
        if(((key >> i) & 0x01) == 0)
        {
          Key_State[ i].Status = KEY_UP;[ i]
        }
        break;
               
      case KEY_LONG:
        if(((key >> i) & 0x01) == 0)
        {
          Key_State[ i].Press_Time = 0;[ i]
          Key_State[ i].Status = KEY_NO;[ i]
        }
        break;  
    }


    if((Key_State[ i].Status == KEY_DOWN) || (Key_State[ i].Status == KEY_LONG))[ i][ i]
    {
      time = Get_Key_Press_Time(Key_State[ i].Press_Time);[ i]
      if(time >= TIME_PRESS_LONG)
      {
        //一直长按Long_Press_Handle
        Key_State[ i].Press_Time = Get_Count_Value();[ i]
        Key_State[ i].Status = KEY_LONG;[ i]
          Key_State[ i].Press_Time = 0;[ i]
          Key_State[ i].Status = KEY_NO;[ i]
        if(Key_State[ i].Long_Press_Handle)[ i]
        {
          Key_State[ i].Long_Press_Handle();[ i]
        }
      }         
    }
    else if(Key_State[ i].Status == KEY_UP)[ i]
    {
      //松开
     time = Get_Key_Press_Time(Key_State[ i].Press_Time);[ i]
   
      Key_State[ i].Press_Time = 0;[ i]
      Key_State[ i].Status = KEY_NO;[ i]
      if((time >= TIME_PRESS_SHORT) && (time < TIME_PRESS_LONG))
      {
        //短按Short_Press_Handle
        if(Key_State[ i].Short_Press_Handle)[ i]
        {
          Key_State[ i].Short_Press_Handle();[ i]
        }
      }
      else if(time >= TIME_PRESS_LONG)
      {
        //长按Long_Press_Handle
        if(Key_State[ i].Long_Press_Handle)[ i]
        {
          Key_State[ i].Long_Press_Handle();[ i]
        }
      }
    }
  }
}


#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);


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:790408 发表于 2020-11-28 10:34 来自手机 | 只看该作者
首先看一下I/O引脚是否定义和配对正确然后看看你要用的按键I/O是否被复用
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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