NRF528xx 芯片外设常用函数与事件速查(三)-----Timer/RTC

一、Timer
(1)定时模式

  1. nrfx_err_t nrfx_timer_init(nrfx_timer_t const * const p_instance,//指向定时器驱动程序实例结构体的指针。
    ------------------------------nrfx_timer_config_t const * p_config,//定时器配置结构体,如果是 NULL,使用默认配置参数。
    ------------------------------nrfx_timer_event_handler_t timer_event_handler)//事件处理函数,不能为 NULL。
    作用:初始化Timer定时器
    示例://定义Timer0的驱动实例
    --------const nrfx_timer_t TIMER = NRFX_TIMER_INSTANCE(0);
    --------NRFX_TIMER_INSTANCE(ID)//宏定义赋值
    --------//定义定时器配置结构体
    --------nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG;
    --------NRFX_TIMER_DEFAULT_CONFIG//宏定义赋值
    --------//初始化定时器
    --------nrfx_timer_init(&TIMER , &timer_cfg, timer_event_handler);
    --------timer_event_handler//事件处理函数

  2. uint32_t nrfx_timer_event_address_get(nrfx_timer_t const * const p_instance,
    ----------------------------------------------nrf_timer_event_t timer_event);
    作用:获取Timer事件的寄存器地址
    示例:nrfx_timer_event_address_get(&TIMER,NRF_TIMER_EVENT_COMPARE0);

EVENT
NRF_TIMER_EVENT_COMPAREn(n=0-5)---------//比较匹配事件通道0-5

  1. uint32_t nrfx_timer_ms_to_ticks(nrfx_timer_t const * const p_instance,
    ---------------------------------------uint32_t timer_ms)
    作用:将要定时的数值转化成ticks节拍数
    示例:uint32_t time_ticks = nrfx_timer_ms_to_ticks(&TIMER, 200);

  2. void nrfx_timer_extended_compare(nrfx_timer_t const * const p_instance,//timer实例
    ----------------------------------------nrf_timer_cc_channel_t cc_channel,//比较通道号
    ----------------------------------------uint32_t cc_value,//ticks节拍数
    ----------------------------------------nrf_timer_short_mask_t timer_short_mask,//设置计数停止或循环
    ----------------------------------------bool enable_int)//定时中断开关
    作用:配置定时器比较值并开启中断
    示例:nrfx_timer_extended_compare(&TIMER, NRF_TIMER_CC_CHANNEL0,
    ------------------------------------time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);

  3. void nrfx_timer_enable(nrfx_timer_t const * const p_instance)
    作用:开启定时器
    示例:nrfx_timer_enable(&TIMER);

  4. void nrfx_timer_disable(nrfx_timer_t const * const p_instance)
    作用:关闭定时器
    示例:nrfx_timer_disable(&TIMER);

  5. void nrfx_timer_pause(nrfx_timer_t const * const p_instance)
    作用:暂停定时器
    示例:nrfx_timer_pause(&TIMER);

(2)计数模式

  1. 将定时器结构体修改为计数模式
    示例:nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG;
    -------timer_cfg.mode = NRF_TIMER_MODE_COUNTER;

  2. void nrfx_timer_increment(nrfx_timer_t const * const p_instance)
    作用:触发count任务,定时器计数加1
    示例:nrfx_timer_increment(&TIMER);

  3. uint32_t nrfx_timer_capture(nrfx_timer_t const * const p_instance,
    -----------------------------------nrf_timer_cc_channel_t cc_channel)
    作用:获取通道2计数值
    示例:uint32_t timVal = nrfx_timer_capture(&TIMER,NRF_TIMER_CC_CHANNEL2);

  4. nrf_timer_task_t nrf_timer_capture_task_get(uint32_t channel)
    作用:触发CC通道n计数器数值任务
    示例:nrf_timer_capture_task_get(NRF_TIMER_CC_CHANNEL2);

  5. uint32_t * nrf_timer_task_address_get(NRF_TIMER_Type * p_reg,
    ----------------------------------------nrf_timer_task_t task)
    作用:获取任务端寄存器地址
    示例:nrf_timer_task_address_get(&TIMER->p_reg,NRF_TIMER_TASK_COUNT);

Task
NRF_TIMER_TASK_START---------Task for starting the timer.
NRF_TIMER_TASK_STOP----------Task for stopping the timer.
NRF_TIMER_TASK_COUNT---------Task for incrementing the timer (in counter mode).
NRF_TIMER_TASK_CLEAR---------Task for resetting the timer value.
NRF_TIMER_TASK_SHUTDOWN------Task for powering off the timer.
NRF_TIMER_TASK_CAPTURE0------Task for capturing the timer value on channel 0.

二、RTC实时计数器
BLE 程序中,协议栈使用了 RTC0,APP 定时器使用了 RTC1,所以,
BLE 程序中 RTC0 和 RTC1是不能作为通用的外设使用的,RTC2 可以作为通用外设使用

  1. nrfx_err_t nrfx_rtc_init(nrfx_rtc_t const * const p_instance,//RTC实例
    ----------------------------nrfx_rtc_config_t const * p_config,//RTC配置结构体
    ----------------------------nrfx_rtc_handler_t handler)//RTC事件处理回调函数
    作用:初始化RTC
    实例://定义RTC2的驱动实例
    -------const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2);
    -------NRF_DRV_RTC_INSTANCE//宏定义配置
    -------//初始化时钟模块,设置低频时钟源32.768KHz
    -------nrf_drv_clock_init();
    -------//请求低频时钟,输入参数NULL表示低频时钟启动后不产生事件
    -------nrf_drv_clock_lfclk_request(NULL);
    -------//定义RTC初始化配置结构体,并使用默认参数初始化
    -------nrfx_rtc_config_t config = NRFX_RTC_DEFAULT_CONFIG;
    -------NRFX_RTC_DEFAULT_CONFIG//宏定义配置
    -------//重写分频系数,分频系数为4095时的递增频率为 = 32768/(4095+1) = 8Hz
    -------//即每125ms counter计数器递增一次,tick事件发生一次
    -------config.prescaler = 4095;
    -------nrfx_rtc_init(&rtc, &config, rtc_handler);
    -------void rtc_handler(nrfx_rtc_int_type_t int_type)

  2. void nrfx_rtc_tick_enable(nrfx_rtc_t const * const p_instance, bool enable_irq)
    作用:使能tick事件,开启中断
    示例:nrfx_rtc_tick_enable(&rtc,true);

  3. void nrfx_rtc_tick_disable(nrfx_rtc_t const * const p_instance)
    作用:禁止tick事件
    示例:nrfx_rtc_tick_enable(&rtc);

  4. void nrfx_rtc_overflow_enable(nrfx_rtc_t const * const p_instance, bool enable_irq);
    作用:使能溢出事件,开启中断
    示例:nrfx_rtc_overflow_enable(&rtc,true);

  5. void nrfx_rtc_overflow_disable(nrfx_rtc_t const * const p_instance);
    作用:禁止溢出事件
    示例:nrfx_rtc_overflow_disable(&rtc);

  6. nrfx_err_t nrfx_rtc_cc_set(nrfx_rtc_t const * const p_instance,
    ------------------------------uint32_t channel,
    ------------------------------uint32_t val,//单位是秒,根据频率乘系数
    ------------------------------bool enable_irq);
    作用:使能比较事件,开启中断
    示例: nrfx_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);

  7. nrfx_err_t nrfx_rtc_cc_disable(nrfx_rtc_t const * const p_instance, uint32_t channel);
    作用:禁止比较事件
    示例:nrfx_rtc_cc_disable(&rtc,0);

  8. void nrfx_rtc_enable(nrfx_rtc_t const * const p_instance)
    作用:使能RTC
    示例:nrfx_rtc_enable(&rtc);

  9. void nrfx_rtc_disable(nrfx_rtc_t const * const p_instance)
    作用:禁止RTC
    示例:nrfx_rtc_disable(&rtc);

  10. uint32_t nrfx_rtc_task_address_get(nrfx_rtc_t const * const p_instance, nrf_rtc_task_t task)
    作用:获取任务寄存器地址
    示例:nrfx_rtc_task_address_get(&rtc,NRF_RTC_TASK_START);

  11. uint32_t nrfx_rtc_event_address_get(nrfx_rtc_t const * const p_instance,nrf_rtc_event_t event)
    作用:获取事件寄存器地址
    示例:nrfx_rtc_event_address_get(&rtc,NRF_RTC_EVENT_TICK);

TASK
NRF_RTC_TASK_START--------------------//开始rtc任务
NRF_RTC_TASK_STOP---------------------//停止rtc任务
NRF_RTC_TASK_CLEAR--------------------//计数器清0任务
NRF_RTC_TASK_TRIGGER_OVERFLOW---------//溢出任务

EVENT
NRF_RTC_EVENT_TICK-------------------计数节拍事件
NRF_RTC_EVENT_OVERFLOW---------------溢出事件
NRF_RTC_EVENT_COMPARE_n(0-3)---------通道n事件

void nrf_delay_ms(uint32_t ms_time)---------------阻塞式延时函数

posted @ 2025-05-10 11:57  感兴趣就学  阅读(97)  评论(0)    收藏  举报