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-23 by David Kelly

On Thu, Mar 22, 2007 at 09:28:01AM -0700, larry barello wrote:
> One can define a struct for something like the USART peripheral and
> use bit fields.  This has, in fact, been done for the GCC compiler.

BTDT. An inline mask usually results in smaller and faster code. Also
masks are much better when one is watching multiple bits.

> Better yet to use the GCC shorthand _BV() to get the "bit value" of a
> number.
>
> #define _BV(A) (1<<(A))
>
> While (UCSRA & _BV(UDRE))
> 	;

Yuck. I find (1<<5) or (1<<UDRE) immediately says what is happening
_BV(5) adds an extra level of indirection confusion and _BV(UDRE)
doubles the indirection confusion. I don't find (1<<UDRE) to be
confusing.

We're pretty much bound by the definitions in Atmel's datasheets. But
when I don't have a historical mandate and am creating new definitions
for a project I'll do it more like this:

#define UCSRA_UDRE_m	(1<<5)

Which is obvious when reading that it only applies to UCSRA and _m is a
reminder that its already in mask form.

-- 
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.