?? bsp_button.c
字號:
/*
*********************************************************************************************************
*
* 模塊名稱 : 按鍵驅(qū)動(dòng)模塊
* 文件名稱 : bsp_button.c
* 版 本 : V2.0
* 說 明 : 實(shí)現(xiàn)按鍵的檢測,具有軟件濾波機(jī)制,可以檢測如下事件:
* (1) 按鍵按下
* (2) 按鍵彈起
* (3) 長按鍵
* (4) 長按時(shí)自動(dòng)連發(fā)
* (5) 組合鍵
*
* 修改記錄 :
* 版本號 日期 作者 說明
* v0.1 2009-12-27 armfly 創(chuàng)建該文件,ST固件庫版本為V3.1.2
* v1.0 2011-01-11 armfly ST固件庫升級到V3.4.0版本。
* v2.0 2011-10-16 armfly ST固件庫升級到V3.5.0版本。
*
* Copyright (C), 2010-2011, 安富萊電子 www.armfly.com
*
*********************************************************************************************************
*/
#include "stm32f10x.h"
#include <stdio.h>
#include "bsp_button.h"
static BUTTON_T s_BtnUser; /* USER 鍵 */
static BUTTON_T s_BtnTamper; /* TAMPER 鍵 */
static BUTTON_T s_BtnWakeUp; /* WAKEUP 鍵 */
static BUTTON_T s_BtnUp; /* 搖桿UP鍵 */
static BUTTON_T s_BtnDown; /* 搖桿DOWN鍵 */
static BUTTON_T s_BtnLeft; /* 搖桿LEFT鍵 */
static BUTTON_T s_BtnRight; /* 搖桿RIGHT鍵 */
static BUTTON_T s_BtnOk; /* 搖桿OK鍵 */
static BUTTON_T s_BtnUserTamper;/* 組合鍵,USER和TAMPER鍵 */
static KEY_FIFO_T s_Key; /* 按鍵FIFO變量,結(jié)構(gòu)體 */
static void bsp_InitButtonVar(void);
static void bsp_InitButtonHard(void);
static void bsp_DetectButton(BUTTON_T *_pBtn);
/*
安富萊STM32F103ZE-EK 按鍵口線分配:
USER鍵 : PG8 (低電平表示按下)
TAMPEER鍵 : PC13 (低電平表示按下)
WKUP鍵 : PA0 (!!!高電平表示按下)
搖桿UP鍵 : PG15 (低電平表示按下)
搖桿DOWN鍵 : PD3 (低電平表示按下)
搖桿LEFT鍵 : PG14 (低電平表示按下)
搖桿RIGHT鍵: PG13 (低電平表示按下)
搖桿OK鍵 : PG7 (低電平表示按下)
定義函數(shù)判斷按鍵是否按下,返回值1 表示按下,0表示未按下
*/
static uint8_t IsKeyDownUser(void) {if (GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_8) == Bit_SET) return 0; return 1;}
static uint8_t IsKeyDownTamper(void) {if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13) == Bit_SET) return 0; return 1;}
static uint8_t IsKeyDownWakeUp(void) {if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_SET) return 1; return 0;}
static uint8_t IsKeyDownUp(void) {if (GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_15) == Bit_SET) return 0; return 1;}
static uint8_t IsKeyDownDown(void) {if (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_3) == Bit_SET) return 0; return 1;}
static uint8_t IsKeyDownLeft(void) {if (GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_14) == Bit_SET) return 0; return 1;}
static uint8_t IsKeyDownRight(void) {if (GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_13) == Bit_SET) return 0; return 1;}
static uint8_t IsKeyDownOk(void) {if (GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_7) == Bit_SET) return 0; return 1;}
static uint8_t IsKeyDownUserTamper(void) {if (IsKeyDownUser() && IsKeyDownTamper()) return 1; return 0;} /* 組合鍵 */
/*
*********************************************************************************************************
* 函 數(shù) 名: bsp_InitButton
* 功能說明: 初始化按鍵
* 形 參:無
* 返 回 值: 無
*********************************************************************************************************
*/
void bsp_InitButton(void)
{
bsp_InitButtonVar(); /* 初始化按鍵變量 */
bsp_InitButtonHard(); /* 初始化按鍵硬件 */
}
/*
*********************************************************************************************************
* 函 數(shù) 名: bsp_PutKey
* 功能說明: 將1個(gè)鍵值壓入按鍵FIFO緩沖區(qū)。可用于模擬一個(gè)按鍵。
* 形 參:_KeyCode : 按鍵代碼
* 返 回 值: 無
*********************************************************************************************************
*/
void bsp_PutKey(uint8_t _KeyCode)
{
s_Key.Buf[s_Key.Write] = _KeyCode;
if (++s_Key.Write >= KEY_FIFO_SIZE)
{
s_Key.Write = 0;
}
}
/*
*********************************************************************************************************
* 函 數(shù) 名: bsp_GetKey
* 功能說明: 從按鍵FIFO緩沖區(qū)讀取一個(gè)鍵值。
* 形 參:無
* 返 回 值: 按鍵代碼
*********************************************************************************************************
*/
uint8_t bsp_GetKey(void)
{
uint8_t ret;
if (s_Key.Read == s_Key.Write)
{
return KEY_NONE;
}
else
{
ret = s_Key.Buf[s_Key.Read];
if (++s_Key.Read >= KEY_FIFO_SIZE)
{
s_Key.Read = 0;
}
return ret;
}
}
/*
*********************************************************************************************************
* 函 數(shù) 名: bsp_KeyState
* 功能說明: 讀取按鍵的狀態(tài)
* 形 參:無
* 返 回 值: 無
*********************************************************************************************************
*/
uint8_t bsp_KeyState(uint8_t _ucKeyID)
{
uint8_t ucState = 0;
switch (_ucKeyID)
{
case KID_TAMPER:
ucState = s_BtnTamper.State;
break;
case KID_WAKEUP:
ucState = s_BtnWakeUp.State;
break;
case KID_USER:
ucState = s_BtnUser.State;
break;
case KID_JOY_UP:
ucState = s_BtnUp.State;
break;
case KID_JOY_DOWN:
ucState = s_BtnDown.State;
break;
case KID_JOY_LEFT:
ucState = s_BtnLeft.State;
break;
case KID_JOY_RIGHT:
ucState = s_BtnRight.State;
break;
case KID_JOY_OK:
ucState = s_BtnOk.State;
break;
}
return ucState;
}
/*
*********************************************************************************************************
* 函 數(shù) 名: bsp_InitButtonHard
* 功能說明: 初始化按鍵硬件
* 形 參:無
* 返 回 值: 無
*********************************************************************************************************
*/
static void bsp_InitButtonHard(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*
安富萊STM32F103ZE-EK 按鍵口線分配:
USER鍵 : PG8 (低電平表示按下)
TAMPEER鍵 : PC13 (低電平表示按下)
WKUP鍵 : PA0 (!!!高電平表示按下)
搖桿UP鍵 : PG15 (低電平表示按下)
搖桿DOWN鍵 : PD3 (低電平表示按下)
搖桿LEFT鍵 : PG14 (低電平表示按下)
搖桿RIGHT鍵: PG13 (低電平表示按下)
搖桿OK鍵 : PG7 (低電平表示按下)
*/
/* 第1步:打開GPIOA GPIOC GPIOD GPIOF GPIOG的時(shí)鐘
注意:這個(gè)地方可以一次性全打開
*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC
| RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOG, ENABLE);
/* 第2步:配置所有的按鍵GPIO為浮動(dòng)輸入模式(實(shí)際上CPU復(fù)位后就是輸入狀態(tài)) */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); /* PA0 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOC, &GPIO_InitStructure); /* PC13 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOD, &GPIO_InitStructure); /* PD3 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_13
| GPIO_Pin_14 | GPIO_Pin_15;
GPIO_Init(GPIOG, &GPIO_InitStructure); /* PG7,8,13,14,15 */
}
/*
*********************************************************************************************************
* 函 數(shù) 名: bsp_InitButtonVar
* 功能說明: 初始化按鍵變量
* 形 參:strName : 例程名稱字符串
* strDate : 例程發(fā)布日期
* 返 回 值: 無
*********************************************************************************************************
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -