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

TImer count is not working

Hi All,

The timer count value reading is not working.

static void timer2_init(void)
{

    /* Start 16 MHz crystal oscillator */
    NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
    NRF_CLOCK->TASKS_HFCLKSTART = 1;
    /* Wait for the external oscillator to start up */
    while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)       
    {
        // Do nothing.
    }
    NRF_TIMER2->TASKS_CLEAR = 1;
    NRF_TIMER2->MODE        = TIMER_MODE_MODE_Timer;
    NRF_TIMER2->PRESCALER   = 4;

    /* Load initial values to Timer 2 CC registers */
    /* Set initial CC0 value to anything > 1 */

    //NRF_TIMER2->CC[0]       = 0;//7000;//(32);
    //NRF_TIMER2->CC[1] = 10000;//10ms
    NRF_TIMER2->BITMODE   = TIMER_BITMODE_BITMODE_16Bit;

    //NRF_TIMER2->SHORTS = (TIMER_SHORTS_COMPARE1_CLEAR_Enabled << TIMER_SHORTS_COMPARE1_CLEAR_Pos);
}


int main(void)
{
    uint32_t COUNT;
	
    nrf_gpio_cfg_input(16,GPIO_PIN_CNF_PULL_Pullup);
    nrf_gpio_cfg_output(18);
    nrf_gpio_cfg_output(19);

    timer2_init();

    NRF_TIMER2->TASKS_START=1;

    NRF_TIMER2->TASKS_CAPTURE[0]=1;

    while (true)
    {		
        COUNT=NRF_TIMER2->CC[0];
        if(COUNT == 500)
        {
            NRF_GPIO->OUTSET=1<<18;
        }

        if(COUNT == 1000)
        {
            NRF_GPIO->OUTSET=1<<19;
        }
    }
}

Kindly let me know what is the issue , even i tried in debug by putting breakpoint but CC[0] is not at all incremeneting

  • Reformatted.

    TASKS_CAPTURE[n] = 1;
    

    just does one single capture, so CC[0] will never change. You need to keep capturing it to get an updated value.

    There are also a lot of other (better?) ways to do this like setting up a COMPARE[0] value and waiting for the compare to trigger. You can even do that with an interrupt so your code can sleep instead of burning up the CPU waiting for a count.

Related