avr-libc-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-libc-dev] improving delay.h


From: Petrov Florin-Viorel
Subject: [avr-libc-dev] improving delay.h
Date: Tue, 11 Sep 2007 10:48:39 +0200

Hi,

I want to propose improved versions of _delay_ms(double) and 
_delay_us(double) to avoid problems related to shorted delays if the 
user don't keep an eye on 768 us / F_CPU or 262.14 ms / F_CPU 
relations.

Proposed code:
--------------------------
void _delay_us ( double __us )
{
    .....
    else if ( __tmp > 255) {
        _delay_ms ( __us / 1000.0 );
        return;
    }
    else
    ....
}
-------------------------
void _delay_ms ( double __ms )
{
    ....
    else if ( __tmp > 65535 ) {
        __ticks = ( uint16_t ) __ms;
        while ( __ticks ) {
            _delay_loop_2 ( ( F_CPU ) / 4e3 );
            __ticks --;
        }
        return;
    }
    else
    ...
}
-------------------------


As of today, using avr-gcc 4.1.2 from WinAVR 20070525 there is 
virtually no penalty on the target program.
The _delay_us is simply replaced with _delay_ms if timing is too 
long.
The _delay_ms will have only the 'while' loop overhead which is 10 
bytes (program memory) and 4 cpu cycles X number of milliseconds.

Both functions were tested with -O1, -O2, -O3 and -Os optimization 
flags and they compile without problems (i. e. the assembler listing 
is as it should be).

Best regards,
Florin-Viorel Petrov





reply via email to

[Prev in Thread] Current Thread [Next in Thread]