??? 09/06/08 10:31 Read: times |
#157999 - Never just C. Responding to: ???'s previous message |
You don't make a 1 minute delay loop in C without using a timer. You don't make any delay loops just relying on C´.
Why? Because you don't have any fixed reference to build your delay upon. With assembler, you have a known number of instructions/second. With C, you can't control how many processor instructions the compiler will insert for a loop. You may find that a 1317-step loop takes 1ms. But changing compiler version or compiler options can greatly change that. A C loop that doesn't have any side effects can even be totally removed from the code - the C compiler is allowed to throw away any code that can be proved to not have any side effects! You do not need to use a timer interrupt for a C delay. It is enough to poll a free-running timer to measure time. Just remember that you often have not access to a free-running timer that will not overflow within 60 seconds, so you should probably create a shorter delay function. Maybe a 1 second delay and a 1ms delay. Then form a 60s delay by 60 iterations of the 1s delay. Just make sure that you always uses unsigned numbers when you work with the timer. unsigned t0; t0 = timer_value; while ((timer_value - t0) < delay) ; The above works even if the timer overflows. But it can only be used for delays shorter than the full period of the free-running timer. And you should not get too close to the full period of the timer. A timer with 65536 ticks and a requested delay of 65500 ticks can result in a twice as long delay if you get an interrupt just before the full delay, i.e. the processor was busy with the interrupt during the narrow time span where the loop would have been able to detect the break condition. |
Topic | Author | Date |
replacing timer by delay loop | 01/01/70 00:00 | |
Never just C. | 01/01/70 00:00 | |
The reason why you must not use C | 01/01/70 00:00 | |
Possible but costly![]() | 01/01/70 00:00 | |
Re | 01/01/70 00:00 | |
Not black-or-white | 01/01/70 00:00 | |
still impossible | 01/01/70 00:00 | |
Still not impossible | 01/01/70 00:00 | |
Hai Per westermark | 01/01/70 00:00 | |
School work? | 01/01/70 00:00 |