我再nrf52840主函数里创建了一个定时器,启动之后发现定时回调函数非常频繁的触发,完全没有按照我设定的时间间隔触发,请问这是什么原因呢?下面是代码:
//Timer时间回调函数
void timer_clock_event_handler(nrf_timer_event_t event_type, void* p_context)
{
switch(event_type)
{
//因为我们配置的是使用CC通道0,所以时间回调函数中判断NRF_TIMER_EVENT_COMPARE0事件
case NRF_TIMER_EVENT_COMPARE0:
nrf_gpio_pin_toggle(LED_POWER);
break;
default:
break;
}
}
//定时器初始化
void timer_init(void)
{
uint32_t err_code = NRF_SUCCESS;
uint32_t time_ms = 1*1000;//定义时间5000ms
uint32_t time_ticks;
//定义定时器配置结构体,并使用默认配置参数初始化结构体
nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG;
//初始化定时器,初始化时会注册timet_led_event_handler时间回调函数
err_code = nrfx_timer_init(&TIMER_CLOCK, &timer_cfg, timer_clock_event_handler);
APP_ERROR_CHECK(err_code);
//时间(单位ms)转换为ticks
time_ticks = nrfx_timer_ms_to_ticks(&TIMER_CLOCK, time_ms);
//设置定时器捕获/比较通道及该通道的比较值,使能通道的比较中断
nrfx_timer_extended_compare(
&TIMER_CLOCK, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
}
void main(void)
{
ret_code_t err_code;
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
nrf_gpio_cfg_output(LED_POWER);
nrf_gpio_cfg_output(LED_DATA);
nrf_gpio_cfg_output(LED_FLAG);
nrf_gpio_pin_set(LED_POWER);
nrf_gpio_pin_clear(LED_DATA);
nrf_gpio_pin_clear(LED_FLAG);
nrf_gpio_cfg_input(KEY_1, NRF_GPIO_PIN_PULLUP);
//nrf_gpio_range_cfg_output(LED_START, LED_START);
//nrf_gpio_range_cfg_output(LED_START, LED_STOP);
NRF_LOG_INFO("LSM6DSO sensor started.");
NRF_LOG_FLUSH();
twi_init();
init_sensor();
gpiote_init();
timer_init(); //初始化定时器
nrfx_timer_enable(&TIMER_CLOCK); //启动定时器
while (true)
{
;
}
}