PinMode in Arduino IDE

Hello,
most used EXAMPLES:
pinMode(pin, mode);

DDRx |= B00000001 or DDRx |= 0b00000001
DDRx = 0x01
DDRx |= (1 << PxY)
DDRx &= ~_BV(PxY)

|= OR, &= AND, ^= XOR
DDRx, PORTx, PINx (I know!)

Are there any other alternatives?

As pinMode() is used mostly in setup() does doing it faster really matter in most cases ?

Every different board may have different hardware registers. The examples you give are all
for ATmega based Arduinos only, and are only variations on a theme - the PORTx registers
control pin output - you also need to set the DDRx registers to control pin direction.
More details in the datasheets for the processors.

Boards based on other processor architectures will be completely different. Looking at the Arduino
IDE code for each architecture will show these - these days its mainly in the plugin modules.

Mode is input or output set by the DDRx (data direction) port register. AVR ports have 3 registers each.

"As pinMode() is used mostly in setup() does doing it faster really matter in most cases ?"

When working a matrix it is good to quickly change pin mode.

Since when is "most use" a criteria for suitability?

I know about DDRx, PORTx and PINx. In list are examples only. pinMode is really slow, few times compared to pure C.

So is digitalRead() and digitalWrite(), both 5x as long, as they've been safetied for beginners for good reason.

As it tells in the link I gave you, you can burn pins up if you work it wrong, depending on your circuit.

It has noting to do with C :wink: pinMode() is C as well. It's direct port manipulation what makes it faster. Because you strip all the overhead that makes it quick to write.

Yes, that is faster BUT!:
a) you need to know what you do (aka, know which pin maps where and how not to interfere with other pins)
b) it's not portable (aka, it will only work for that micro and you need to do all the reseach if you want to use it on other mico's.

Aka, it's interchanging speed of writing the program with execution speed.

GoForSmoke:
So is digitalRead() and digitalWrite(), both 5x as long, as they've been safetied for beginners for good reason.

As it tells in the link I gave you, you can burn pins up if you work it wrong, depending on your circuit.

For beginners is good use several times slow digitalWrite. For now, they will blink LED only.

Even for not beginners it's good. :wink: Because a lot of the time you don't need a fast(er) setting of the port but you do need the quick way to write a program (and have it portable / the same on different micro's).

If you do not have time, yes. If you have the time and you need to optimize size code, it is unacceptable that ordinary LED blink (with digitalWrite) should have nearly 1kB of flash memory versus 88 bytes in pure C.

When working a matrix it is good to quickly change pin mode.

Since when is "most use" a criteria for suitability?

As I implied in reply #1 it is horses for courses.

Need to set pinModes in setup() ?
Use pinMode(). It is quick enough, easy and safe to use

Need to change pinMode between INPUT and OUTPUT within the main program ?
Consider using port manipulation if pinMode() is not fast enough but be aware of possible problems

it is unacceptable that ordinary LED blink (with digitalWrite) should have nearly 1kB of flash memory versus 88 bytes in pure C.

When you say it is unacceptable I think that you have lost sight of the origins of Arduino as an easy to use microcontroller.

MarekBujko:
If you do not have time, yes. If you have the time and you need to optimize size code, it is unacceptable that ordinary LED blink (with digitalWrite) should have nearly 1kB of flash memory versus 88 bytes in pure C.

How do you use the spared flash space?

An Ardunino may use all available resources, there is no competition.

If you run out ouf space, you have to optimize, not before.

Which does not mean that wasting memory would be a good thing.

This is pinMode:

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;
 }
}

Yeah so?

Do remember you only need speed when you do specific stuff. Otherwise, time writing is wayyyyyyyy more valuable then more optimized code. Also note, it's even easier to wast more when you do everything yourself.

If you really want to optimize code, don't use Arduino. It's meant to be easy and quick to use and the compiler does a pretty good job at optimizing. And if you reallllllly want to squeeze every bit out of it, use assembler. But do note, than it reallllly comes down to your knowledge and skill to optimize it :wink:

MarekBujko:
This is pinMode:

and your point is what ?

UKHeliBob:
Easy to use microcontroller, yes, but with these macros you will not learn how microcontrollers really works. It is good for start. Arduino could create some project to program these microcontrollers into the depths.

Whandall:
If you have 32 kB Uno, you have to look still at memory.

I'm not here for some kind of war or anything. I'm glad there is something like Arduino, but I personally do not want to stay, just for use macros.

First, it's NOT a macro. So if you want to learn about programming micro controller, see it as a lesson :wink:

True, you're limited. But not as limited as you used to be (20 years back or so). I personally never ran out of memory on a Uno. Simply don't piss it away and you're fine.

And if you really want to learn how a micro really works, you should not be messing with a higher level language like C/C++ but stick to assembler :wink: If you just want to start with micros and programming them (and that seems to be your state) and learn as you go, Arduino and all it's functions is fine. Even with a 32kB Uno :slight_smile:

septillion:
First, it's NOT a macro. So if you want to learn about programming micro controller, see it as a lesson :wink:

True, you're limited. But not as limited as you used to be (20 years back or so). I personally never ran out of memory on a Uno. Simply don't piss it away and you're fine.

And if you really want to learn how a micro really works, you should not be messing with a higher level language like C/C++ but stick to assembler :wink: If you just want to start with micros and programming them (and that seems to be your state) and learn as you go, Arduino and all it's functions is fine. Even with a 32kB Uno :slight_smile:

I added you karma for this post.

MarekBujko:
Whandall:
If you have 32 kB Uno, you have to look still at memory.

You look at the spared memory, giving you what?

But like @septillion mentioned, pinMode is a function, not a macro,
so you only spare some menory if you have one or two pinMode usages.

How would you apply your optimization when you have an array of pins that should become output pins?

And if you look at the pinMode code you posted, it is interrupt save, your method is not.

Portability is the main issue here IMHO.