This blog post last modified: January 06, 2017, 11:06.
First, Introduction
This article takes simplebleperipheral as an example to introduce how to customize the normal IO port (P12) as a key.
Note: This article adds the key method does not conflict with the protocol stack's key, the protocol stack to carry the key to still be able to use normally.
second, the experimental platform
Protocol stack version: ble-cc254x-1.4.0
Compiling software: IAR 8.20.2
Hardware platform: Smart RF (main chip CC2541)
three . Copyright notice
Bo Main: Sweet melon
Statement: Water do not forget to dig well people, reproduced please indicate the source.
Original address: Http://blog.csdn.NET/feilusia
Contact Way: 897503845@qq.com
Melon ble CC2541 Group: 127442605
Melon ble CC2640 Group: 557278427
Melon ble android Group: 541462902 melon Single chip computer Stm8/stm32 group: 164311667
Sweet Big melon Shop (Taobao): https://shop217632629.taobao.com/?spm=2013.1.1000126.d21.hd2o8i
Four, the experiment Premise 1, before the step of this article, please read the following blog: No
2, before the steps of this article, please implement the following blog:
Temporarily No
v. Basic knowledge
Temporarily No
Vi. Steps of the experiment
1, write and add a custom button driver
1 Write a button driver key.c (stored in the "... \ble-cc254x-1.4.0\projects\ble\simplebleperipheral\source\gua" path)
NAME:KEY.C/
/introduce: Key driver//author: Sweet melon//changetime:2016.04.14//email:897503845@qq.com #include <ioCC2540.h> #in Clude "Key.h"/********************* macros define the macro #define to use for ************************///Registration NO_TASK_ID 0xFF///No registered task ID #define NO_EVEN_ID 0x0000////No Event ID//No registration Define Key_debounce_value 20//dither time 20ms #ifndef false #define FALSE 0 #endif #ifndef true #defin E true 1 #endif typedef signed CHAR int8; !< signed 8 bit integer typedef unsigned char uint8; !< Unsigned 8 bit integer typedef signed short int16; !< signed bit integer typedef unsigned short uint16; !< Unsigned bit integer typedef signed long int32; !< signed bit integer typedef unsigned long uint32; !< Unsigned bit integer/********************* internal variable ************************/static U8 Registeredkeytaski
D = no_task_id;
static U16 Registeredkeyevenid = no_even_id; /********************* function declaration ************************/extern uint8 Osal_start_timerex (uint8 task_id, UInt16 event_id,
UInt32 timeout_value); Name:key_init//in Troduce: Key to initialize//parameter:none//return:none//author: Sweet melon//email : 897503845@qq.com//changetime:2016.04.14//***************************************
void Key_init (void) {P1sel &= ~ (1 << 2);//p12 set to IO port P1dir &= ~ (1 << 2); P12 set toInput P1INP &= ~ (1 << 2); P1 pull down mode p2inp &= ~ (1 << 6); P1 p1_2 = 1; P12 P1IFG &= ~ (1 << 2); Initialize P12 interrupt flag bit Pictl |= (1 << 1); Descent along the triggering P1ien |= (1 << 2); Enable P12 interrupt IEN2 |= (1 << 4);
Allow P1 port interruption; }//******************************************************************************//name:registerfo Rkey//introduce: Register task number, process event Number//parameter:task_id: Task ID//EVEN_ID: Event ID//return: True: Registration Successful//flase: Registration unsuccessful//author: Sweet melon//email:897503845@ Qq.com//changetime:2016.04.14//************************************************************** U8 Registerforkey (U8 task_id, U16 even_id) {//Allow only the ' the ' the ' only ' if (registeredkeyt
Askid = = no_task_id) {registeredkeytaskid = task_id; }
else return (false);
Allow only the even if (Registeredkeyevenid = = no_even_id) {Registeredkeyevenid = even_id;
else return (false);
return (true); }//******************************************************************************//name:key_check_ Pin//introduce: button to detect the high and low level status//parameter:none//return:key_loosen: At this time no key pressed// Key_press: At this point press the//author: Sweet melon//email:897503845@qq.com//changetime: 2016.04.14//****************************************************************************** U8 Key_Che
Ck_pin (void) {if (P1 & (1 << 2)) {return key_loosen;
else {return key_press;
}}//******************************************************************************//name:p1_isr Introduce: P1 's interrupt entrance//parameter:none//return:none//author: Sweet big melon//email: 897503845@qq.com//changetime:2016.04.14//************************************************* #pragma vector = P1int_vector __interrupt void P1_isr (void) {if (key_chec
K_pin () = = key_press) {Osal_start_timerex (Registeredkeytaskid, Registeredkeyevenid, Key_debounce_value); } P1IFG = 0; Clear Interrupt Sign p1if = 0; Clear Break Sign}
2 Write a button header file Key.h
(Stored under "... \ble-cc254x-1.4.0\projects\ble\simplebleperipheral\source\gua" path)
//name: Key.h
// Introduce: key-Driven header file
//author: sweet big melon
//changetime: 2016.04.14
//email: 897503845@ qq.com
//******************************************************************************
#ifndef KEY_H
#define KEY_H
/********************* macro definition ************************/
#ifndef U8
typedef unsigned char U8;
#endif
#ifndef U16
typedef unsigned short U16;
macro
#define Key_loosen 0x01
#define Key_press 0x00 for #endif//IO Port status detection
/************ function declaration ************************/
extern void key_init (void);
extern U8 Registerforkey (U8 task_id, U16 even_id);
extern U8 key_check_pin (void);
#endif
3) Add key.c and Key.h to the project
4 Add key to drive source file path in IAR settings
$PROJ _dir$\. \.. \simplebleperipheral\source\gua
2. Define a key event
1 defines a key event (in the simplebleperipheral_processevent of SIMPLEBLEPERIPHERAL.C)
Press the key to detect the processing event
if (Events & Sbp_key_check_process_evt)
{
//prevent jitter, OK is the key
if (key_check_pin () = = Key_ Press)
{
//Key processing function
gua_key_process ();
}
Return (events ^ sbp_key_check_process_evt);
2 A macro that defines a key event (in SimpleBLEPeripheral.h)
#define SBP_KEY_CHECK_PROCESS_EVT 0x1000 //Key detection handling events
3. Define and declare a key-handling function (SIMPLEBLEPERIPHERAL.C)
1 defines a key-handling function (simplebleperipheral.c )
//name: gua_key_ Process
//introduce: key processing function
//parameter: none
//return: none
//author: Sweet Big melon
//email: 897503845@qq.com
//changetime: 2016.04.14
//*************************
static void gua_key_process (void)
{
//test
P1sel &= ~ (1 << 0); Set to IO port
p1dir |= (1 << 0); Set to output
p1_0 = ~p1_0; Here the test presses the key once, takes back once p1_0, the convenient observation p1_0 corresponding LED
//test
}
2 Declare a key-handling function ( in SIMPLEBLEPERIPHERAL.C )
static void gua_key_process (void); Key processing function
4, use the key in the application layer
1) Key initialization (in SIMPLEBLEPERIPHERAL.C simplebleperipheral_init)
Press the key to initialize the
key_init ();
Registerforkey (Simplebleperipheral_taskid, sbp_key_check_process_evt);
2 Add key driver header file in Application layer code (SIMPLEBLEPERIPHERAL.C)
GUA
#include "Key.h"
//gua
Vii. Matters needing attention
Temporarily No
viii. Results of the experiment
Do not have a button on hand, so take a jumper, one end of the GND, the other end of the constant touch P12 pin, you will find P10 corresponding LED1 will be constantly lit up with the touch.
Therefore the experiment succeeds, realizes the P12 key to trigger P10 the led to extinguish.