DDR vs pinMode [SOLVED]

In a recent build I've been moving my code to using registers instead of the built in functions and I've stumbled across an odd phenomenon. If I substitute the pinMode line below for the DDRD line below, the output pin has a distinctive slope when it is cleared.

DDRD |= 0x20;

pinMode(5, OUTPUT);

In my current build, I'm setting the pin then a timer is setup for 60us, after which the pin is cleared. This pulse (set and clear) is visible on both attached graphs but the one has a prominent slope when the pin is cleared. Please note that no hardware was changed between these two graphs.

I've done some research into pinMode() and the only difference I can see is that interrupts are disabled during the DDR assignment within pinMode().

I've tested adding the interrupt disable around the DDRD line in my code with no change in result. I've also tested running the pulse longer (1ms) and the slope is still there.

Can anyone explain why these two lines don't produce the same results?

EDIT:
Something else to note, the rise time has a distinctive curve when using DDR. When using pinMode, the rise time is much quicker. If I didn't know that it was coming from a digital pin, I would think the pin was analog.

EDIT/SOLVED:
See reply #9.

60us Pulse DDR.PNG

60us Pulse pinMde.PNG

The main difference between pinMode and accessing the registers directly is the timing. If you use pinMode the Arduino reads the translation vom the Arduino pin number to the register/bit pair from the flash memory which needs some time.

To give you any feedback on the pictures you posted, you have to post the code that produced them. Small excerpts might be the only difference between two sketches but the rest of the code is usually even more important than the posted parts.

Which one of the following actions you have taken to clear the DPin-5? Please mention pair-wise
(DDRD, ____) and (pinMode(), _____).

PORTD = 0b11011111;//write                 execution time at 16 MHz sysCLK = 4 cycles : 0.25 us
PORTD &=0b11011111;    //read-modify-write  "-------------------- = 4 cycles: 0.25 us
bitClear(PORTD, 5);         //write                     ''------------------- = 4 cycles: 0.25 us
bitWrite(PORTD, 5, 0);    //write                     ''-------------------- = 4 cycles: 0.25 us
digitalWrite(5, LOW);     //write          "--------------------- = 74 cycles: 4.625 us

There's another way of looking at it.

DDRD |= 0x20; takes 2 clock cycles == 0.125us

pinMode(5, OUTPUT); takes 71 clock cycles (do the math)

Below is the code that changes the pin.

  PORTD |= 0x20;

  //Setup timer for first stage of test
  //First step is to end 50kHz pulse
  cli();
  TCNT1 = 0x0000;
  OCR1A = 0x0078; //0078 ~= 60us; 3 pulses at 50kHz
  sei();
  nextState = false;
  // do nothing while waiting for 50kHz to turn off
  while (!nextState) {
  }
  nextState = false;

//  //Timer has triggered; stop firing the SCR
//  digitalWrite(pinMuxEnable, 0);

  PORTD &= 0xDF;

The interrupt associated with OCR1A toggles nextState to advance the code. Bear in mind nothing in this part of the code changed between the two graphs.

FWIW there is no penalty in using bitSet(x,n) so,

PORTD |= 0x20; and bitSet(PORTD,5);

and for portability you can set the macro

#define bitSet(x,n) x |= (1 << n)

@DKWatson, I hadn't seen those functions before.

In an effort to try everything, I changed the code to use bitSet and bitClear with no change.

Shouldn't be a change. In my humble opinion it just makes the code a bit more readable.

There's a jpg of a poster I got from someone some time ago that I enjoy and like to pass around.

Like most programming issues, the problem was self-generated. It took some rubber-duck debugging and writing out discrete bits (1100 vs C) to find it but later on in the code, I set the DDR again and ended up setting this pin as an input.

DDRD &= 0x63;

-vs-

DDRD &= 0x9C;

Thanks to everyone for their input!

wooplogic:
to find it but later on in the code,

Which is why we always ask people to post their full sketch!!!

As attached: