Pages: [1]   Go Down
Author Topic: Specifying pin as INPUT not necessary  (Read 1205 times)
0 Members and 1 Guest are viewing this topic.
Firestone, CO
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16518
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: March 30, 2012, 10:04:23 am by retrolefty » Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 439
Posts: 23739
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

"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.
Logged

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.

Belgium
Offline Offline
Edison Member
*
Karma: 58
Posts: 1731
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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 -

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 439
Posts: 23739
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Not 13, but 20.
Do it as a for loop in setup, for example:
Code:
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
}
Logged

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.

Belgium
Offline Offline
Edison Member
*
Karma: 58
Posts: 1731
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Not 13 not 20 but 18 smiley-razz
Or 1 and a loop like crossroad does

Best regards
Janje
Logged

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 -

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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:

}
Logged

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12451
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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 smiley

Logged

Rob Tillaart

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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

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

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 439
Posts: 23739
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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.

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Same as digitalWrite.  Write a 1.

e.g.
PORTB = 1;
PORTD = 1;
Logged

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12451
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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 smiley-wink
Logged

Rob Tillaart

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void setup()
{
}

void loop()
  {
  }

For me was 450 bytes.



And adding the pinModes certainly pushed it up:

Code:
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:
void setup()
 {
 }

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

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



We confirm that here:

Code:
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.
Logged

Pages: [1]   Go Up
Jump to: