DWT Counts for precise time measurement

Hi I am using DWT for precise time measurement as follows:


  unsigned long d = 10;
  Serial.println("using tobias DWT Timer");
  uint32_t start;
  uint32_t stop;
  uint32_t elapsed;
  // enable DWT
  CoreDebug->DEMCR |= 0x01000000;
  // Reset cycle counter
  DWT->CYCCNT = 0;
  // enable cycle counter
  DWT->CTRL |= 0x1;
  start = DWT->CYCCNT;
  delay(d);
  stop = DWT->CYCCNT;
  elapsed = stop-start;
  Serial.print("cycles for delay: "+String(d) +"ms is: ");
  Serial.println(elapsed);

Q1. it gives different counts per millisecond. Why it is not consistent?

> 
> 50ms
> cycles for delay: 50ms is: 158320
> 
> 5ms
> cycles for delay: 5ms is: 26017
> 
> 
> 1ms
> cycles for delay: 1ms is: 11198
> 
> 
> 5ms
> cycles for delay: 5ms is: 26158
> 
> 10ms 
> cycles for delay: 10ms is: 40902

Q2. What is the frequency of DWT timers, I can not find it in the datasheet of [nRF52840 microcontroller].

"precise time measurement"

What do you mean by "precise timing"?

https://developer.arm.com/documentation/ddi0439/b/Data-Watchpoint-and-Trace-Unit/DWT-functional-description

https://deepbluembedded.com/stm32-delay-microsecond-millisecond-utility-dwt-delay-timer-delay/

The Result For LAB Testing

Here are some screenshots from my DSO showing the timing for various signals. 1uSec, 10uSec, 100uSec, 1mSec delays. And how accurate it actually is. The DWT delay has shown similar results to the timer functions, they are nearly identical. However, the timer delay is much accurate at the low-end very short 1uSec delay. But all in all, it’s as good as it can be.

https://deepbluembedded.com/stm32-delay-microsecond-millisecond-utility-dwt-delay-timer-delay/

Obviously DWT The accuracy of the counter is related to the main frequency of the system

https://chowdera.com/2021/09/20210903192523143Z.html

1 Like

Thanks the response, from "precise measurement" I mean precise or more accurate than then using millis() function of the arduino.
Here i don't understand if the counts for 1ms are 11198 then it should be roughly multiples of 5,10, and 50 for 5ms, 10ms and 50ms.

I mean precise or more accurate than then using millis() function of the arduino

https://www.arduino.cc/reference/en/language/functions/time/delaymicroseconds/

1 Like

Sorry,

https://www.arduino.cc/reference/en/language/functions/time/micros/

1 Like

@hussain123 , your topic has been moved to a more suitable location on the forum. Introductory Tutorials is not for questions. Feel free to write a tutorial once you have mastered your problem :wink:

Which board / microcontroller are you using?

DWT is supposed to run at the CPU clock rate.
But I believe that the Nordic chips might put the cpu in low power modes during “delay”, which adds both jitter and maybe stops the cpu clock. Or something.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.