avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] How to (efficeiently !!!) test abitwithinamulti-byte


From: Dave Hylands
Subject: Re: [avr-gcc-list] How to (efficeiently !!!) test abitwithinamulti-byte intege
Date: Fri, 4 Nov 2005 15:54:45 -0800

> FWIW, I always had good luck with the delay functions in delay.h for short
> hardcoded (usec) delays.  I don't have access to the code now, but I had a
> macro that calculated the minimum number of trips through the four-cycle
> loop that would guarantee the specified delay.  Something like
>
>    #define usec_count_(x) ((OSC_FREQ*(x))/4000000)
>    #define delay_us(x) _delay_loop_2(usec_count_(x))
>
> It looks like the library also provides _delay_us, which might be better,
> though I've never tried it.  Requires you to #define F_CPU before #including
> avr/delay.h.

I picked up this one somewhere:

#define LOOPS_PER_MS (F_CPU/1000/4)
#define LOOPS_PER_US (LOOPS_PER_MS/1000)

/* spin for us microseconds */
void us_spin(unsigned short us)
{
        if (!us)
                return;

        /* the inner loop takes 4 cycles per iteration */
        __asm__ __volatile__ (
                "1:                     \n"
                "       ldi r26, %3     \n"
                "       ldi r27, %2     \n"
                "2:     sbiw r26, 1     \n"
                "       brne 2b         \n"
                "       sbiw %0, 1      \n"
                "       brne 1b         \n"
                : "=w" (us)
                : "w" (us), "i" (LOOPS_PER_US >> 8), "i" (0xff & LOOPS_PER_US)
                );
}

/* spin for ms milliseconds */
void ms_spin(unsigned short ms)
{
        if (!ms)
                return;

        /* the inner loop takes 4 cycles per iteration */
        __asm__ __volatile__ (
                "1:                     \n"
                "       ldi r26, %3     \n"
                "       ldi r27, %2     \n"
                "2:     sbiw r26, 1     \n"
                "       brne 2b         \n"
                "       sbiw %0, 1      \n"
                "       brne 1b         \n"
                : "=w" (ms)
                : "w" (ms), "i" (LOOPS_PER_MS >> 8), "i" (0xff & LOOPS_PER_MS)
                );
}

The ones in avr/delay.h use the same core loop.

--
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/




reply via email to

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