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-22 by larry barello

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.  Some folks use
these definitions, but most stick with the shift & mask technique
illustrated below as bit fields are un-portable (you may say, why bother
with something like the AVR, but I used them for an H8 project and got
totally bitten when we needed to transport data across Bluetooth to a PC and
pocket PC devices).

The XXXX.B notation comes from BASCOM and who knows what other non-ansi C
like compilers.  It is a great shorthand, but still broken (IMHO) because
raw numbers are terrible as a way of documenting what is the intent of the
code.

For example (not correct since I don't have my data sheet handy...)

typedef struct
{
	unsigned char UDRE:1;
 	unsigned char UDTXC:1; 	
	unsigned char UDRX:1;	
	...
}
UCSRA_t;

#define UCSRA (*(UCSRA_t*)0xXXXX)	// 0xXXXX SRAM address of peripheral

...

while (UCSRA.UDRE == 0)
	;

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))
	;

-----Original Message-----
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On Behalf
Of David Kelly
Sent: Thursday, March 22, 2007 9:04 AM
To: AVR-Chat@yahoogroups.com
Subject: Re: [AVR-Chat] Re: Some C help please !

On Thu, Mar 22, 2007 at 11:28:53AM -0400, Mike Bronosky wrote:
> All this is fine. But for many of us hobbyists that know just enough
> about C, especially when it comes to AVRs, to be
> down-right-dangerous...
> 
> For me something like the following, is good, proper and works. It is not
> fully understood until I dissect.
> while ((UCSRA & (1<<UDRE))==0);
> 
> while(UCSRA.5 == 0);  //while(USART Data Register Empty)
> Haven't tried this but I think it should work and means "while the
> data register is empty sit right here".

*I* see what you intend but I don't think the compiler will. "5 is not a
member of the struct UCSRA". In fact UCSRA isn't a struct at all so the
dot thing won't work.

Also suggest one pretty-print the while() usage something like this so
that it stands out like a sore thumb that one has a small loop:

	while ((UCSRA & (1<<UDRE))==0)
		;

> Now to the Xsperts may disagree. I'm a hobbiest, it works for me and
> best of all I understand why it works.

As a general rule its always bad to write loops with only one possible
exit. At the least you should provide a time out exit. Or enable the
watchdog timer and kick the dog once prior to entering the above loop.

-- 
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.


 
Yahoo! Groups Links

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.