实验目的:
(1)按下按键,产生外部中断,由中断服务函数使LED翻转
实验原理:
(1)GPIOE 寄存器:
(2)设置GPIOE的CONFIG寄存器,实现GPIOE与普通GPIO绑定
实验代码:
(1)External Interrupt Init function
//event void EXIT_KEY_Init(void) { nrf_gpio_cfg_input(13,NRF_GPIO_PIN_PULLUP);//key 1 nrf_gpio_cfg_input(14,NRF_GPIO_PIN_PULLUP);//key 2 NVIC_EnableIRQ(GPIOTE_IRQn);//Enable External Interrupt //key1 NRF_GPIOTE->CONFIG[0] = (13 << GPIOTE_CONFIG_PSEL_Pos) //IO port | (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos)//Mode -> event or task or disable |(GPIOTE_CONFIG_POLARITY_HiToLo << GPIOTE_CONFIG_POLARITY_Pos);//POLARITY HiToLo NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_IN0_Set << GPIOTE_INTENSET_IN0_Pos;// enable INT //key2 NRF_GPIOTE->CONFIG[1] = (GPIOTE_CONFIG_POLARITY_HiToLo << GPIOTE_CONFIG_POLARITY_Pos) | (14<< GPIOTE_CONFIG_PSEL_Pos) | (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos); NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_IN1_Set << GPIOTE_INTENSET_IN1_Pos; }
(2)
void GPIOTE_IRQHandler(void) { if ((NRF_GPIOTE->EVENTS_IN[0] == 1) && //judge Interrupt event (NRF_GPIOTE->INTENSET & GPIOTE_INTENSET_IN0_Msk))//meanwhile, enable INT0 { NRF_GPIOTE->EVENTS_IN[0] = 0; //clear INT flag nrf_delay_ms(10); //key shake off if(nrf_gpio_pin_read(13)== 0) { nrf_gpio_pin_toggle(17);//led toggle } } if ((NRF_GPIOTE->EVENTS_IN[1] == 1) && (NRF_GPIOTE->INTENSET & GPIOTE_INTENSET_IN1_Msk)) { NRF_GPIOTE->EVENTS_IN[1] = 0; //clear INT flag nrf_delay_ms(10); //key shake off nrf_gpio_pin_toggle(18); } }
实验视频:
https://www.bilibili.com/video/BV1Mp4y1k7Hh/