【NRF51822】协议栈下按键的使用--外部中断


1.目的

   nrf51822外部中断

2.分析

    在实际应用中经常要用到外部中断,比如按键唤醒。

3.平台:

协议栈版本:SDK10.0.0

编译软件:keil 5.12

硬件平台:微雪开发板nrf51822

例子:SDK 10.0.0\

1.目的

   nrf51822外部中断

2.分析

    在实际应用中经常要用到外部中断,比如按键唤醒。在这里设置5个外部按键中断

3.平台:

协议栈版本:SDK10.0.0

编译软件:keil 5.12

硬件平台:微雪开发板nrf51822

例子:SDK 10.0.0\examples\ble_peripheral\ble_app_hrs\pca10028\s110\arm4

4.步骤

   1.首先配置5个端口,第五个端口是添加的添加代码如红色方框



2.bsp_init()修改为蓝色部分改为红色部分



3.中断事件配置如下。不知道为什么GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS 要设置按键中断的个数。。不知道为什么


4.bsp_event_handle()添加代码如下:


  
  
  1. /**@brief Function for handling events from the BSP module.
  2. *
  3. * @param[in] event Event generated by button press.
  4. */
  5. void bsp_event_handler(bsp_event_t event)
  6. {
  7. uint32_t err_code;
  8. switch (event)
  9. {
  10. case BSP_EVENT_SLEEP:
  11. sleep_mode_enter();
  12. break;
  13. case BSP_EVENT_DISCONNECT:
  14. err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  15. if (err_code != NRF_ERROR_INVALID_STATE)
  16. {
  17. APP_ERROR_CHECK(err_code);
  18. }
  19. break;
  20. case BSP_EVENT_WHITELIST_OFF:
  21. err_code = ble_advertising_restart_without_whitelist();
  22. if (err_code != NRF_ERROR_INVALID_STATE)
  23. {
  24. APP_ERROR_CHECK(err_code);
  25. }
  26. break; case BSP_EVENT_KEY_0:
  27. break;
  28. case BSP_EVENT_KEY_1: //添加代码
  29. break;
  30. case BSP_EVENT_KEY_2: //添加代码
  31. break;
  32. case BSP_EVENT_KEY_3: //添加代码
  33. break;
  34. case BSP_EVENT_KEY_4: //添加代码
  35. while( 1);
  36. break;
  37. default:
  38. break;
  39. }
  40. }

5。download程序。然后安按键hey1.跳到BSP_EVENT_KYEY4 :while(1)里面。ok。不过发现有些按键定义的事件发生了变化如图:初始化按键事件后


查看下m_events_list的值,和预定义的一样。


但是程序执行到这里的时候,发生了变化.

 


原来 bsp_btn_ble_init(NULL, &startup_event);里面又把一些给设置了一边


  
  
  1. uint32_t bsp_btn_ble_init( bsp_btn_ble_error_handler_t error_handler, bsp_event_t * p_startup_bsp_evt)
  2. {
  3. uint32_t err_code = NRF_SUCCESS;
  4. m_error_handler = error_handler;
  5. if (p_startup_bsp_evt != NULL)
  6. {
  7. err_code = startup_event_extract(p_startup_bsp_evt);
  8. RETURN_ON_ERROR(err_code);
  9. }
  10. if (m_num_connections == 0)
  11. {
  12. err_code = advertising_buttons_configure();
  13. }
  14. return err_code;
  15. }



  
  
  1. static uint32_t advertising_buttons_configure()
  2. {
  3. uint32_t err_code;
  4. err_code = bsp_event_to_button_action_assign(BTN_ID_DISCONNECT,
  5. BTN_ACTION_DISCONNECT,
  6. BSP_EVENT_DEFAULT);
  7. RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);
  8. err_code = bsp_event_to_button_action_assign(BTN_ID_WHITELIST_OFF,
  9. BTN_ACTION_WHITELIST_OFF,
  10. BSP_EVENT_WHITELIST_OFF);
  11. RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);
  12. err_code = bsp_event_to_button_action_assign(BTN_ID_SLEEP,
  13. BTN_ACTION_SLEEP,
  14. BSP_EVENT_SLEEP);
  15. RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);
  16. return NRF_SUCCESS;
  17. }


上面可以知道,对一个按键可以同时设置按下,长按,释放3个事件。每个动作产生都会产生对应的事件。如对同一个端口设置长按 短按,释放事件。。

 


  
  
  1. err_code = bsp_event_to_button_action_assign(num, BSP_BUTTON_ACTION_LONG_PUSH, BSP_EVENT_KEY_4);
  2. err_code = bsp_event_to_button_action_assign(num, BSP_BUTTON_ACTION_PUSH, BSP_EVENT_KEY_4);
  3. err_code = bsp_event_to_button_action_assign(num, BSP_BUTTON_ACTION_RELEASE, BSP_EVENT_KEY_4);


转载自:https://blog.csdn.net/a369000753/article/details/51314849

猜你喜欢

转载自blog.csdn.net/p1279030826/article/details/86497164
今日推荐