nRF52832无协议栈下软件定时器的使用

编译器及例程说明

1. nRF支持包 : NordicSemiconductor.nRF_DeviceFamilyPack.8.17.0.pack
2. ARM支持包 : ARM.CMSIS.4.5.0.pack
3. Toolchain : MDK-ARM Community  Version: 5.33.0.0

4. 将调试信息从J-Link RTT打印出来
5. 创建两个定时器,通过J-Link RTT将其运行次数打印出来

sdk_config.h配置说明

//使能定时器模块
APP_TIMER_ENABLED = 1

//定时器RTC频率为32768HZ
APP_TIMER_CONFIG_RTC_FREQUENCY = 0

//定时器队列大小,决定创建定时器数量
APP_TIMER_CONFIG_OP_QUEUE_SIZE = 10

//定时器优先级
APP_TIMER_CONFIG_IRQ_PRIORITY = 6



// 使能日志
NRF_LOG_ENABLED = 1

// 使能J-Link RTT作为日志输出
NRF_LOG_BACKEND_RTT_ENABLED = 1
    
//日志默认级别,4最高,高级别兼容低级别
0-Off	1-Error		2-Warning 
3-Info 	4-Debug 
NRF_LOG_DEFAULT_LEVE = 4

一、日志初始化

/****************************************************************************************************
 * 函数名 : log_init
 * 描  述 : 日志初始化
*****************************************************************************************************/
static void log_init(void)
{
	ret_code_t errCode = NRF_LOG_INIT(NULL);//Init logs modules
	APP_ERROR_CHECK(errCode);
	NRF_LOG_DEFAULT_BACKENDS_INIT();//Init UART or RTT output log
}

二、空闲状态处理

/****************************************************************************************************
 * 函数名 : idle_state_handle
 * 描  述 : 空闲状态处理
 * 说  明 : 1. 在空闲状态时处理挂起的日志,若没有挂起日志则进行电源管理
*****************************************************************************************************/
static void idle_state_handle(void)
{
	//Processing deferred logs
	if(NRF_LOG_PROCESS() == false)
	{
		//Running power management
		nrf_pwr_mgmt_run();
	}
}

三、LED GPIO配置

/****************************************************************************************************
 * 函数名 : led_gpio_config
 * 描  述 : LED GPIO配置
 * 说  明 : 1. 输出模式、无上拉、0/1标准驱动、关引脚感知
*****************************************************************************************************/

#define LED1_GPIO_Pin													12	//P0.12
#define LED2_GPIO_Pin													13	//P0.13
#define LED3_GPIO_Pin													14	//P0.14


tatic void led_gpio_config(void)
{
	nrf_gpio_cfg_output(LED1_GPIO_Pin);
	nrf_gpio_cfg_output(LED2_GPIO_Pin);
	nrf_gpio_cfg_output(LED3_GPIO_Pin);

	nrf_gpio_pin_set(LED1_GPIO_Pin);//GPIO pin set
	nrf_gpio_pin_set(LED2_GPIO_Pin);
	nrf_gpio_pin_set(LED3_GPIO_Pin);

	// nrf_gpio_pin_clear(LED1_GPIO_Pin);//GPIO pin claer
	// nrf_gpio_pin_clear(LED2_GPIO_Pin);
	// nrf_gpio_pin_clear(LED3_GPIO_Pin);

	// nrf_gpio_pin_toggle(LED1_GPIO_Pin);//GPIO pin toggle
	// nrf_gpio_pin_toggle(LED2_GPIO_Pin);
	// nrf_gpio_pin_toggle(LED3_GPIO_Pin);
}

四、定时器1超时处理

/****************************************************************************************************
 * 函数名 : timer1_timeout_handle
 * 描  述 : 定时器1超时处理
 * 输  入 : p_context : 参数
*****************************************************************************************************/
static void timer1_timeout_handle(void * p_context)
{
	static uint16_t i = 0;
	
	
	UNUSED_PARAMETER(p_context);
	NRF_LOG_INFO("Timer1 Running Count: %d", ++i);
	nrf_gpio_pin_toggle(LED1_GPIO_Pin);
}

五、定时器2超时处理

/****************************************************************************************************
 * 函数名 : timer2_timeout_handle
 * 描  述 : 定时器2超时处理
 * 输  入 : p_context : 参数
*****************************************************************************************************/
static void timer2_timeout_handle(void * p_context)
{
	static uint16_t x = 0;
	
	
	UNUSED_PARAMETER(p_context);
	NRF_LOG_INFO("Timer2 Running Count: %d", ++x);
	nrf_gpio_pin_toggle(LED2_GPIO_Pin);
}

六、定时器初始化

/****************************************************************************************************
 * 函数名 : timer_init
 * 描  述 : 定时器(软件)初始化
 * 说  明 : 1. 清SWI中断标志、设置SWI中断优先级、使能SWI中断
 * 			2. 根据欲分频值初始化RTC1,0为不分频
 * 			3. APP_TIMER_CONFIG_RTC_FREQUENCY为RTC1预分频值 
 * 注  意 : 1. 软件定时器数量受APP_TIMER_CONFIG_OP_QUEUE_SIZE限制
*****************************************************************************************************/

APP_TIMER_DEF(timer1_id);										//Timer id
#define TIM1_INTERVAL				APP_TIMER_TICKS(1000)		//Timer interval,1s

APP_TIMER_DEF(timer2_id);										//Timer id
#define TIM2_INTERVAL				APP_TIMER_TICKS(2000)		//Timer interval,2s


static void timer_init(void)
{
	ret_code_t errCode = app_timer_init();//Init RTC1 timer module
	APP_ERROR_CHECK(errCode);
	
	//Create software timer1
	errCode = app_timer_create(&timer1_id, APP_TIMER_MODE_REPEATED, timer1_timeout_handle);//Create timer
	APP_ERROR_CHECK(errCode);
	
	//Create software timer2
	errCode = app_timer_create(&timer2_id, APP_TIMER_MODE_REPEATED, timer2_timeout_handle);//Create timer
	APP_ERROR_CHECK(errCode);
}

七、启动定时器

/****************************************************************************************************
 * 函数名 : start_timer
 * 描  述 : 启动定时器(软件)
*****************************************************************************************************/
static void start_timer(void)
{
	//Start software timer1
	ret_code_t errCode = app_timer_start(timer1_id, TIM1_INTERVAL, NULL);//Start timer
	APP_ERROR_CHECK(errCode);	

	//Start software timer2
	errCode = app_timer_start(timer2_id, TIM2_INTERVAL, NULL);//Start timer
	APP_ERROR_CHECK(errCode);
}

八、低频时钟配置(LFCLK)

/****************************************************************************************************
 * 函数名 : lfclk_config
 * 描  述 : 低频时钟配置
 * 说  明 : 1. 低频时钟源为外部32.768KHZ,由NRFX_CLOCK_CONFIG_LF_SRC决定
 * 			2. 软件定时器由RTC1实现,RTC1的时钟源由LFCLK提供,故必须配置LFCLK
 * 注  意 : 1. 只有在裸机下才需要配置LFCLK,在协议栈下自动配置且LFCLK一直运行
*****************************************************************************************************/
static void lfclk_config(void)
{
	ret_code_t errCode = nrf_drv_clock_init();//Init the nrf_drv_clock module
	APP_ERROR_CHECK(errCode);

	nrf_drv_clock_lfclk_request(NULL);//Request lfclk, NULL no event
}

九、主函数

/****************************************************************************************************
 * 函数名 : main
 * 描  述 : 主函数
*****************************************************************************************************/
int main(void)
{
	log_init();
	timer_init();
	led_gpio_config();
	start_timer();
	lfclk_config();

	NRF_LOG_DEBUG("BLE Template example Test");
	NRF_LOG_INFO("BLE Template example:Log use J-Link RTT as output terminal");
	while(true)
	{
		idle_state_handle();
	}
}

十、例程结果

<info> CLOCK: Function: nrfx_clock_init, error code: NRF_SUCCESS.
<info> CLOCK: Module enabled.
<info> clock: Function: nrf_drv_clock_init, error code: NRF_SUCCESS.
<debug> app: BLE Template example Test
<info> app: BLE Template example:Log use J-Link RTT as output terminal
<info> app: Timer1 Running Count: 1
<info> app: Timer1 Running Count: 2
<info> app: Timer2 Running Count: 1
<info> app: Timer1 Running Count: 3
<info> app: Timer1 Running Count: 4
<info> app: Timer2 Running Count: 2
<info> app: Timer1 Running Count: 5
<info> app: Timer1 Running Count: 6
<info> app: Timer2 Running Count: 3
<info> app: Timer1 Running Count: 7
<info> app: Timer1 Running Count: 8
<info> app: Timer2 Running Count: 4
<info> app: Timer1 Running Count: 9
<info> app: Timer1 Running Count: 10
<info> app: Timer2 Running Count: 5
<info> app: Timer1 Running Count: 11
<info> app: Timer1 Running Count: 12
<info> app: Timer2 Running Count: 6
<info> app: Timer1 Running Count: 13
<info> app: Timer1 Running Count: 14
<info> app: Timer2 Running Count: 7
<info> app: Timer1 Running Count: 15
<info> app: Timer1 Running Count: 16
<info> app: Timer2 Running Count: 8
<info> app: Timer1 Running Count: 17
<info> app: Timer1 Running Count: 18
<info> app: Timer2 Running Count: 9
<info> app: Timer1 Running Count: 19

在这里插入图片描述

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值