Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: [AVR-Chat] Delay functions and "__builtin_avr_delay_cycles".

2011-07-05 by David Kelly

On Tue, Jul 05, 2011 at 09:35:19AM -0600, Cat C wrote:
> 
> Thank you all for input,There is sooth in what you all say and I tend
> to agree. However, where does it stop?If it takes longer to write
> something because I don't trust any libraries I waste time writing my
> own.

Didn't say that. Said it was your responsibility to review the code and
suggested leaving the "generate disassembly" option enabled to serve
that purpose.

> If I trust them, I may need to spend time fixing IF they break (and I
> know that's likely at some point).

More likely the problem is that they do not produce the time intervals
expected. Another problem is the potential to use unexpected resources.

> I've done just NOP loops sometimes but it's not as nice as being able
> to specify number of microseconds directly.

Yes, but unless you review the code you don't really know its producing
microseconds. You may specify in microseconds but the resolution is
milliseconds. You may specify a big number that fits in the function
prototype only to find a smaller number is all that is used.

> I'm using my timers for other things and I don't want to add ifs when
> triggered either in or outside ISRs.

Its not that bad. Most projects should have something like a 10 ms timer
running all the time that you can multiplex that one timer into multiple
timers very easily. Especially when events don't have to happen exactly
at a specified time but you only need to know if enough time has
elapsed.

In your interrupt service routine do something like this:

	if( timer_waiting_for_Christmas )
		timer_waiting_for_Christmas--;

You can add many of those at almost no execution time cost. If you want
to know when a second has elapsed just put 100 in there and when it
reaches zero you know a second has passed. For the AVR if the timer
variable is larger than 8 bits then you need to block interrupts when
setting or reading. And in all cases the variable needs to be volatile.

If you really have to do something simple when the software timer
expires then:

	if( timer_waiting_for_Christmas )
		if( --timer_waiting_for_Christmas == 0 )
			something_simple;

-- 
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.