五、nrf52832的timer定时器

timer
1.定时器的功能:定时(需要配置比较/捕获寄存器的比较值)、计数
2.nrf52832具有5个32位的timer定时器
3.时钟源:
    1.当f < 1MHz,系统自动选择PCLK1M的时钟源(低功耗)
    2.当f > 1MHz,系统自动选择PCLK16M的时钟源
4.timer的时钟频率可以选择(0-9分频)
    无论使用哪个时钟源都是以16M的为基准
timer常见函数
1.定义定时器实例:
     nrfx_timer_t timer_instance = NRF_DRV_TIMER_INSTANCE(timer_id)
2.定义定时器结构体:
    nrfx_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG
    (定时器默认配置,可以字sdkconfig.h进行修改)
    1.frequency :时钟频率
    2.mode      :模式 
    3.bit_witdth:位宽
    4.interrupt_priority:优先级
    5.中断处理函数
3.定时器初始化函数:
    nrfx_timer_init(&timer_instance,&timer_cfg,timer_cb_handle)
4.定时器使能函数:
    nrfx_timer_enable(&timer_instance)
5.定时器不使能函数
    nrfx_timer_disable(&timer_instance)
    
6.定时器的计数(count)方式:
    读取计数值函数;
        nrfx_timer_capture(&timer_instance,cc_channal)
        cc_channal:比较通道
    触发方式;nrfx_timer_increment(&timer_instance)
        手动触发:
        PPI(可编程外设互联)触发:
timer定时示例程序
#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "boards.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_drv_timer.h"


static void log_init(void)
{
	ret_code_t err = NRF_LOG_INIT(NULL);
	APP_ERROR_CHECK(err);
	NRF_LOG_DEFAULT_BACKENDS_INIT();
}

const nrfx_timer_t timer_instance = NRFX_TIMER_INSTANCE(0);					//定义一个定时器实例,选择timer0

void timer_event_handle(nrf_timer_event_t event_type,void* p_context)		//定时器事件回调函数
{
	switch (event_type)
		{
			case NRF_TIMER_EVENT_COMPARE0:									//定时器0事件,翻转电平
				nrf_gpio_pin_toggle(17);
				break;
			default:
			break;
		}
}

void timer_init(void)
{
	uint32_t err = NRF_SUCCESS;
	uint32_t timer_ms = 500;
	uint32_t timer_ticks;
	//可以在sdkconfig.h文件中修改默认配置(16M,16bit, priority:6,无参数,mode:timer)
	nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG;				//定义一个定时器结构体,默认配置
	err = nrfx_timer_init(&timer_instance,&timer_cfg,timer_event_handle);	//初始化定时器,注册回调函数
	APP_ERROR_CHECK(err);
	timer_ticks = nrfx_timer_ms_to_ticks(&timer_instance,timer_ms);			//计算500ms占用几个tick
	nrfx_timer_extended_compare(&timer_instance,NRF_TIMER_CC_CHANNEL0,timer_ticks,NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,true);
	//定时器0比较通道溢出清0
}


int main(void)
{
	log_init();

	nrf_gpio_cfg_output(17);
	nrf_gpio_pin_set(17);
	timer_init();
	nrfx_timer_enable(&timer_instance);									//使能定时器
	
	NRF_LOG_INFO("GPIO example started");
	NRF_LOG_FLUSH();
	
	while(1)
	{
		
	}
}
timer的计数示例程序
#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "boards.h"
/* 包含log头文件 */
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
/* 包含定时器头文件 */
#include "nrf_drv_timer.h"
/* 包含uart头文件 */
#include "app_uart.h"
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif

/* 定义uart发送接收缓冲区 */
#define 	UART_TX_BUF_SIZE		256
#define		UART_RX_BUF_SIZE		256

const nrfx_timer_t timer_instance = NRFX_TIMER_INSTANCE(0);					//定义一个定时器实例,选择timer0

/**
 * log初始化函数
 */
static void log_init(void)
{
	ret_code_t err = NRF_LOG_INIT(NULL);		//log初始化
	APP_ERROR_CHECK(err);						//检测log配置信息
	NRF_LOG_DEFAULT_BACKENDS_INIT();			//开启log
}

/**
 * uart事件回调函数
 */
void uart_event_handle(app_uart_evt_t* event)
{
	//uart通讯出错事件
	if(event->evt_type == APP_UART_COMMUNICATION_ERROR)
	{
		APP_ERROR_HANDLER(event->data.error_communication);
	}
	//fifo错误事件
	else if(event->evt_type == APP_UART_FIFO_ERROR)
	{
		APP_ERROR_HANDLER(event->data.error_code);
	}
}

/**
 * uart初始化参数配置函数
 */
void uart_config(void)
{
	uint32_t err;
	/* 串口结构体参数 */
	const app_uart_comm_params_t comm_params = {
		RX_PIN_NUMBER,									//接收引脚p0.08
		TX_PIN_NUMBER,									//发送引脚p0.06
		RTS_PIN_NUMBER,									//输出信号引脚p0.05
		CTS_PIN_NUMBER,									//输入信号引脚p0.07
		APP_UART_FLOW_CONTROL_DISABLED,					//使能软件控制流
		false,											//不校验
		NRF_UART_BAUDRATE_115200						//串口波特率115200
	};
	/* 串口初始化 */
	APP_UART_FIFO_INIT(&comm_params,					//串口结构体
						UART_RX_BUF_SIZE,				//串口接收缓冲区大小
						UART_TX_BUF_SIZE,				//串口发送缓冲区大小
						uart_event_handle,				//串口事件回调函数
						APP_IRQ_PRIORITY_LOWEST,		//串口中断优先级最低
						err);							//配置信息
	APP_ERROR_CHECK(err);								//检测配置是否成功
}

void timer_event_handler(nrf_timer_event_t event_type, void* p_context){}

void timer_init(void)
{
	uint32_t err = NRF_SUCCESS;
	//可以在sdkconfig.h文件中修改默认配置(16M,16bit, priority:6,无参数,mode:timer)
	nrfx_timer_config_t timer_cfg = NRFX_TIMER_DEFAULT_CONFIG;				//定义一个定时器结构体,默认配置
	timer_cfg.mode = NRF_TIMER_MODE_COUNTER;
	err = nrfx_timer_init(&timer_instance,&timer_cfg,timer_event_handler);	//初始化定时器,不需要注册回调函数
	APP_ERROR_CHECK(err);
}

/**
 * uart初始化函数
 */

int main(void)
{
	uint32_t value = 0;
	//log初始化
	log_init();

	nrf_gpio_cfg_output(17);
	nrf_gpio_pin_clear(17);
	//串口初始化
	uart_config();

	timer_init();
	nrfx_timer_enable(&timer_instance);

	NRF_LOG_INFO("uart example start");
	NRF_LOG_FLUSH();

	while(1)
	{
		nrfx_timer_increment(&timer_instance);
		value = nrfx_timer_capture(&timer_instance,NRF_TIMER_CC_CHANNEL2);
		printf("value = %d\r\n",value);
		nrf_delay_ms(500);
		nrf_gpio_pin_toggle(17);
	}
}
记得包含串口的源文件,否则可能出现编译没问题,运行没效果的现象
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在nRF52832上启用低功耗定时器(Low Power Timer),你需要进行以下步骤: 1. 配置低功耗定时器 首先,你需要配置低功耗定时器的时钟源和预分频器。你可以选择使用低频时钟(LFCLK)源,例如内部RC振荡器或外部晶体振荡器。然后,选择适当的预分频器来设置定时器的时钟速度。 2. 初始化低功耗定时器 使用nRF SDK或直接操作寄存器来初始化低功耗定时器。你需要设置定时器的模式(定时器模式或计数器模式)、位宽(8位或16位)、比较值(如果需要)等。 3. 启动低功耗定时器 在初始化完成后,启动低功耗定时器以开始计时或计数。你可以使用相应的函数或操作寄存器来启动定时器。 下面是一个示例代码片段,展示了如何在nRF52832上启用低功耗定时器: ```c #include <stdbool.h> #include "nrf.h" #include "nrf_drv_clock.h" #include "nrf_delay.h" void lptimer_init(void) { // 配置LFCLK源为内部RC振荡器 NRF_CLOCK->LFCLKSRC = CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos; NRF_CLOCK->EVENTS_LFCLKSTARTED = 0; NRF_CLOCK->TASKS_LFCLKSTART = 1; // 等待LFCLK稳定 while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0) { // 等待 } // 配置低功耗定时器 NRF_LPTIMER->PRESCALER = 0; // 预分频器设置为1 NRF_LPTIMER->BITMODE = LPTIMER_BITMODE_BITMODE_16Bit; // 使用16位模式 // 初始化低功耗定时器 NRF_LPTIMER->TASKS_START = 1; // 启动低功耗定时器 } int main(void) { // 初始化时钟 nrf_drv_clock_init(); nrf_drv_clock_lfclk_request(NULL); // 初始化低功耗定时器 lptimer_init(); while (true) { // 主循环代码 } } ``` 在这个例程中,我们首先需要初始化时钟,然后调用`lptimer_init()`函数来配置和初始化低功耗定时器。最后,在主循环中,你可以添加自己的代码来处理定时器中断或检查定时器的当前值。 请注意,以上代码仅为示例,你可能需要根据自己的具体应用场景进行适当的修改和配置。在实际使用中,请参考nRF52832的技术参考手册和开发环境文档以获取更多详细信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值