Hi Hein,
> Thanks for the quick response, I understand what the code does, just
> not why a bitwise AND is performed and what (1<<UDRE) "means". In my
> baby-C, I would probably have written
>
> while (UCSRA.x != 0);
>
> where x is the UDRE bit. Any advantages to the way it's done below ?
Bitfields aren't portable across compilers. The order of the bits
within the bytes, and exactly how many bits are needed, and other
details are all defined as implementation specific.
It isn't even required that they be the same from one version of the
compiler to the next.
Using bitmasks, though, is extremely portable, and you get exactly
what you want. You can also initialize several fields simulataneously
using bitmasks.
I'll often do stuff like:
#define UART0_DATA_BIT_8 (( 1 << UCSZ01 ) | ( 1 << UCSZ00 ))
#define UART0_PARITY_NONE (( 0 << UPM01 ) | ( 0 << UPM00 ))
#define UART0_STOP_BIT_1 ( 1 << USBS0 )
UCSR0C = ( UART0_DATA_BIT_8 | UART0_PARITY_NONE | UART0_STOP_BIT_1 );
which would require 3 separate assignments with bitfields.
--
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/Message
Re: [AVR-Chat] Re: Some C help please !
2007-03-13 by Dave Hylands
Attachments
- No local attachments were found for this message.