Reputation: 18881
I'm facing some issues with this macro:
#define SHOW(val) PORTB = ((PORTB & 0xFF^OUT_PINS) | ((val) & OUT_PINS));
Let's say I have (defined earlier)
#define OUT_PINS 0b00011110
and PORTB
has some values on other bits that I want to preserve.
The macro was intended to apply val
to PORTB
(OUT_PINS
only) and leave the rest alone.
However, I'm just getting 1's on all output pins.
What's wrong with my code?
Upvotes: 0
Views: 194
Reputation: 18881
Okay so this was a silly mistake.
#define SEG_DOT _BV(PB1)
#define SEG_DIAG1 _BV(PB2)
#define SEG_DIAG2 _BV(PB3)
#define SEG_HORIZ _BV(PB4)
#define BUTTON _BV(PB0)
#define OUT_PINS SEG_DOT | SEG_DIAG1 | SEG_DIAG2 | SEG_HORIZ
#define IN_PINS BUTTON
#define BTN() (PINB & BUTTON == 0)
#define SHOW(val) PORTB = ((PORTB & ~OUT_PINS) | ((val) & OUT_PINS));
As you can see the OUT_PINS
macro does not have brackets around it, so when it's expanded in the SHOW
macro, it all becomes a huge nonsense.
Two possible fixes:
#define OUT_PINS (SEG_DOT | SEG_DIAG1 | SEG_DIAG2 | SEG_HORIZ)
OR
#define SHOW(val) PORTB = ((PORTB & ~(OUT_PINS)) | ((val) & (OUT_PINS)));
I like the first fix better, because the second looks very Lispy. Though, why not use both, after all.
Upvotes: 1