[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-chat] HELP! mega128 USART receive restarts execution
From: |
Rick Mann |
Subject: |
Re: [avr-chat] HELP! mega128 USART receive restarts execution |
Date: |
Sun, 12 Feb 2006 11:21:23 -0800 |
On Feb 12, 2006, at 10:46 AM, David Kelly wrote:
in avr/sfr_defs.h:
#define _BV(bit) (1 << (bit))
in avr/iom128.h:
#define TXCIE0 6
So !_BV(TXCIE0) is !(1<<6) which is usually nonsense and why Larry
got tripped on it.
I don't think it's any more nonsensical than what you do below,
shifting 0s. However, I'm beginning to lean toward that use.
If I were writing the code, first off I would NOT use _BV(). Its a
crutch which hides what is really being done and anyone worth their
salt will instantly recognize (1<<TXCIE0).
I only use it for compactness (I like having space around operators).
The other thing is that I'd either list all the bits on separate
lines as above, but comment out the 0's. Or I'd only list the bits
I'm setting:
UCSR0B = (1<<RXCIE0) | (1<<RXEN0) | (1<<TXEN0);
Another thing I sometimes do to make it positively obvious that I
want a zero in a particular bit, while leaving it easy to toggle
that bit in the future:
UCSR0B = (1<<RXCIE0) | (0<<TXCIE0) | (0<<UDRIE0)
| (1<<RXEN0) | (1<<TXEN0) | (0<<UCSZ02);
If starting from scratch I usually write something like this:
#define TXCIE0_b (6)
#define TXCIE0_m (1<<TXCIE0_b)
Traditionally, Mac OS APIs were written like this, giving both a bit
number and a mask value. However, the technique doesn't let you
explicitly show which bits you want to be zero.
Perhaps better would be to define all the bit names as macros that
generate a mask:
#define RXCIE0m(v) ((v) << RXCIE0)
Even better, when cplusplus is invoked:
inline char RXCIE0m(char v) { return v << RXCIE0; }
With that you could write:
UCSR0B = RXCIE0m(1) | TXCIE0m(0) | UDRIE0m(0);
You could conceivable get fancier, and let multi-bit settings be set
in a single call, but that doesn't work when the bits are
(annoyingly) spread across multiple registers.
--
Rick
smime.p7s
Description: S/MIME cryptographic signature