久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1850|回復: 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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 97视频成人| 欧美成人免费 | 欧美中文 | 伊人狠狠干 | 亚洲人va欧美va人人爽 | 亚洲精品av在线 | 日韩一区二区三区av | 久久久久一区二区三区 | 北条麻妃一区二区三区在线观看 | 午夜av电影院 | 亚洲国产精品激情在线观看 | 成人国内精品久久久久一区 | a级片播放| 一区二区三区四区日韩 | 91精品麻豆日日躁夜夜躁 | 在线亚州 | 中文字幕一区二区三区四区不卡 | 黄色毛片一级 | 91精品国产综合久久久久蜜臀 | 欧美在线一区二区三区四区 | av高清 | aaaa一级毛片 | 日韩在线免费 | 97超级碰碰 | 新疆少妇videos高潮 | 亚洲免费一区二区 | 日本午夜一区 | 久久国产精品无码网站 | 五月婷亚洲 | 久久天堂网 | 美女一区 | 日韩在线免费电影 | 欧美视频三级 | 黄网站色大毛片 | 久久精品久久久久久 | 日本高清视频在线播放 | 欧美日韩高清在线一区 | 黄网站涩免费蜜桃网站 | 一级黄色生活视频 | 国产免费自拍 | 综合国产 |