I just spent about 30 minutes tracking down a very annoying bug. The code below is the distillation. Can you spot the problem? (Code should run on any Arduino compatible.)
#define USISIF 7
#define USIOIF 6
#define USIPF 5
#define USIDC 4
#define USICNT3 3
#define USICNT2 2
#define USICNT1 1
#define USICNT0 0
void setup( void )
{
Serial.begin( 115200 );
}
void loop( void )
{
Serial.print(
(1 << USIOIF)
| // USIOIF: Counter Overflow Interrupt Flag; cleared because it is used in the loop below.
// (1 << USIPF) | // USIPF: Stop Condition Flag; cleared for no particular reason.
// (1 << USIDC) | // USIDC: Data Output Collision; cleared for no particular reason.
(0 << USICNT3) | (0 << USICNT2) || (0 << USICNT1) | (0 << USICNT0) // USICNT3:0: Counter Value; cleared because it is used in the loop below.
, HEX );
Serial.print( F(" =? " ) );
Serial.print(
(1 << USIOIF)
// | // USIOIF: Counter Overflow Interrupt Flag; cleared because it is used in the loop below.
// (1 << USIPF) | // USIPF: Stop Condition Flag; cleared for no particular reason.
// (1 << USIDC) | // USIDC: Data Output Collision; cleared for no particular reason.
// (0 << USICNT3) | (0 << USICNT2) || (0 << USICNT1) | (0 << USICNT0) // USICNT3:0: Counter Value; cleared because it is used in the loop below.
, HEX );
Serial.println();
delay( 1000 );
}
I believe gcc is capable of warning about this if you add -Wlogical-op to the command line. Unfortunately, it doesn't appear that the Arduino IDE provides any easy way of adding extra options to the gcc command line.