Examples

Nordic52840SDK学习之定时器

Nordic 52840 SDK学习之定时器

今天开始学习52840SDK,特在此处记录学习内容,防止以后忘记,或许可以给以后的初学者提供一些帮助。如有错误,请发邮件至843036544@qq.com,我看到会及时改正。

当前只是初学该sdk,内容可能比较简单,不喜勿喷。

 

简介:定时器部分,主要是写了一些测试代码(在源sdk中添加了一些测试接口),用来学习定时器的使用。

步骤:

1)包含头文件app_timer.h

2)定义Timer id和interval

其中id用来区分不同功能的定时器,interval表示定时器的间隔(即每隔多长时间进入定时器中断函数)

 

3)Timer初始化(该初始化在原始sdk中已经调用)

 

4)创建一个定时器Timer

ret_code_t app_timer_create(app_timer_id_t const *      p_timer_id,
                            app_timer_mode_t            mode,
                            app_timer_timeout_handler_t timeout_handler);

p_timer_id: 定时器id

mode:APP_TIMER_MODE_SINGLE_SHOT(只进入一次);APP_TIMER_MODE_REPEATED(重复进入)

timeout_handler:定时器中断处理函数

 

5)启动定时器

ret_code_t app_timer_start(app_timer_id_t timer_id, uint32_t timeout_ticks, void * p_context)

timeout_ticks: 定时器启动后多长时间进入定时器处理函数

p_context: 如果有参数需要传入定时器处理函数,可通过该参数传入

 

6)停止定时器

ret_code_t app_timer_stop(app_timer_id_t timer_id);

7)定时器其他函数

ret_code_t app_timer_stop_all(void);

uint32_t app_timer_cnt_get(void);

void app_timer_pause(void);

void app_timer_resume(void);

uint8_t app_timer_op_queue_utilization_get(void);

uint32_t app_timer_cnt_diff_compute(uint32_t   ticks_to, uint32_t   ticks_from);

 

其中,我只测试了pause函数,该函数会将所有timer都暂停。 具官方文档介绍,该函数用于debug。

This function can be used for debugging purposes to ensure that application is halted when entering a breakpoint.

其他函数后续使用的时候在继续添加使用方法。

 

 

下面是我的测试代码:

 

#include "app_timer.h"

APP_TIMER_DEF( ipl_app_timer_id );
#define IPL_TIME_INTERVAL APP_TIMER_TICKS( 1000 )  //每隔1000ms进入一次

APP_TIMER_DEF( ipl_app_timer_id2 );
#define IPL_TIME_INTERVAL_2 APP_TIMER_TICKS( 500 ) //每隔500ms进入一次


void ipl_app_timerout_handle( void *p_context )
{
    static int count = 0;
    count ++;
    
    NRF_LOG_INFO( "ipl app timer count=%d\n",count );
    
    if( count==5 )
    {
        //app_timer_pause(); //此次调用该函数,会导致timer1和timer2都停止        
        app_timer_stop( ipl_app_timer_id );
    }
}


void ipl_app_timerout_handle2( void *p_context )
{
    static int num = 0;
    num ++;
    
    NRF_LOG_INFO( "ipl app timer2 num=%d\n",num );
    
    if( num==30 )
    {
        app_timer_start( ipl_app_timer_id, IPL_TIME_INTERVAL, NULL );
    }
}



/**@brief Function for ipl timer test init.
 */
ret_code_t ipl_app_timers_init( void )
{    
    ret_code_t err_code;
    

    err_code = app_timer_create( &ipl_app_timer_id, APP_TIMER_MODE_REPEATED, ipl_app_timerout_handle );
    if( err_code != NRF_SUCCESS )
    {
        NRF_LOG_INFO( "Create timer error\n" );    
        return NRF_ERROR_NULL;
    }
    
    
    app_timer_start( ipl_app_timer_id, IPL_TIME_INTERVAL, NULL );
    return NRF_SUCCESS;
}


ret_code_t ipl_app_timers2_init( void )
{
    ret_code_t err_code;
    

    err_code = app_timer_create( &ipl_app_timer_id2, APP_TIMER_MODE_REPEATED, ipl_app_timerout_handle2 );
    if( err_code != NRF_SUCCESS )
    {
        NRF_LOG_INFO( "Create timer error\n" );    
        return NRF_ERROR_NULL;
    }
    
    app_timer_start( ipl_app_timer_id2, IPL_TIME_INTERVAL_2, NULL );
    return NRF_SUCCESS;
}


int main( void )
{
    timers_init();  //初始化定时器
    ipl_app_timers_init();
    ipl_app_timers2_init();
   
    ......
    
}

 

posted on 2020-03-13 15:21  足各火丁  阅读(797)  评论(0编辑  收藏  举报

导航

Examples