In side ARDUINO Library

Dear All

pinMode(2,OUTPUT);

After executing above code pin 2 of Arduino becomes output

But it is actually

DDRD |= 1<< 2; // Data direction register

My Question is

Can we see the in side the ARDUINO library, above second code

Please advice
Thanks i advance

Yes, you have the source code, you are positively encouraged to go and read it. Quite where it is
depends on which OS and Arduino version, but in something like .../hardware/arduino/avr/cores/arduino/
or .../hardware/arduino/cores/arduino/ inside your install.

PS There's more to pinMode than that, its interrupt-safe for one thing.

Here’s the code for AVR in 1.0.5

void pinMode(uint8_t pin, uint8_t mode)
{
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);
	volatile uint8_t *reg, *out;

	if (port == NOT_A_PIN) return;

	// JWS: can I let the optimizer do this?
	reg = portModeRegister(port);
	out = portOutputRegister(port);

	if (mode == INPUT) { 
		uint8_t oldSREG = SREG;
                cli();
		*reg &= ~bit;
		*out &= ~bit;
		SREG = oldSREG;
	} else if (mode == INPUT_PULLUP) {
		uint8_t oldSREG = SREG;
                cli();
		*reg &= ~bit;
		*out |= bit;
		SREG = oldSREG;
	} else {
		uint8_t oldSREG = SREG;
                cli();
		*reg |= bit;
		SREG = oldSREG;
	}
}

You need to hunt around for some of the machinery it uses, …/…/variants/standard/pins_arduino.h for
instance.

MarkT:
Here's the code for AVR in 1.0.5

void pinMode(uint8_t pin, uint8_t mode)

{
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *reg, *out;

if (port == NOT_A_PIN) return;

// JWS: can I let the optimizer do this?
reg = portModeRegister(port);
out = portOutputRegister(port);

if (mode == INPUT) { 
	uint8_t oldSREG = SREG;

cli();
*reg &= ~bit;
*out &= ~bit;
SREG = oldSREG;
} else if (mode == INPUT_PULLUP) {
uint8_t oldSREG = SREG;
                cli();
*reg &= ~bit;
*out |= bit;
SREG = oldSREG;
} else {
uint8_t oldSREG = SREG;
                cli();
*reg |= bit;
SREG = oldSREG;
}
}



You need to hunt around for some of the machinery it uses, ../../variants/standard/pins_arduino.h for
instance.

Thanks for the reply

Please advice to find this file in arduino library

MarkT:
Yes, you have the source code, you are positively encouraged to go and read it. Quite where it is
depends on which OS and Arduino version, but in something like …/hardware/arduino/avr/cores/arduino/
or …/hardware/arduino/cores/arduino/ inside your install.

PS There’s more to pinMode than that, its interrupt-safe for one thing.

Dear Mark Thanks for the reply

I searched H files but I did not fine it

Use grep (Linux/Mac) or grepWin (Windows). The source is in wiring_digital.c (IDE 1.6.6)