1. 程式人生 > >mygod2008ok的專欄

mygod2008ok的專欄

以SDK14.0.0為例 ,TIMER0的定時器使用

1.加入檔案nrf_drv_timer.c

2.sdk_config.h中的巨集開啟

#ifndef TIMER_ENABLED #define TIMER_ENABLED 1 #endif

#ifndef TIMER0_ENABLED #define TIMER0_ENABLED 1 #endif

3.定義一個TIMER例項 

const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0);

4.定義一個timer事件處理函式 /**  * @brief Handler for timer events.  */ void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context) {                     nrf_gpio_pin_toggle(31);           }

5.初時化timer

int main(void) {     uint32_t time_ms = 244; //Time(in miliseconds) between consecutive compare events.     volatile uint32_t time_ticks;     uint32_t err_code = NRF_SUCCESS;

    //Configure all leds on board.     nrf_gpio_cfg_output(31);   //p0_31 配置成輸出模式       //Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.     nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;     timer_cfg.frequency = 4;  // 修改分頻,頻率變成1MHZ     err_code = nrf_drv_timer_init(&TIMER_LED, &timer_cfg, timer_led_event_handler);     APP_ERROR_CHECK(err_code);

   // time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_LED, time_ms); //呼叫此方法,轉換成TICK數,如果不呼叫,則可以實現

                                                                                                            //微秒級定時器

    nrf_drv_timer_extended_compare(          &TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ms, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);

    nrf_drv_timer_enable(&TIMER_LED);

       while (1)     {         __WFI();     } }