Detect digital pin 0 and 1 as 0 on Oscilloscope

Hey guys,

I am trying to define my pins on Arduino Mega 2560 for detecting digital pin 0(TX0) and 1 (RX0) as 0. My Arduino code is:

void setup()
{
  Serial.begin(9600);
  DDRD = B11111100;
}
 
void loop()
{
  
  for (int i = 0; i < 255; i++){
    PORTD = i;
    delayMicroseconds(1);
  }
  
}

But I still get nothing from 2-7, and I got signal from 0 and 1. Any suggestion for this issue? I am trying to get nothing from 0 and 1 and get signal from 2-7. Thanks a lot!!

This is too fast:
delayMicroseconds(1);

"
Caveats and Known Issues
This function works very accurately in the range 3 microseconds and up. We cannot assure that delayMicroseconds will perform precisely for smaller delay-times."

CrossRoads:
This is too fast:
delayMicroseconds(1);
...
...

I'm not familiar with direct port manipulation, but should the code not toggle the outputs anyway?

Thank you guys for the replies, I've tried to make it slower, says delay(1000); But it still nothing shows up. Any suggestion for this issue? Thank you so much.

Do you want pins 2-7 set as outputs and to only show bits 2-7 of the "i" value?

If you are not using pins 0 and 1 as Serial pins, get rid of the Serial.begin() call. If you ARE using them as Serial, get rid of other expectations of being able to control those pins.

digital pin 0(TX0) and 1 (RX0)

Wrong way round.

Guys, thank you so much for your reply. It’s a dumb mistake, I took off the Serial.begin() and also plug off the USB with my laptop, it’s fine now. But it seems DDRD and PORTD don’t work on Arduino Mega 2560, because I’ve tried the exactly same code on Arduino UNO, it works fine. Is there any suggestion for Arduino Mega 2560??

Thank you so much.

Ahh, looks like the Mega 2560 uses different and multiple ports for pins 0-7.

dlloyd:
Ahh, looks like the Mega 2560 uses different and multiple ports for pins 0-7.
http://diyot.net/wp-content/uploads/2015/12/arduino_mega_2560_pinleri.jpg

This is extremely helpful. So the Digital Port 0~7 are PORTE from 0 to 3, 4 is PROTG, 5 is PORTE, 6 and 7 are PORTH??

Yes.

Looking under the hood, open pins.arduino.h (on my PC its at "C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\mega"). It verifies this.

Here's a thread I refer to from time to time that you might find helpful.