Darren_pty

导航

5-gpioe port event 库函数 (nrf52832)

实验目的:

将普通GPIO管脚配置为GPIOE中断输入事件,能够绑定的只有8个通道,如果我们中断的数据量超过8个,多的中断无法处理,此时可以使用GPIOE模块的GPIOE PORT功能

使用GPIOE的port event功能控制LED

 

PS:使用几个中断,这里sdk_config.h需要设置对应的数值

 

 

 

实验代码:

(1)驱动函数

 1 #include "user_gpioe.h"
 2 #include "nrf_drv_gpiote.h"
 3 #include "pca10040.h"
 4 
 5 //将普通GPIO管脚配置为GPIOE中断输入事件,能够绑定的只有8个通道,
 6 //如果我们中断的数据量超过8个,多的中断无法处理,此时可以使用GPIOE模块的GPIOE PORT功能
 7 
 8 //PORT事件的产生是在 DETECT 信号的上升沿产生  GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);false--port event
 9 
10 static uint8_t LEDS[] = LEDS_LIST;   //17 18 19 20
11 static uint8_t Btns[] = BUTTONS_LIST;//13 14 15 16
12 
13 
14 
15 void leds_init(void)
16 {
17     nrf_gpio_cfg_output(12);//按键初始化
18     nrf_gpio_pin_clear(12);
19     
20     nrf_gpio_range_cfg_output(17,20);//LED初始化
21     nrf_gpio_pin_set(17);
22     nrf_gpio_pin_set(18);
23     nrf_gpio_pin_set(19);
24     nrf_gpio_pin_set(20);
25 }
26 
27 
28 //传入按键号,  返回按键的索引号,其与LED一一对应
29 static uint8_t button_pin_to_idx(uint8_t pin_num)
30 {
31   uint8_t ret = 0xFF;
32   uint8_t i;
33   for (i = 0; i < 4; ++i)
34   {
35       if (Btns[i] == pin_num)
36       {
37           ret = i;
38           break;
39       }
40   }
41   return ret;
42 }
43 
44 
45 void button_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
46 { 
47       uint8_t idx = button_pin_to_idx(pin);//读取pin电平状态
48       if(nrf_drv_gpiote_in_is_set(pin)) //按键按下
49         {
50           nrf_gpio_pin_toggle(LEDS[idx]);//关闭LED
51             nrf_gpio_pin_clear(12);
52         }
53         else
54         {
55             nrf_gpio_pin_clear(LEDS[idx]);//点亮LED
56             nrf_gpio_pin_set(12);
57         }     
58 }
59 
60 
61 //GPIOTE功能
62 void button_hitolo_init(void)
63 {
64     //配置SENSE模式,选择fales为sense配置
65    nrf_drv_gpiote_in_config_t btn_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(false);//配置为下降沿触发  false--port event
66      btn_config.pull = NRF_GPIO_PIN_PULLUP;//上拉
67     
68     //配置按键 绑定 POTR
69      for(uint8_t i=0; i<BUTTONS_NUMBER; i++)
70     {
71       nrf_drv_gpiote_in_init(Btns[i], &btn_config, button_pin_handler);//引脚号  配置变量  BTN_pin_handler回调函数
72         nrf_drv_gpiote_in_event_enable(Btns[i], true);//中断使能
73     }
74 }

(2)main

int main(void)
{
    leds_init();
    
    ret_code_t err_code;
    err_code = nrf_drv_gpiote_init();//初始化gpioe外设
    APP_ERROR_CHECK(err_code);


   button_hitolo_init();


    while (true)
    {}
}

 

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