1-key nrf52832

实验目的:

(1)触摸电容按键,4个LED电平翻转

(2)按下4个普通按键的其中之一,有源蜂鸣器响起

实验原理:

  

 

实验代码:

(1)普通按键key &&  电容按键Touch key 初始化函数

/*
KEY: 13-16  

TOUCH KEY:  12
*/

#define ENABLE_TOUCH_KEY  //touch key enable 

void Key_init(void)
{

#ifndef ENABLE_TOUCH_KEY
        nrf_gpio_cfg_output(12);//beep
        nrf_gpio_range_cfg_input(13, 16, NRF_GPIO_PIN_PULLUP);//key
#else
        nrf_gpio_cfg_input(12,NRF_GPIO_PIN_PULLUP);//touch key
#endif
    
}

(2)4个普通按键 code

//TOUCH KEY and BEEP share one pin, so need exchange short cap  
void Key_BEEP(void)
{
  if(!nrf_gpio_pin_read(14) || !nrf_gpio_pin_read(15) || !nrf_gpio_pin_read(16) || !nrf_gpio_pin_read(13))
        {
            nrf_delay_ms(10);// key shake off
            if(!nrf_gpio_pin_read(14) || !nrf_gpio_pin_read(15) || !nrf_gpio_pin_read(16) || !nrf_gpio_pin_read(13))
            {
                    nrf_gpio_pin_set(12);// open beep
                    nrf_delay_ms(100);
            }
      //wait key release 
            while(!nrf_gpio_pin_read(14) || !nrf_gpio_pin_read(15) || !nrf_gpio_pin_read(16) || !nrf_gpio_pin_read(13));
        }
        if(nrf_gpio_pin_read(14) || nrf_gpio_pin_read(15) || nrf_gpio_pin_read(16) || nrf_gpio_pin_read(13))
        nrf_gpio_pin_clear(12);//close beep
}

(3) touch key code

void TouchKey_LED(void)
{
 if(nrf_gpio_pin_read(12))
 {
     nrf_delay_ms(10);//key shake off
     if(nrf_gpio_pin_read(12))//touch key 
     {
            nrf_gpio_pin_toggle(17);
            nrf_gpio_pin_toggle(18);
            nrf_gpio_pin_toggle(19);
            nrf_gpio_pin_toggle(20);    
     }
     while(nrf_gpio_pin_read(12));//wait key release
 }
}

(4)touch key main function

int main(void)
{
    //led init
    nrf_gpio_range_cfg_output(17, 20);//led
    nrf_gpio_pin_set(17);
    nrf_gpio_pin_set(18);
    nrf_gpio_pin_set(19);
    nrf_gpio_pin_set(20);
    
//  EXIT_KEY_Init();
    Key_init();
    
  while(true)
  {
        TouchKey_LED();
//        Key_BEEP();
  }
}

Touch key实验视频:

https://www.bilibili.com/video/BV1ji4y177QZ/

普通IO Test

https://www.bilibili.com/video/BV1na4y1s7aC/

原文地址:https://www.cnblogs.com/darren-pty/p/13902148.html