matelot:
I am just reading through threads and got confused by this? should it be pinMode(13,OUTPUT);?
JimboZA:
pinMode(13,INPUT);
digitalWrite(13,HIGH);
I.E. can you digitalWrite to an input?
Michael's answer is correct. I'm going to reiterate in case another point of view helps explain what's going on.
Each I/O pin on the AVR is controlled by a "port". AVR Ports are 8-bit pin controllers, meaning each port can control up to 8 I/O pins. There are three registers that define the pin's behavior (in general I/O context, which does not include whatever special function they may be capable of performing -- like serial or PWM). On the AVR, ports are given an alphabetical index (A-L), usually starting from Port A and incrementing to however many ports are necessary to control the number of I/O pins on the chip. Let's take an example of the three registers responsible for Port A:
DDRA: This register defines the I/O direction of the pin -- input or output.
PORTA: This register, when written to, has 8 bits that set the drive level of output pins -- high or low.
PINA: This register, when read, has 8 bits that correspond to the driven level of input pins -- high or low.
Because Atmel likes to be sneaky and give registers multiple purposes, PORTA and PINA also have alternate functions when used in their opposite contexts:
PORTA: For pins that are set as inputs, setting that pin's bit here enables the internal pull-up resistor.
PINA: Writing to this register when the pin is an output toggles its drive level (low->high, high->low).
So, in any version of the IDE, when you do this:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
... it sets the appropriate bit in the DDRx register (where x corresponds to the port representing pin 13) to make the pin an output, and then sets PORTx to drive the pin HIGH. Even though it's not an input pin, you can still read its level by doing this:
int x = digitalRead(13);
... and you will get the level of the pin, which will be whatever you set it to with digitalWrite(). Now, if (again in any version of the IDE) you set the pin as an input:
pinMode(13, INPUT);
... this changes the bit in the DDRx register to change the pin to an input. If the bit in PORTx is still set, like would happen with this explicit sequence:
pinMode(13, INPUT);
digitalWrite(13, HIGH);
... you've set the pin to an input, and written to PORTx, which in input mode, turns on the pull-up resistor. As mentioned, in later version of the IDE, you can also accomplish that same thing like this:
pinMode(13, INPUT_PULLUP);
Behind the curtain, the end result is the same either way, and both are perfectly acceptable. There's no reason you have to use the new syntax. At least not yet -- there's no move to deprecate the old syntax that I know of. In fact, changing this behavior would require digitalWrite() to examine the state of the DDR register and refuse to work if the corresponding pin were an input, which seems pointless to me. It would only add overhead, making the function slower.