how to prevent digital pin turning LOW during initialization

In the moment the digital pin (I/O 10 in my case) is declared to be an output within the void setup section "pinMode(pin10, OUTPUT);", the pin toggles to LOW immediately. Also when the following command would be "pin10 = HIGH;" the pin first goes to LOW. And the time, the pin is low, is already long enough for my application, to cause a malfunction.

Is there any help to avoid the pin to become LOW level even for just some microseconds? An undefined digital pin is by default set to the high impedance state, and I wanted to switch from that state to HIGH level seamless, without going to LOW level first.

Unfortunately I never experienced that behavior with any other application before, so I already designed and produced the hardware, and now ran into a problem I obviously can't fix just with the programming. Or can I?

Thank you.

"pin10=HIGH" doesn't set pin10 high.

It sets the variable named pin10 to HIGH (which is just #defined to 1)... you need digitalWrite(pin10,HIGH);

You can do:

digitalWrite(pin10,1);
pinMode(pin10,OUTPUT);

In this case, the pullups are turned on, then when the pinMode() call is reached, the output driver is turned on (so at first it will have just a pullup, and then 50 clock cycles or so later, pinMode() will finish.

You can reduce that length of time by instead setting the registers directly - this will run in under a microsecond (I think like half a microsecond at 16mhz?), compared with like... 6 or so (?) with pinMode and digitalWrite, half of which would be the time between the commands.

PORTB |=4; //set PB2 (pin 10) high - pullup will be on now.
DDRB |= 4; //set PB2 (pin 10) output

That's not safe if you might have interrupts that write to those registers firing during the time that code is running, since it's a read-modify-write type thing; if it got interrupted between reading the register, and writing to it, and the interrupt wrote something to the register, it would come back and write to the register - but it had read the old value, not the new one. (you can see in the source for digitalWrite() and friends how they disable interrupts while setting them, and then restore the status register - but since you say you're doing that in setup, I assume that's not an issue - and a great many programs don't have interrupts doing things like that anyway).

As an aside, in two of my favorite chips ('841 and '1634) have slightly different port behavior than just about the entire AVR product line for this issue - they control the pullups with a separate register, so you can set the pin high as input, and it won't do anything until you set it to output.

Thanks DrAzzy, but it was just to make clear what I do, not about the syntax. I did not want to copy all the code, sorry. I know the right syntax.

Thank you for the idea with the direct port addressing, this probably could solve the problem. In the meantime, I found a less nice, but also working solution, by setting the port first to input with activated pull-up resistors, before I set it to an output.

pinMode(whateverportnumber, INPUT);
digitalWrite(whateverportnumber, HIGH);
pinMode(whateverportnumber, OUTPUT);
digitalWrite(whateverportnumber, HIGH);

volkerforster:
pinMode(whateverportnumber, INPUT);
digitalWrite(whateverportnumber, HIGH);
pinMode(whateverportnumber, OUTPUT);
digitalWrite(whateverportnumber, HIGH);

If this is in setup(), you don't need to set pinMode() to input - it's already an input, so that step does nothing. Everything is an input at power on.

You also don't need the second digitalWrite() - The appropriate bit in PORTx is already set when you wrote it high while it was still an input, and it is not unset when you set the pin to be an output.