NRF52832定时器

以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();
    }
}

 

代码下载地址:https://download.csdn.net/download/mygod2008ok/10734229

 

 

 

 

 

 

 

你可能感兴趣的:(NRF52832个人学习笔记)