I have set up a simple example with an arduino UNO receiving commands on one port (PORTC) and outputting data on another port (PORTD, with serial disabled). Using very basic code below, it works if PORTD is assigned a constant; however, if I try to assign it the value from another variable, it does not work.
byte count = 0;
void setup() {
DDRC = DDRC & 0b11000000;
DDRD = 0b11111111;
}
void loop() {
// If not selected, don't run
if ( (PINC & 0x03) != 0x02 ) return;
// PORTD = 0xd4;
PORTD = count;
if (++count == 0xfe ) count = 0;
}
I then use an arduino MEGA to trigger the UNO and read the bits that are on PORTD. I trigger it every 100ms as a slow test. These are the bytes that are being put onto PORTD by the UNO:
E0,F6,22,36,50,71,95,B4,D0,E5,3,
31,55,72,97,A5,C3,E2,1,12,26,42,
73,93,B7,D3,E3,5,16,32,53,74,83,
which is obviously not counting.
If I replace PORTD = count with PORTD = 0xd4, then it works perfectly well. I have tried changing the type of count to uint8_t to no avail.
Any idea why I cannot set the value of PORTD to that contained in a variable? Thank you!