Go Down

Topic: Specifying pin as INPUT not necessary (Read 1 time) previous topic - next topic

drewp63

For those interested in saving a little bit of precious memory, you can forgo the step of coding pinMode( pin, INPUT) if you are initializing the pin for the first time, you do not need this step as ATmel has set all pins to input by default.  It doesn't save much memory only 8 bytes but if your pushing the limits with your code, this is one way to get some of that space back.

retrolefty

#1
Mar 30, 2012, 05:01 pm Last Edit: Mar 30, 2012, 05:04 pm by retrolefty Reason: 1

For those interested in saving a little bit of precious memory, you can forgo the step of coding pinMode( pin, INPUT) if you are initializing the pin for the first time, you do not need this step as ATmel has set all pins to input by default.  It doesn't save much memory only 8 bytes but if your pushing the limits with your code, this is one way to get some of that space back.


Yes, Atmel guarantees that all I/O pins default to input pins upon reset or power up condition. However the bootloader and the arduino sketch initialization code do manipulate some pins so I'm not sure if you can assume that ALL I/O pins are in input mode as you sketch start-up function is called (pins 0,1,13 come to mind). Perhaps one of our software gurus can verify what assumptions one can make for ALL I/O pin modes when the set-up function is called?

However I would recommend that even if you don't explicitly have to change or use the mode command to use a pin for your sketch, that you still explicitly state what pins and mode you are using as comments in the start of your sketch, makes it much easier to understand later and troubleshoot initially.

Lefty

CrossRoads

"what assumptions one can make "
I assume nothing, and define them all either as input with internal pullup enabled, or as output & driven either high or low.
Required for battery operation so the pins do not float & drift between levels consuming power.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Jantje

It is hard to believe removing maximum 13 times pinMode( pin, INPUT) will make a noticeably difference to your program size.

Greeting from Belgium
Jantje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

CrossRoads

Not 13, but 20.
Do it as a for loop in setup, for example:
Code: [Select]

void setup(){
for (int x =2; x<20; x=x+1){ // declare pins 2 thru 19 as input
pinMode (x, INPUT);
digitalWrite (x, HIGH);
}
Serial.begin (9600); // takes care of pin 0, 1
}
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Jantje

Not 13 not 20 but 18 :P
Or 1 and a loop like crossroad does

Best regards
Janje
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

James C4S

Interesting...

Using 1.0, board set to Uno.

1:  BareMinimum (blank setup and loop): 466 bytes.
2:  Manually Setting Up 2-13 As INPUT:  728 bytes.
3:  Loop to set pins 2-13 as INPUT:  652 bytes.
4:  A single pinMode(2, INTPUT):  640 bytes.


Manual code:
Code: [Select]

void setup() {
  // put your setup code here, to run once:
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(9, INPUT);
  pinMode(10, INPUT);
  pinMode(11, INPUT);
  pinMode(12, INPUT);
  pinMode(13, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
}


Loop code:
Code: [Select]

void setup() {
  // put your setup code here, to run once:
  for (int x=2; x<14; x++){ // declare pins 2 thru 13 as input
    pinMode (x, INPUT);
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

robtillaart

@James
You did not test with the direct port manipulations - DDRB DDRD - you can set 8 pins at once ? - http://www.arduino.cc/en/Reference/PortManipulation

DDRB = 0;
DDRD = 0;

might be the smallest one :)

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

James C4S

In the spirit of the drewp63's desire to save memory, we should note that the numbers don't change if configuring all for inputs or outputs.

It's been my experience nearly every project has at least one output in which case, robtillaart is right.  Direct port manipulation is ultimate in saving space:

@James
You did not test with the direct port manipulations - DDRB DDRD - you can set 8 pins at once ? -
DDRB = 0;
DDRD = 0;


Rob is right.  Direct port manipulation is the smallest - 474 bytes. 

Code: [Select]

void setup() {
  // put your setup code here, to run once:
DDRB=0xEF;
DDRD=0xFE;
}


The code won't be as portable but you can configure all bits for input or output in only 8 bytes of overhead.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

CrossRoads

If declared as inputs (or left as defaulted), what's the direct port manipulation to turn on the internal pullups so the pins don't float?
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

James C4S

Same as digitalWrite.  Write a 1.

e.g.
PORTB = 1;
PORTD = 1;
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

robtillaart


Quote
what's the direct port manipulation to turn on the internal pullups so the pins don't float?

DDRD = B01111100;     // set pin 0,1,7 as input, pin 2,3,4,5,6 as output
PORTD = B10000011;   // set pullups high for the pins 0,1,7

or PORTD = ~DDRD;     // maybe even simpler ;)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Nick Gammon


Using 1.0, board set to Uno.

1:  BareMinimum (blank setup and loop): 466 bytes.
3:  Loop to set pins 2-13 as INPUT:  652 bytes.



Yes, but a fair test is not an empty sketch. Otherwise who cares how much memory it takes? Or what the pins are?

This sketch:

Code: [Select]
void setup()
{
}

void loop()
 {
 }


For me was 450 bytes.




And adding the pinModes certainly pushed it up:

Code: [Select]
void setup()
{
 for (int x=0; x< A5; x++)
   pinMode (x, INPUT);
}

void loop()
 {
 }


Now: 636 bytes.




But let's assume that we use pinMode somewhere in our sketch:

Code: [Select]
void setup()
{
}

void loop()
 {
 pinMode (5, OUTPUT);
 }


Now it's 624 bytes. So we only really "lost" 12 bytes.




We confirm that here:

Code: [Select]
void setup()
{
 for (int x=0; x< A5; x++)
   pinMode (x, INPUT);
}

void loop()
 {
 pinMode (5, OUTPUT);
 }


That was 644 bytes. Only 20 bytes more than not doing it.




So, you can set them to inputs if you want. That is, if you don't believe the datasheet. But if you don't believe the datasheet you are going to have trouble programming it anyway.

The only thing I have found inconsistent with the datasheet is that pin 13 seems to be left high (but input) which means the internal pull-ups are enabled. Perhaps the bootloader shouldn't do that.

(edit) This was with version 0022 of the IDE so the actual figures vary slightly from those above.
http://www.gammon.com.au/electronics

Go Up