nRF52系列支持5个timer Register分别为
这里的5个timer Register分别对应 NRF_DRV_TIMER_INSTANCE中的0~4
PS:默认SDK使用0(Timer0),如果使用了SDK,那么Timer只能从1开始
初始化nrfx_err_t nrfx_timer_init(nrfx_timer_t const * const p_instance,
nrfx_timer_config_t const * p_config,
nrfx_timer_event_handler_t timer_event_handler)
- p_instance
timer register - p_config:
-
frequency
默认为0(NRF_TIMER_FREQ_16MHz),可以设定(0-9),对应关系参考nrf_timer_frequency_t -
mode
默认为0(NRF_TIMER_MODE_TIMER):timer;可以设定为counter(1或者2),1官方文档中说明弃用
-
bit_width
默认为0(NRF_TIMER_BIT_WIDTH_8),可以设定(0~3),对应关系参考nrf_timer_bit_width_t,设定的最大timer数 -
interrupt_priority
中断优先级,可以设定(0~7),0为最高 -
p_context
NULL
-
在compare模式下面设定timer channel
nrfx_timer_extended_compare(nrfx_timer_t const * const p_instance,
nrf_timer_cc_channel_t cc_channel,
uint32_t cc_value,
nrf_timer_short_mask_t timer_short_mask,
bool enable_int)
- p_instance
timer register - cc_channel
timer channel,默认0(NRF_TIMER_CC_CHANNEL0),可以设定(0~5) - cc_value: 设定的时间转换的ticks
- uint32_t nrfx_timer_us_to_ticks ( nrfx_timer_t const *const p_instance, uint32_t time_us )
- uint32_t nrfx_timer_ms_to_ticks ( nrfx_timer_t const *const p_instance, uint32_t time_ms )
- timer_short_mask
对应STOP还是CLEAR的MASK,参考nrf_timer_short_mask_t定义 - enable_int
默认true
使能nrfx timer
nrfx_timer_enable(nrfx_timer_t const * const p_instance)
- p_instance: timer register
代码:
#include "nrf_drv_timer.h"
const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(1);
void timer_led_event_handler(nrf_timer_event_t event_type, void *p_context)
{
switch(event_type)
{
case NRF_TIMER_EVENT_COMPARE0:
nrf_gpio_pin_toggle(LED_R);
break;
default:
nrf_gpio_pin_set(LED_B);
break;
}
}
static void instance1_timer_init(void)
{
uint32_t time_ms = 667;
uint32_t time_ticks;
uint32_t err_code = NRF_SUCCESS;
nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
timer_cfg.frequency = NRF_TIMER_FREQ_16MHz;
timer_cfg.mode = NRF_TIMER_MODE_TIMER;
timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_16;
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);
nrf_drv_timer_extended_compare(&TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
nrf_drv_timer_enable(&TIMER_LED);
}
main中初始化instance1_timer_init()
instance1_timer_init();
sdk_config.h相关
NRFX_TIMER_ENABLE - nrfx_timer - TIMER periperal driver开关要打开(如果没有,到官方example中找到对应的demo,把配置文件拷过来,参考:examples\peripheral\timer\pca10056\blank\arm5_no_packs)
拷的时候注意,以下面的// 开始
// <e> TIMER_ENABLED - nrf_drv_timer - TIMER periperal driver - legacy layer
以 // 结束
// </e>
// </e>
分别打开NRFX_TIMER0_ENABLED,NRFX_TIMER1_ENABLED…
这边的0-4就是对应的timer Register