nrf52832 之 timer

nrf52832 有五个定时器,timer0--timer4 。因为SDK已经使用了timer0,所以如果使用了SDK,就不能再使用timer0,只能使用timer1--timer4。


开发环境:在ble_app_hrs   demo上添加定时器任务      参考timer这个demo。


本例使用timer1,并且修改为32bit模式



添加步骤:


1、添加驱动文件    nrf_drv_timer.c      在协议栈中的目录位置 :   nRF52_SDK\components\drivers_nrf\timer文件夹下


2、mian.c中添加头文件nrf_drv_timer.h

#include "nrf_drv_timer.h"

3、main.c中添加timer结构体变量:

const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(1);

4、在main.c中添加timer1 初始化函数以及定时任务处理函数:

/**
 * @brief Handler for timer events.
 */
void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context)
{
   // uint32_t led_to_invert = (1 << leds_list[(i++) % LEDS_NUMBER]);
   // printf("6\n");
    switch(event_type)
    {
        case NRF_TIMER_EVENT_COMPARE0:
          //  LEDS_INVERT(led_to_invert);
		  		ledFlash();
			
            break;
        
        default:
            //Do nothing.
            break;
    }    
}


void myTimerInit()
{
	uint32_t time_ms = 1000; //Time(in miliseconds) between consecutive compare events.
    uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;
    
    //Configure all leds on board.
 
    
    //Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.
    err_code = nrf_drv_timer_init(&TIMER_LED, NULL, timer_led_event_handler);
    APP_ERROR_CHECK(err_code);
	
    time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_LED, time_ms);
    
    nrf_drv_timer_extended_compare(
         &TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
    
    nrf_drv_timer_enable(&TIMER_LED);
}



5、main函数中添加timer初始化调用:

	myTimerInit();

6、在nrf_driver_config.c中修改timer1的定义:

#define TIMER1_ENABLED 1

#if (TIMER1_ENABLED == 1)
#define TIMER1_CONFIG_FREQUENCY    NRF_TIMER_FREQ_16MHz
#define TIMER1_CONFIG_MODE         TIMER_MODE_MODE_Timer
#define TIMER1_CONFIG_BIT_WIDTH    TIMER_BITMODE_BITMODE_32Bit
#define TIMER1_CONFIG_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW



你可能感兴趣的:(低功耗蓝牙)