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

Unable to Run timer1 with S110

I'm developing application on nrf51 for that i'm using... SDK : nRF51_SDK_8.1.0_b6ed55f Board : PCA10028 I'm trying to run code from SDK examples C:\nRF51_SDK_8.1.0_b6ed55f\examples\peripheral\timer\pca10028\armgcc in SDK with s110 softdevice. it is compiling well but at run time, leds are not blinking. it seems like execution stuck anywhere or changing value of timer delay in variable time_ms is not being reflected. I have used timer1. without using s110 softdevice it blinks well..my code look like

/** @file
 * @defgroup nrf_dev_timer_example_main main.c
 * @{
 * @ingroup nrf_dev_timer_example
 * @brief Timer Example Application main file.
 *
 * This file contains the source code for a sample application using Timer1.
 *
 */

#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "bsp.h"

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

const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(1);

const uint8_t leds_list[LEDS_NUMBER] = LEDS_LIST;

/**
 * @brief Handler for timer events.
 */
void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context)
{
    static uint32_t i;
    uint32_t led_to_invert = (1 << leds_list[(i++) % LEDS_NUMBER]);
    
    switch(event_type)
    {
        case NRF_TIMER_EVENT_COMPARE1:
            LEDS_INVERT(led_to_invert);
            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_ticks;
    uint32_t err_code = NRF_SUCCESS;
    
    //Configure all leds on board.
    LEDS_CONFIGURE(LEDS_MASK);
    LEDS_OFF(LEDS_MASK);
    
    //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_CHANNEL1, time_ticks, NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK, true);
    
    nrf_drv_timer_enable(&TIMER_LED);

    while(1)
    {
        __WFI();
    }
}

can anyone help for this..? thank you....

  • uint32_t time_ms = 500;
    

    You are using Timer1 which is only 16-bit wide. By default it it uses 16MHz clock and prescaler of 0, which means that it generates 16000000 ticks per second. So for 500ms it generates half of that which is 8000000(0X7A1200) ticks. The maximum TIMER1 can take is 0XFFFF (65535 ticks). Which means that when you try to write 0X7A1200 into this 16-bit register it only writes 0X1200 in it. Which in turn configures your timer to generate interrupt very 287us. Your leds are blinking but really really fast.

    So you have to make sure that your 500ms fits into your timer. This you can achieve by changing the prescaler of TIMER1 to

    #if (TIMER1_ENABLED == 1)
    #define TIMER1_CONFIG_FREQUENCY    NRF_TIMER_FREQ_125kHz
    

    Now Timer1 will generate only 125000 ticks per second, so for 500ms it will generate 625000(0XF2F4) ticks. Which will fit nicely for you case.

    The higher the timout value, lower should be timer tick frequency, else the tick wont fit in.

Related