i am using a c programming, just want to ask if anyone knows how to create a loop like while(), for() or any others than have precise timing.
i want something like a loop that goes for 5 sec to check the condition. after 5 secs turn it off.
hi
generally accurate timing is exact for assembly only.
u can ues jump command to make a loop in mostly all microcontroller in that sense:
loop :
x++
if x=.... jump to...
else ....
( of course that is not a code!!!!!!!!!!)
if the loop doesnt give enough time make another loop inside it.
u could put the condition whereever u like
if you meant that you want to check a condition for 5 sec.
you can use delay for that after you check the condition, after that delay it. For example delay for 50ms and loop it for 100 times. (it won't be exact 5 sec but around 5 sec).
If you are using microcontroler, U can use timer interrupt and example:set it for around 50 ms. Your program will goes to timer interrupt procedure every 50 ms. And then you can make a variable as a flag that it haven't looping for 100 times (you can check your condition in timer procedures). After it's 100 times you just turn off timer interupt.
I agree if a timer is available use it with some counter for exact timing. If not then I suggest that you write the timing portion in assembly and call it from the C code. This will give you more accurate timing than the C code only but still not 100% due to the compiler overhead which is usually hidden from the coder.