cancel
Showing results for 
Search instead for 
Did you mean: 

DWT_CYCCNT -- explanation

arunease
Associate II
Posted on December 08, 2015 at 13:08

Hi

i have seen the below code on this forum. for example if i wnt to make a delay for 10 seconds then how to calculate the tick value.? Can anyone help ?. volatile unsigned int *DWT_CYCCNT = (volatile unsigned int *)0xE0001004; //address of the register volatile unsigned int *DWT_CONTROL = (volatile unsigned int *)0xE0001000; //address of the register volatile unsigned int *SCB_DEMCR = (volatile unsigned int *)0xE000EDFC; //address of the register //****************************************************************************** void EnableTiming(void) { static int enabled = 0; if (!enabled) { *SCB_DEMCR = *SCB_DEMCR | 0x01000000; *DWT_CYCCNT = 0; // reset the counter *DWT_CONTROL = *DWT_CONTROL | 1 ; // enable the counter enabled = 1; } } void TimeDelay(unsigned int tick) { unsigned int start, current; start = *DWT_CYCCNT; do { current = *DWT_CYCCNT; } while((current - start) < tick); }
3 REPLIES 3
Posted on December 08, 2015 at 13:24

10 * HCLK frequency in Hz

But, generally, don't use ''code I have seen'' unless you understand what does it do. JW
arunease
Associate II
Posted on December 08, 2015 at 13:38

ok.. i understand but not whole.

is it 20160000 for 120ms (168mhz clock)?
Posted on December 08, 2015 at 13:53

The math here is not unduly complicate, nor the frequency/period relationship

168MHz mean it ticks 168,000,000 times in a second

for 120 ms

168,000,000 * 0.120 = 20,160,000

The routine is computing the elapsed time in ~6ns units of time from a free-running 32-bit counter. The 32-bit nature will limit you to about 25.5 seconds in this example.

I'm not sure it's entirely appropriate for 10 second delays, but could be modified to implement a timeout. For 10 seconds, it would perhaps be better to use a slower timer and an interrupt so you're not grinding the processor in a loop doing nothing.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..