nrf52832-定时器例程

SDK版本:15.20
代码

#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "boards.h"
#include "nrf_drv_timer.h"

//驱动程序实例的ID对应Timer的ID
const nrfx_timer_t TIMER_LED = NRFX_TIMER_INSTANCE(0);


//Timer事件回调函数
void timer_led_event_handler(nrf_timer_event_t event_type, void *p_context)
{
	switch(event_type)
	{
		//CC[0]的比较匹配事件
		case NRF_TIMER_EVENT_COMPARE0:
		  //翻转指示灯D1状态
		  nrf_gpio_pin_toggle(LED_1);
		break;
	}
}

//定时器初始化
void timer_init(void)
{
	uint32_t err_code = NRF_SUCCESS;
	uint32_t time_ms = 200;  //定时时间为200ms
	uint32_t time_ticks;
	
	//定义定时器配置结构体,并使用默认配置参数初始化结构体
	nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG;
	
	//初始化定时器,初始化时会注册timer_led_event_handler事件回调函数
	err_code = nrfx_timer_init(&TIMER_LED,&timer_cfg,timer_led_event_handler);
    APP_ERROR_CHECK(err_code);
	
	//定时时间(单位ms)转换为ticks
	time_ticks = nrfx_timer_ms_to_ticks(&TIMER_LED,time_ms);
	
	//设置定时器捕获/比较通道及该通道的比较值,使能通道的比较中断
	nrfx_timer_extended_compare(
	                            &TIMER_LED,NRF_TIMER_CC_CHANNEL0,time_ticks,NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, 
								true);
    	
}

int main()
{
	//初始化开发板上的4个LED,即将驱动LED的GPIO配置为输出
	bsp_board_init(BSP_INIT_LEDS);
	
	timer_init();  //初始化定时器
	
	nrfx_timer_enable(&TIMER_LED);  //启动定时器
	
	while(true)
	{

	}
		return 0;
}

原文地址:https://www.cnblogs.com/Manual-Linux/p/10330677.html