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

how to create a microsecond counter without interrupts?

I need to add a time stamp to the sensor date。

But I dont known how to get the time in microseconds.

Maybe the timer in with 1Mhz can realize this,but I don't known how to config it and how to read the count.

Parents Reply Children
  • I agree.

    If you intend to measure the tie between two events, then you should start the timer, and then capture one CC register at the first event, and another CC register at the second event. Then compare the two, and it will give you the time between the events.

    volatile uint32_t time1;
    volatile uint32_t time2;
    
    void init_timer()
    {
        NRF_TIMER3->BITMODE                 = TIMER_BITMODE_BITMODE_32Bit << TIMER_BITMODE_BITMODE_Pos;
        NRF_TIMER3->PRESCALER               = 0;
        NRF_TIMER3->MODE                    = TIMER_MODE_MODE_Timer << TIMER_MODE_MODE_Pos;
        NRF_TIMER3->TASKS_START = 1;
    }
    
    void event_1_handler()
    {
        NRF_TIMER3->TASKS_CAPTURE[0] = 1;
    }
    
    void event_2_handler()
    {
        NRF_TIMER3->TASKS_CAPTURE[1] = 1;
        
        uint32_t ticks0 = NRF_TIMER3->CC[0];
        uint32_t ticks1 = NRF_TIMER3->CC[1];
        uint32_t diff_ticks = ticks1 - ticks0;  // the timein us depends on the clock configuration. 
    }

    Do you use the softdevice (Bluetooth Low Energy)? If so, you may not get exact times, because the softdevice will always have first priority. In that case, there is something called PPI. You can use this to make the peripherals interact with one another without the need of the CPU. event1 can trigger the first capture, and event2 can trigger the second capture, and you can use the event handler for event 2 to calculate the time difference. 

    If PPI is needed, please check the SDK\examples\peripheral\ppi example.

    Best regards,

    Edvin

Related