This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nrf51822 TIMER 1 and TIMER 2

In timer example in sdk 11, I replace the Timer 0 by timer 1 and timer 2 but the LED did not blink. Can you guys please guide me through this? In nrf_drv_config.h I have already replace:

#define TIMER1_ENABLED 0 
#define TIMER2_ENABLED 0

by

    #define TIMER1_ENABLED 1
 #define TIMER1_ENABLED 1

Anh can you please show me how to use 2 timers in one time? For example, TIMER0 incharge of LED1 and TIMER1 incharged of LED2. And they blink at different period of time, say: 500 ms and 700 ms?. I have tried this following code but the LED just turn on but not blink. Thank you for your support.

Here is my code:

#include "nrf.h"
#include "bsp.h"

#include "nrf_drv_timer.h"
#include "bsp.h"
#include "app_error.h"

#define LED1															11
#define LED2															12

const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(1);
const nrf_drv_timer_t TIMER_LED_1 = NRF_DRV_TIMER_INSTANCE(2);

void led_init(){
	nrf_gpio_cfg_output(LED1);
	nrf_gpio_cfg_output(LED2);
}


/**
 * @brief Handler for timer events.
 */
void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context)
{
    
    switch(event_type)
    {
        case NRF_TIMER_EVENT_COMPARE0:
            LEDS_INVERT(1UL<<LED2);
            break;
        
        default:
            //Do nothing.
            break;
    }    
}

/**
 * @brief Handler for timer events.
 */
void timer_led_event_handler_1(nrf_timer_event_t event_type, void* p_context)
{
    
    switch(event_type)
    {
        case NRF_TIMER_EVENT_COMPARE1:
            LEDS_INVERT(1UL<<LED1);
            break;
        
        default:
            //Do nothing.
            break;
    }    
}

/**
 * @brief Function for main application entry.
 */
int main(void)
{
    uint32_t time_ms = 500; //Time(in miliseconds) between consecutive compare events.
		uint32_t time_ms_1 = 700;
	uint32_t time_ticks, time_ticks_1;
    uint32_t err_code = NRF_SUCCESS;
    led_init();
    //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);
    
		err_code = nrf_drv_timer_init(&TIMER_LED_1, NULL, timer_led_event_handler_1);
    APP_ERROR_CHECK(err_code);
	
    time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_LED, time_ms);
		time_ticks_1 = nrf_drv_timer_ms_to_ticks(&TIMER_LED_1, time_ms_1);
	
    nrf_drv_timer_extended_compare(
         &TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
	
	nrf_drv_timer_extended_compare(
         &TIMER_LED_1, NRF_TIMER_CC_CHANNEL1, time_ticks_1, NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK, true);
    
    nrf_drv_timer_enable(&TIMER_LED);
		nrf_drv_timer_enable(&TIMER_LED_1);
    while(1)
    {
        __WFI();
    }
}

/** @} */
Parents
  • In your comment I think you made a typo, the SD's use Timer0 and RTC0. 1 and above are available for both types of timers.

    You can easily use one timer for multiple things it just involves more code and ISR's. A good example of this is the app_timer. And if you are creating a lot of timers, app_timer is a good resource. Though since it makes use of a lot of ISR's to accomplish what it does, latency can be long and jittery. So, don't expect millisecond accuracy, maybe 10 msec accuracy instead.

    You can find it used in most examples. The beacon example uses it for led's. Here is the documentation: infocenter.nordicsemi.com/index.jsp

Reply
  • In your comment I think you made a typo, the SD's use Timer0 and RTC0. 1 and above are available for both types of timers.

    You can easily use one timer for multiple things it just involves more code and ISR's. A good example of this is the app_timer. And if you are creating a lot of timers, app_timer is a good resource. Though since it makes use of a lot of ISR's to accomplish what it does, latency can be long and jittery. So, don't expect millisecond accuracy, maybe 10 msec accuracy instead.

    You can find it used in most examples. The beacon example uses it for led's. Here is the documentation: infocenter.nordicsemi.com/index.jsp

Children
Related