2022-01-27 01:53 PM
Hi All,
Using an STM32F429 and reading online documentation I have come frequently to this code used for generating delays in Ms if the SYSCLK is 16Mhz
void delayMs(uint32_t n)
{
int i;
for (; n > 0; n--)
for (i = 0; i < 3195; i++) ;
}
I cant understand why the 3195 value is used?
In my understanding for achieving 1Ms of delay we will need
FOSC=16MHz
TOSC=1/16Mhz = 62.5nS
For 1Ms, then I can calculate how many cycles need to be "burned" by the following equation:
My_1_Ms_counter = 1mS / TOSC = 0.001 / 62.5nS = 16000
In summary I will need 16000 dummy instructions to be performed in order to obtain 1Ms of code execution (thus delay).
I come back to my questions of why does the 3195 is used?
Thanks
Solved! Go to Solution.
2022-01-28 06:58 AM
Hi @TDK , yes I also agree. Thank you both
2022-01-28 07:00 AM
Hi @Terry Greeniaus , what a superb explanation thanks. It took me three reads of your post to fully understand what's going on. Great to see how experts can provide these in-depth insights.
2022-01-28 07:39 AM
I didn't, I just determined it was an entire waste of energy and time to get inside the code author(s) head, as to why/how it "worked for them"
Like analyzing Eighteen Century Romantic Poetry, or making 999 non-working light bulbs to get full coverage of the problem space..
2022-01-28 07:46 AM
Indeed a great comparison. Check @Terry Greeniaus post where he elaborates and educates us about it, hope you find it interesting as well. I found it interesting that you claim it's a waste of time while actively replying to this post.
2022-01-29 05:07 AM
Declaring "i" as volatile will help somehow