Darren_pty

导航

4-gpioe IN event 库函数2 (nrf52832)

实验目的:

按下按键,对应LED点亮

 

实验代码:

(1)驱动函数

 1 #include "user_gpioe.h"
 2 #include "nrf_drv_gpiote.h"
 3 #include "pca10040.h"
 4 
 5 static uint8_t LEDS[] = LEDS_LIST;   //17 18 19 20
 6 static uint8_t Btns[] = BUTTONS_LIST;//13 14 15 16
 7 
 8 
 9 
10 void leds_init(void)
11 {
12     nrf_gpio_cfg_output(12);//按键初始化
13     nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(true);//配置LED引脚为输出模式
14     for(uint8_t i=0;i<4;i++)//将LED管脚置位
15     {
16       nrf_drv_gpiote_out_init(LEDS[i], &out_config);
17     }
18 }
19 
20 
21 //传入按键号,  返回按键的索引号,其与LED一一对应
22 static uint8_t button_pin_to_idx(uint8_t pin_num)
23 {
24   uint8_t ret = 0xFF;
25   uint8_t i;
26   for (i = 0; i < 4; ++i)
27   {
28       if (Btns[i] == pin_num)
29       {
30           ret = i;
31           break;
32       }
33   }
34   return ret;
35 }
36 
37 
38 void button_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
39 {
40   switch(pin)      //那个管脚产生回调
41   {
42   case BUTTON_1:
43   case BUTTON_2:
44   case BUTTON_3:
45   case BUTTON_4:
46     break;
47   default:
48     break;
49   }
50         
51       uint8_t idx = button_pin_to_idx(pin);//读取pin电平状态
52         if(action == GPIOTE_CONFIG_POLARITY_HiToLo) //按键按下
53          {
54             nrf_gpio_pin_clear(LEDS[idx]);//点亮LED
55          }
56 
57          
58 }
59 
60 
61 //GPIOTE功能
62 void button_hitolo_init(void)
63 {
64    nrf_drv_gpiote_in_config_t btn_config = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);//配置为下降沿触发
65      btn_config.pull = NRF_GPIO_PIN_PULLUP;//上拉
66     //配置btn引脚为边沿敏感
67      for(uint8_t i=0; i<BUTTONS_NUMBER; i++)
68     {
69       nrf_drv_gpiote_in_init(Btns[i], &btn_config, button_pin_handler);//引脚号  配置变量  BTN_pin_handler回调函数
70         nrf_drv_gpiote_in_event_enable(Btns[i], true);//中断使能
71     }
72 }
73 
74 void button_lotohi_init(void)
75 {
76    nrf_drv_gpiote_in_config_t btn_config = GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);//配置为下降沿触发
77      btn_config.pull = NRF_GPIO_PIN_PULLDOWN;//下拉
78     //配置btn引脚为边沿敏感
79      for(uint8_t i=0; i<BUTTONS_NUMBER; i++)
80     {
81       nrf_drv_gpiote_in_init(Btns[i], &btn_config, button_pin_handler);//引脚号  配置变量  BTN_pin_handler回调函数
82         nrf_drv_gpiote_in_event_enable(Btns[i], true);//中断使能
83     }
84 }

 

(2)mian函数

int main(void)
{
   nrf_drv_gpiote_init();//初始化gpioe外设
    
   button_hitolo_init();
   leds_init();

    while (true)
    {}
}

 

posted on 2020-11-03 09:49  Darren_pty  阅读(236)  评论(0编辑  收藏  举报