Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » AVR-GCC
  • » [avr-gcc-list] How to (efficeiently !!!) test abitwithinamulti-byte intege [RSS Feed]

#1 Nov. 4, 2005 22:26:53

Dave H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


From: "David Brown" <>
The bug is almost certainly a delay loop variable that is not declared
volatile. The delay function is probably something like:

void shortDelay(void) {
uint8_t n = 50;
while (n--);
}That (might) work fine with little or no optomisation, but whenoptomisationis on, it fails. The solution is simply to make n "volatile". Note thatitwill not give you precise control of your delay - different optomisationsordifferent compiler versions might give slightly different code, and may or
may not inline the function. But in a case like this it doesn't matter -
all you are looking for is a slight delay, and it doesn't matter if it istoo long. I've done exactly this sort of thing (with "volatile",naturally)myself for LCD routines.FWIW, I always had good luck with the delay functions in delay.h for shorthardcoded (usec) delays. I don't have access to the code now, but I had amacro that calculated the minimum number of trips through the four-cycleloop 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 #includingavr/delay.h.Regards,
-=Dave




_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#2 Nov. 4, 2005 23:56:12

Dave H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


> 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, Canadahttp://www.DaveHylands.com/_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#3 Nov. 5, 2005 18:06:41

Vincent T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


> FWIW, I always had good luck with the delay functions in delay.h for short
> hardcoded (usec) delays.

Lucky you ! Other than wanting to avoid all this in-line stuff, the
reason I replaced _delay_us(40) in my lcd routine, by an empty for loop,
is that I accidentally realised that _delay_us(40) produced a 1,500us
delay somehow !!! Couldn't figure out why, I set the F_CPU #define
properly, and _delay_ms() works perfectly, but _delay_us clearly
doesn't. Might be a bug with the particular delay of 40us, don't know,
as don't use us delays anywhere else in my program.


--
Vince



_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#4 Nov. 5, 2005 19:25:08

Joerg W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


Vincent Trouilliez <> wrote:

> Lucky you ! Other than wanting to avoid all this in-line stuff, the
> reason I replaced _delay_us(40) in my lcd routine, by an empty for
> loop, is that I accidentally realised that _delay_us(40) produced a
> 1,500us delay somehow !!! Couldn't figure out why, ...

Bug report #14224, has been resolved meanwhile.

--
cheers, J"org .-.-. --... ...-- -.. . DL8DTLhttp://www.sax.de/~joerg/NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)



_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#5 Nov. 5, 2005 19:56:15

Vincent T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


On Sat, 2005-11-05 at 20:24 +0100, Joerg Wunsch wrote:
> Vincent Trouilliez <> wrote:
>
> > Lucky you ! Other than wanting to avoid all this in-line stuff, the
> > reason I replaced _delay_us(40) in my lcd routine, by an empty for
> > loop, is that I accidentally realised that _delay_us(40) produced a
> > 1,500us delay somehow !!! Couldn't figure out why, ...
>
> Bug report #14224, has been resolved meanwhile.

Ah great, thanks for fixing it :-)
The bug relates to _delay_ms() though, which I have never problems with.
It's _delay_us() that misbehaved. But I guess the two are related... so
I will just have to install the latest version of avr-libc I guess...

--
Vince



_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#6 Nov. 5, 2005 20:35:44

Joerg W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


Vincent Trouilliez <> wrote:

>> Bug report #14224, has been resolved meanwhile.

> The bug relates to _delay_ms() though, which I have never problems
> with. It's _delay_us() that misbehaved. But I guess the two are
> related...

Both have been implemented similarly, so I'd assume they suffered
from similar problems.

> so I will just have to install the latest version of avr-libc I
> guess...

Alas, the fix is not yet in any released version of avr-libc, I just
checked. So you'd either need to pull the fix straight from CVS (as
all this is just in a header file, the fix would be self-contained,
you don't need more than that file itself), or you gotta wait a bit.
Both, avr-libc 1.4.0 as well as 1.2.6 will be there ``really soon
now'', as they say...

--
cheers, J"org .-.-. --... ...-- -.. . DL8DTLhttp://www.sax.de/~joerg/NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)



_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

#7 Nov. 5, 2005 21:15:33

Vincent T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

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


> Alas, the fix is not yet in any released version of avr-libc, I just
> checked. So you'd either need to pull the fix straight from CVS (as
> all this is just in a header file, the fix would be self-contained,
> you don't need more than that file itself), or you gotta wait a bit.
> Both, avr-libc 1.4.0 as well as 1.2.6 will be there ``really soon
> now'', as they say...

Well I fell lazy, so I will just wait for 1.4.0 ;-)


--
Vince



_______________________________________________
AVR-GCC-list mailing list
AVR-GCC-list@nongnu.orghttp://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Offline

  • Root
  • » AVR-GCC
  • » [avr-gcc-list] How to (efficeiently !!!) test abitwithinamulti-byte intege [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support