按键查询和按键中断

2020-07-01  本文已影响0人  昨天剩下的一杯冷茶

代码下载地址:

https://download.csdn.net/download/qq_31806069/12566260

按键查询

int main(void)
{
    
    lfclk_config();
    rtc_config();
    init_led();
    init_key();
    
    while (true)
    {

        if (KEY1_STA()==0)
        {
            nrf_delay_ms(20);
            if (KEY1_STA()==0)
            {
                LED_TOG();
                while(KEY1_STA()==0){}
            }
        }
    }
}

按键中断

void exit_key_init(void)
{

    nrf_gpio_cfg_input(KEY1,NRF_GPIO_PIN_PULLUP);


    NVIC_EnableIRQ(GPIOTE_IRQn);

    NRF_GPIOTE->CONFIG[0] =  (GPIOTE_CONFIG_POLARITY_HiToLo << GPIOTE_CONFIG_POLARITY_Pos)
                             | (16 << GPIOTE_CONFIG_PSEL_Pos)
                             | (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos);
    NRF_GPIOTE->INTENSET  = GPIOTE_INTENSET_IN0_Set << GPIOTE_INTENSET_IN0_Pos;
}


void GPIOTE_IRQHandler(void)
{

    if ((NRF_GPIOTE->EVENTS_IN[0] == 1) &&
            (NRF_GPIOTE->INTENSET & GPIOTE_INTENSET_IN0_Msk))
    {
        NRF_GPIOTE->EVENTS_IN[0] = 0;
        nrf_delay_ms(20);
        if (KEY1_STA()==0)
        {
            LED_TOG();
        }
    }

}

上一篇下一篇

猜你喜欢

热点阅读