2016年2月3日 星期三

nRF51x22 Simple KeyScan

1. Test Schematic: (KEY1 = GPIO25)












2. Button.c
--------------------------------------------------------------------------------------------------------------------
  1.  
  2. #include "Button.h"
  3.  
  4. uint16_t DebounceKeyCnt = 0;
  5. uint8_t KeyValue = 0;
  6. uint8_t KeyState = 0;
  7.  
  8. void Button_Init(void)
  9. {
  10. nrf_gpio_pin_dir_set(Button_GPIO_Pin,NRF_GPIO_PIN_DIR_INPUT); //configurate pin direction to input
  11. nrf_gpio_cfg_input(Button_GPIO_Pin, NRF_GPIO_PIN_PULLUP); //pull-up
  12. }
  13.  
  14. uint8_t KeyScan(void)
  15. {
  16. KeyValue = nrf_gpio_pin_read(Button_GPIO_Pin);
  17. #ifdef high_active
  18. KeyValue = KeyValue; //active high
  19. #endif
  20. #ifdef low_active
  21. KeyValue = !(KeyValue); //active low
  22. #endif
  23. if(KeyValue >= 1)
  24. {
  25. DebounceKeyCnt++;
  26. if(DebounceKeyCnt >= 5)
  27. {
  28. DebounceKeyCnt = 0;
  29. KeyState = 1;
  30. }
  31. }
  32. else
  33. {
  34. DebounceKeyCnt = 0;
  35. KeyState = 0;
  36. }
  37. return(KeyState);
  38. }
  39.  


3. Button.h
---------------------------------------------------------------------------------------------------------------------
  1.  
  2.  
  3. #include "nrf_drv_gpiote.h"
  4.  
  5. #define Button_GPIO_Pin 25
  6.  
  7. #if 0
  8. #define high_active
  9. #else
  10. #define low_active
  11. #endif
  12.  
  13. void Button_Init(void);
  14. uint8_t KeyScan(void);
  15.  


沒有留言:

張貼留言