Programming PINMODE(xx,ZZ);

HI,

According to : https://forum.arduino.cc/index.php?topic=448152.0 programming pinmode (xx,ZZ) in a loop in not a smart idea.

But:

I use my ARD 2650 as an experimental board controlled by an external Delphi program, sending commands to the ARD, that does what it is supposed to do, and giving respons(es) to the calling Delphi program. It means that I don't know how the different ports/registers (DDRx and PORTx) will be configured at runtime / executing the projects.

I don't want to (re)compile the ARD-code whenever I start a new project, so I am searching for a more general routine, that can set the ports ( eg.: DDRx and PORTx) to the project dependent mode.

I intend to use the PINMODE command programming the DDRx - registers (and DIGITALREAD / DIGITALWRITE commands when reading/writing to the ports.)

I konow I can use the command:

DDRx = 0xFF; and DDRx = 0x00; (out / inp )

But it's faster (for me - not necessary programmatic) to use the PINMODE-command.

New project:

PORT A will be OUTPUT and port A will be input:

Set DDRA as OUTPUT:

for (int n = 22; n = 29; ++n) { PINMODE(n,1); }

(pin 22 --> pin 29 is portA[0] --> portA[7] )

and

Set DDRB as OUTPUT:

for (int n = 10; n = 13; ++n) { PINMODE(n,0); } for (int n = 19; n = 23; ++n) { PINMODE(n,0); }

These commands will be only RUN ONCE per project.

an alternative could be:

Running the Command PINMODE(N,OM); //N = pinnumber, OM = i/o-Mode from the Delp. prgm 8 time (per port) (an eqivivalent in the ARD-code).

If this is possible it will reduce the code-overhead in the ARD quite significant, thereby speeding it up, and speed is a critical factor.

These "setup - routines " will NOT be in the void setup() section

So the question is:

Is this possible ?

Kris aka snestrup2016

snestrup2016:
According to : https://forum.arduino.cc/index.php?topic=448152.0 programming pinmode (xx,ZZ) in a loop in not a smart idea.

No, you read it wrong. Unnecesarily calling pinMode() (note C++ is case sensitive, please use the correct case in your function names) over and over again in a loop is not a smart idea. For example this is silly:

void setup() {}

void loop() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

because since the pin is only ever used as an output and you know it will be used as such from the beginning you can just call pinMode() once in setup():

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

snestrup2016:
for (int n = 22; n = 29; ++n)
{
PINMODE(n,1);
}

(pin 22 → pin 29 is portA[0] → portA[7] )

and

Set DDRB as OUTPUT:

for (int n = 10; n = 13; ++n)
{
PINMODE(n,0);
}
for (int n = 19; n = 23; ++n)
{
PINMODE(n,0);
}

These commands will be only RUN ONCE per project.

That’s perfectly fine. But note that the Arduino standard API function name is pinMode(), not PINMODE().

snestrup2016:
speed is a critical factor.

Well you should know that the Arduino I/O functions are not very efficient. They are intended to be beginner friendly and portable but are typically much slower than writing directly to the registers.

snestrup2016:
These "setup - routines " will NOT be in the void setup() section

That doesn’t matter.

HI,

Thanks for updating me. (pinMode). Glad I read it wrong (hehe).

And, I know I wrote the PINMODE command wrong (should be pinMode ).

I assumed you read it as Pseudo-code (as you did, thanks a lot ) and I did it to put attention to that particular part of the code , deliberately ignoring syntax.

Of course, in real code I use pinMode(zz,yy);

Again Thanks for updating me.

Kris aka snetrup2016