十二、nrf52832的RTC(实时计数器)

本文介绍了nrf52832芯片上的RTC实时计数器功能,包括RTC的初始化、使能tick、配置比较通道和启动等操作。示例程序展示了如何使用RTC进行系统调度,并通过中断处理RTC事件,实现低功耗运行。同时,还详细讲解了UART的配置和RTC事件回调函数的编写,为基于RTC的嵌入式系统开发提供了参考。
摘要由CSDN通过智能技术生成
RTC
注意:这里指明的RTC不是实时时钟,而是实时计数器
1.nrf52832具有3个RTC实时计数器(RTC0~RTC2)24位
2.nrf52832使用RTC为RTOS的系统调度提供支持(为了实现低功耗,不使用系统systick)
RTC的常用函数
1.RTC初始化函数:nrfx_rtc_init(&p_instance,&p_config,event_handle)
2.使能RTC的tick函数:nrfx_rtc_tick_enable(&p_instance,enable_irq)
3.配置比较通道函数:nrfx_rtc_cc_set(&p_instance,channel,value,enable_irq)
4.启动rtc函数:nrfx_rtc_enable(&p_instance)
RTC的示例程序
#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"
/* 包含uart头文件 */
#include "app_uart.h"
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"							//带easydma的串口
#endif

#include "nrf_drv_rtc.h"						//包含rtc实时计数器的头文件
#include "nrf_drv_clock.h"

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

/**
 * 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);								//检测配置是否成功
}

/* 创建RTC实例 */
const nrf_drv_rtc_t rtc_instance = NRF_DRV_RTC_INSTANCE(0);

//配置低频时钟,时钟源设置的是外部32.768KHz晶体
static void lfclk_config(void)
{
    //初始化时钟模块,设置低频时钟源
	ret_code_t err_code = nrf_drv_clock_init();
    APP_ERROR_CHECK(err_code);
    //请求低频时钟,输入参数为NULL表示低频时钟启动后不产生事件
    nrf_drv_clock_lfclk_request(NULL);
}

/**
 * rtc事件回调函数
 */
void rtc_event_handle(nrfx_rtc_int_type_t event)
{
	if(event == NRFX_RTC_INT_TICK)
	{
		nrf_gpio_pin_toggle(17);
		printf("rtc tick event is happened\r\n");
	}
	else if(event == NRFX_RTC_INT_COMPARE0)
	{
		nrf_gpio_pin_toggle(18);
		nrfx_rtc_counter_clear(&rtc_instance);								//清除计数器数值
		printf("rtc cc event is happened\r\n");
	}
}

/**
 * RTC初始化配置函数
 */
static void rtc_config(void)
{
	ret_code_t err;
	nrfx_rtc_config_t rtc_config = NRFX_RTC_DEFAULT_CONFIG;					//创建rtc结构体,默认配置
	rtc_config.prescaler = 4095;											//设置分频系数为4095
																			//t = 32768/(4095+1) = 8Hz(125ms)
	err = nrfx_rtc_init(&rtc_instance,&rtc_config,rtc_event_handle);		//初始化rtc,注册rtc事件回调函数
	APP_ERROR_CHECK(err);

	nrfx_rtc_tick_enable(&rtc_instance,true);								//初始化rtc的tick事件,使能中断
	nrfx_rtc_cc_set(&rtc_instance,0,2*8,true);								//初始化rtc的cc比较通道,时间(2s (8Hz)),使能中断
	//需要屏蔽nrfx_rtc.c中的禁止比较事件和比较事件中断的代码(299和300行)(否则只执行一次)
	nrfx_rtc_enable(&rtc_instance);											//使能rtc
}	


int main(void)
{
	log_init();

	nrf_gpio_cfg_output(17);
	nrf_gpio_pin_set(17); 
	nrf_gpio_cfg_output(18);
	nrf_gpio_pin_set(18); 

	lfclk_config();
	uart_config();
	rtc_config();

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

	while(1)
	{
		__SEV();										//进入低功耗
		__WFE();										//等待事件唤醒
		__WFE();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值