avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Bit-wise structure and unions


From: David Kelly
Subject: Re: [avr-gcc-list] Bit-wise structure and unions
Date: Tue, 21 Nov 2006 18:27:33 -0600

On Tue, Nov 21, 2006 at 01:31:13PM -0700, Eric Weddington wrote:

You should really use the C language bitwise operators (|, &, ~, ^,
<<, >>) and use bit masks to manipulate the bits that you want.

The bit-wise operators have one advantage over using the bit-field
structure. Using bit-wise operators you can operate on multiple,
*non-contiguous* bits simultaneously within a variable. You can't do
that with bit-field structures.

I agree with Eric. I'll typically define STATUS_LED_m and STATUS_LED_p and write STATUS_LED_p |= STATUS_LED_m or variations thereof. The generated code won't be any smaller with a bitfield. Eric makes a good point that one can smack multiple bits with one punch this way, not only for smaller code but because often one desires all outputs to change at the same time, not several clock cycles apart.

One other point is that I/O registers often return the value on the pin, not the value previously written. Doubt one wants a read-modify- write instruction to read an incorrect value as will happen if the pin is overloaded. In this case is best to have a copy of the port's intended value laying around in RAM, so one more define for STATUS_LED_r and write STATUS_LED_p = (STATUS_LED_r |= STATUS_LED_m);

Think that will evaluate correctly without parenthesis, but they help when reading.

Some MCU families (believe HC12 is one) have a separate register for reading the port's actual input/external value, and another for reading output latches containing the value last written. A bitfield works fine on the output latch register.

--
David Kelly N4HHE, address@hidden
========================================================================
Whom computers would destroy, they must first drive mad.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]