Yahoo Groups archive

AVR-Chat

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

Message

Re: [AVR-Chat] Re: Some C help please !

2007-03-25 by Ned Konz

I wrote:

> So, using my headers, your example test for tx completed or empty could 
> be written:
> 

[snip]

>        if (UCSR0B & ((1 << bitTXC0) | (1 << bitUDRE0)))
>           /* do stuff */
> 
> 
> and the generated code would look like this:

[snip]

> or this (somewhat more efficient):
> 
> 
>    15:test2.c  ***       if (UCSR0B & ((1 << bitTXC0) | (1 << bitUDRE0)))
> 
>   324 0010 8AB1      		in r24,42-0x20
>   325 0012 8076      		andi r24,lo8(96)
>   326 0014 31F0      		breq .L5
>    *** /* do stuff */
>   338                   .L5



And here we see the problem with the bit numbers: they're not connected 
with the register.

I made the mistake of referring to UCSR0B, when TXC0 and UDRE0 are 
actually in UCSR0A.

This is a not-uncommon error using this idiom. You don't have the 
ability to make this error when using bitfields.

The above should be:

         if (UCSR0A & ((1 << bitTXC0) | (1 << bitUDRE0)))
            /* do stuff */


I spent some time making a safer version using C++ templates, but 
haven't been satisifed with its ease of use.

-- 
Ned Konz
ned@bike-nomad.com
http://bike-nomad.com

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.