版權聲明:本版面文章皆爲原創、或參考其他技術網站、博客後自己動手做實驗所得,轉載請註明出處。
參考說明:參考http://blog.csdn.net/feilusia/article/details/50535963。
商務合作:[email protected]
易開嵌入式工作室
基於BLE-CC254x-1.3.2協議棧版本的CC2541應用開發已經做了一段時間,先後開發出幾塊藍牙模塊,並移植測試過透傳等案例,但並未對一些基礎的寄存器做過深入研究,今天開始會陸續進行基本的外設實驗,並通過本博客記錄實驗結果。
一、簡介
本篇博文將介紹基於SimpleBLEPeripheral添加自定義按鍵。
二、實驗平臺
IAR8.30
BLE1.0測試板
三、測試代碼
在工程的APP組裏,添加Key.c和Key.h兩個文件
Key.c 文件類容:
1. 頭文件、宏定義、函數聲明
#include <ioCC2540.h>
#include "Key.h"
/*********************宏定義************************/
//註冊時使用的宏
#define NO_TASK_ID 0xFF //沒有註冊時的任務id
#define NO_EVEN_ID 0x0000 //沒有註冊時的事件id
//中斷消抖時使用的宏
#define KEY_DEBOUNCE_VALUE 20 //消抖時間20ms
/*********************內部變量************************/
static uint8 registeredKeyTaskID = NO_TASK_ID;
static uint16 registeredKeyEvenID = NO_EVEN_ID;
/*********************函數聲明************************/
extern uint8 osal_start_timerEx( uint8 task_id, uint16 event_id, uint32 timeout_value );
2.按鍵初始化
根據原理圖,與按鍵相連引腳爲P1.6。
void Key_Init(void)
{
P1SEL &= ~BIT6; //P16設置爲IO口
P1DIR &= ~BIT6; //P16設置爲輸入
P1INP &= ~BIT6; //P1上拉/下拉模式
P2INP &= ~BIT6; //P1上拉
P1_6 = High; //P16拉高
P1IFG &= ~BIT6; //初始化P16中斷標誌位
PICTL |= (1 << 2); //下降沿觸發
P1IEN |= BIT6; //使能P16中斷
IEN2 |= (1 << 4); //允許P1口中斷;
}
3.按鍵事件任務號、事件號註冊函數
uint8 RegisterForKey(uint8 task_id, uint16 even_id)
{
// Allow only the first task
if ( registeredKeyTaskID == NO_TASK_ID )
{
registeredKeyTaskID = task_id;
}
else
return ( FALSE );
// Allow only the first even
if ( registeredKeyEvenID == NO_EVEN_ID )
{
registeredKeyEvenID = even_id;
}
else
return ( FALSE );
return ( TRUE );
}
4.按鍵檢測高低電平狀態
uint8 Key_Check_Pin(void)
{
if(P1 & (BIT6))
{
return KEY_LOOSEN;
}
else
{
return KEY_PRESS;
}
}
5.P1中斷入口
#pragma vector = P1INT_VECTOR
__interrupt void P1_ISR(void)
{
if(Key_Check_Pin() == KEY_PRESS)
{
osal_start_timerEx(registeredKeyTaskID, registeredKeyEvenID, KEY_DEBOUNCE_VALUE);
}
P1IFG = 0; //清中斷標誌
P1IF = 0; //清中斷標誌
}
Key.h 文件類容:
1.按鍵驅動頭文件,保護一些宏定義和外部函數聲明
#ifndef KEY_H
#define KEY_H
#include "hal_types.h"
#define BIT0 (1 << 0)
#define BIT1 (1 << 1)
#define BIT2 (1 << 2)
#define BIT3 (1 << 3)
#define BIT4 (1 << 4)
#define BIT5 (1 << 5)
#define BIT6 (1 << 6)
#define BIT7 (1 << 7)
#ifndef High
#define High 1
#define Low 0
#endif
//檢測io口狀態時使用的宏
#define KEY_LOOSEN 0x01
#define KEY_PRESS 0x00
/*********************函數聲明************************/
extern void Key_Init(void);
extern uint8 RegisterForKey(uint8 task_id, uint16 even_id);
extern uint8 Key_Check_Pin(void);
#endif
SimpleBLEPeripheral.c文件函數添加:
1.在SimpleBLEPeripheral_Init,添加按鍵初始化函數和註冊函數:
Key_Init();
RegisterForKey(simpleBLEPeripheral_TaskID, SBP_KEY_CHECK_PROCESS_EVT);
2.在SimpleBLEPeripheral_ProcessEvent中,添加事件處理函數:
//按鍵檢測處理事件
if ( events & SBP_KEY_CHECK_PROCESS_EVT )
{
//防止抖動,確定是按鍵
if(Key_Check_Pin() == KEY_PRESS)
{
//按鍵處理函數
GUA_Key_Process();
}
return (events ^ SBP_KEY_CHECK_PROCESS_EVT);
}
3.按鍵處理函數
這裏爲體現按鍵作用,用點亮和熄滅LED的方式來測試。
根據原理圖,使用DS1作爲按鍵測試的LED,直接翻轉P1.5的電平。
static void GUA_Key_Process(void)
{
//test
P1SEL &= ~BIT5; //設置爲IO口
P1DIR |= BIT5; //設置爲輸出
P1_5 = ~P1_5; //這裏測試按一次按鍵,就取反一次P1_5,方便觀察P1_5對應的led
//test
}
SimpleBLEPeripheral.c文件函數添加:
在defined symbol中,添加HAL_KEY=TRUE
四、編譯後下載
按鍵按下後,LED狀態改變,實驗成功。