|
From: | David Brown |
Subject: | Re: [avr-libc-dev] Wishlist: place static bool in GPIO registers |
Date: | Thu, 20 Dec 2007 10:01:30 +0100 |
User-agent: | Thunderbird 2.0.0.9 (Windows/20071031) |
Stu Bell wrote:
You can't put a #define in the macro, but you can express yourself a little differently:-----Original Message-----From: Weddington, Eric [mailto:address@hidden Sent: Wednesday, December 19, 2007 1:13 PMany implementation. . . BIT_receive_132 = 1; BIT_recv_error = 0; if (BIT_buffer_status == 1) {} Thanks! Note that "true" and "false" are already defined in <stdbool.h> andshould use those definitions.Sorry, just cut&pasted my little reminder from the post to here. I wouldn't include it.It would be great if someone could volunteer to help work up a fullimplementation... ;-) The real problem with a full workup would be a macro to help define "bit" variables. For example it would be cool to do something like the following: #define DECLARE_BIT_VARIABLE(var,port,bnum) \ #define (name) ((volatile BitRegisterType*)_SFR_MEM_ADDR( (port ) )->bit ## bnum The above code is completely wrong, as the preprocessor won't allow a declare within a declare. Does someone have a clue how to "nicely" declare a bit variable other than putting the above into a long explanation in the comment header/documentation (which will surely be ignored by the noobs)?
#define DECLARE_BIT_VARIABLE(var,port,bnum) \ ((volatile BitRegisterType*)_SFR_MEM_ADDR( (port ) \ )->bit ## bnum Use as: #define BIT_buffer_status DECLARE_BIT_VARIABLE(GPIOR0, 0) (The macro names are a bit unwieldy for my tastes, but that's cosmetics.) For at least 15 years, I've used a set of macros of the form: #define _BitPort(add, bitM) add #define BitPort(bitAdd) _BitPort(bitAdd) #define _BitAdd(bitAdd) (word) &(bitAdd) #define BitAdd(bitAdd) (word) &BitPort(bitAdd) #define _BitMask(add, bitM) bitM #define BitMask(bitAdd) _BitMask(bitAdd) #define MakeBit(port, bitNo) port, (1 << bitNo) #define SetBit(bitAdd) _BitPort(bitAdd) |= _BitMask(bitAdd) #define ResetBit(bitAdd) _BitPort(bitAdd) &= ~_BitMask(bitAdd) #define ToggleBit(bitAdd) _BitPort(bitAdd) ^= _BitMask(bitAdd) #define GetBit(bitAdd) (_BitPort(bitAdd) & _BitMask(bitAdd)) The usage is: #define ledRed MakeBit(PORTC, 0) #define dipSwitch0 MakeBit(PORTB, 5) if (GetBit(dipSwitch0)) { SetBit(ledRed); } else { ResetBit(ledRed); }It is an alternative way to tie the bit number to the port in a single name, giving the programmer the same sort of benefits as the bitfield solution. But not all compilers are equally good at producing efficient code for bitfield expressions, while embedded compilers *should* generate optimal code for the bitmask manipulations in most cases. I've also used equivalent macros for assembly programming on various microcontrollers (though I don't do much assembly now) - it's a very portable system.
mvh., David
[Prev in Thread] | Current Thread | [Next in Thread] |