본문 바로가기

Programing/ Nordic(BLE)

외부 인터럽트 & 타이머 인터럽트를 이용한 롱버튼 구현

timer 예제 이용해서 수정하였습니다.

버튼을 3초이상 누르면 불이켜지고 다시 3초 이상 누르면 불이 꺼집니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "bsp.h"
 
#include "nrf_drv_timer.h"
#include "bsp.h"
#include "app_error.h"
 
#define LED 22
#define BUTTON 16
 
const nrf_drv_timer_t TIMER = NRF_DRV_TIMER_INSTANCE(0);
 
 
void GPIOTE_IRQHandler(void)
{
    // Event causing the interrupt must be cleared.
    if ((NRF_GPIOTE->EVENTS_IN[0== 1) &&
        (NRF_GPIOTE->INTENSET & GPIOTE_INTENSET_IN0_Msk))
    {
        NRF_GPIOTE->EVENTS_IN[0= 0;
                if(nrf_gpio_pin_read(BUTTON))
                {
                        //nrf_gpio_pin_set(LED);
                        nrf_drv_timer_disable(&TIMER);
                }
                else
                {
                        //nrf_gpio_pin_clear(LED);
                        nrf_drv_timer_enable(&TIMER);
                }
    }
}
 
void timer_event_handler(nrf_timer_event_t event_type, void* p_context)
{
    
    switch(event_type)
    {
        case NRF_TIMER_EVENT_COMPARE0:
                    nrf_drv_timer_disable(&TIMER);
            nrf_gpio_pin_toggle(LED);
            break;
        
        default:
            //Do nothing.
            break;
    }    
}
 
 
static void gpio_init(void)
{
        nrf_gpio_cfg_output(LED);
    nrf_gpio_cfg_input(BUTTON, NRF_GPIO_PIN_PULLUP);
    nrf_gpio_pin_clear(LED);
    
    // GPIO interrupt:
    NVIC_EnableIRQ(GPIOTE_IRQn);
    NRF_GPIOTE->CONFIG[0= (GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos)
                            | (BUTTON << GPIOTE_CONFIG_PSEL_Pos)
                            | (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos);
    NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_IN0_Set << GPIOTE_INTENSET_IN0_Pos;
}
 
 
int main(void)
{
    uint32_t time_ms = 3000//Time(in miliseconds) between consecutive compare events.
    uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;
    
    gpio_init();
    
    err_code = nrf_drv_timer_init(&TIMER, NULL, timer_event_handler);
    APP_ERROR_CHECK(err_code);
    
    time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER, time_ms);
    
    nrf_drv_timer_extended_compare(
         &TIMER, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
    
    //nrf_drv_timer_enable(&TIMER);
 
    while(1)
    {
        __WFI();
    }
}
 
cs


'Programing > Nordic(BLE)' 카테고리의 다른 글

ble_app_uart 인디케이터 LED 설정 변경  (0) 2015.09.24
시중에 판매중인 모듈 사용하기  (0) 2015.09.23
pin change interrupt  (0) 2015.05.25
LED 깜빡이기  (0) 2015.05.10
타이머 인터럽트  (0) 2015.05.10