2016年2月3日 星期三

nRF51x22 Simple KeyScan

1. Test Schematic: (KEY1 = GPIO25)












2. Button.c
--------------------------------------------------------------------------------------------------------------------

#include "Button.h"

uint16_t  DebounceKeyCnt = 0;
uint8_t   KeyValue = 0; 
uint8_t   KeyState = 0;

void Button_Init(void)
{
 nrf_gpio_pin_dir_set(Button_GPIO_Pin,NRF_GPIO_PIN_DIR_INPUT);   //configurate pin direction to input
 nrf_gpio_cfg_input(Button_GPIO_Pin, NRF_GPIO_PIN_PULLUP); //pull-up
}

uint8_t KeyScan(void)
{
 KeyValue = nrf_gpio_pin_read(Button_GPIO_Pin);
 
 #ifdef high_active
  KeyValue = KeyValue;    //active high
 #endif
 
 #ifdef low_active
  KeyValue = !(KeyValue); //active low
 #endif
  
 if(KeyValue >= 1)
 {
  DebounceKeyCnt++;
 
                if(DebounceKeyCnt >= 5)
                {
   DebounceKeyCnt = 0;  
   KeyState = 1;
                }    
 }
 else
 {
  DebounceKeyCnt = 0;
  KeyState = 0;   
 } 
        
 return(KeyState);
}



3. Button.h
---------------------------------------------------------------------------------------------------------------------


#include "nrf_drv_gpiote.h"

#define Button_GPIO_Pin 25

#if 0
 #define high_active
#else
 #define low_active
#endif

void Button_Init(void);
uint8_t KeyScan(void);



沒有留言:

張貼留言