detecting if a pin is set for INPUT or OUTPUT

Can you detect the Mode of a pin?

I want to write a function to dump the state of the machine to the serial port.

Alan

You could read the appropriate DDR register.

Search the forum, I seem to remember discussing this a year or so ago. Might even be some code, a function to return the pin mode or similar.

Found it: http://arduino.cc/forum/index.php/topic,118153.msg889410.html

(And it wasn't nearly a year ago, I'm even worse at estimating time than I thought!)

Maybe you should get away from those 8 MHz boards then ;)

CrossRoads: Maybe you should get away from those 8 MHz boards then ;)

LOL! I think you may be right! :D And I thought my Arduino's clock wasn't very accurate!

Thank you for that, it set me off on the right trail. Incidentally http://www.arduino.cc/en/Reference/PortManipulation gives a good introduction to it all.( I can not see how to navigate to it from the Arduino Home Page.)

I will publish some code that will make the state of a pin more easily accessible in a few days (including whether it is being used for PWM ). If there is anything specific that anyone needs let me know here.

Here is a simple class that gives pinMode,value and PWM value for each pin

It has a small bit of output for testing at the end

class State
{
  /* this is a basic class that provides easy access tfor each pin
     - whether it is set as INPUT or OUTPUT in InOut[];
     - it's current value HIGH or LOW in HighLow[];
     - the value of its PWM if being used (0 = not being used) in PWM[]
     /*
  
  public:
  bool InOut[20];   // setas Output (1) or Input (0)
  bool HighLow[20]; // set  High (1) or LOW (0) taken from PORT or 
                    // PIN depending on DDR
  byte PWM[20];     // PWM can be on pins 3,5,6,9,10,11,12 this holds the value
                    // this holds the value = 0 if not in use
 
  State(){};
  void calcValues();
};
void setup()
{
 Serial.begin(9600);
  State test=State();
  test.calcValues();
}

void loop()
{

}

void State::calcValues()
{
  // For each port B,C,D
  //
  // DDR  = INPUT (0) or OUTPUT (1) set 
  // PORT = HiGH (1) or LOW (0) for OUTPUT pins
  // PIN  = HIGH (0) or LOW (0) for INPUT pins 
  // 
  // port B pins 8-13 in first 6 positions 
  // port C pins Analog pins A0-A5 in first 6 positions
  // port D pins 0-7 in first 8  positions
  //
  // InOut,HighLow,PWM hold info for the relevent Pin No. 
  // for Analog Pins there info starts at position 14 for A0
  // and ends in Position 19 for A5
  
  /*bool InOut[20];   // setas Output (1) or Input (0)
  bool HighLow[20]; // set  High (1) or LOW (0) taken from PORT or 
                    // PIN depending on DDR
  byte PWM[20];     // PWM can be on pins 3,5,6,9,10,11,12 this holds the value
                    // this holds the value = 0 if not in use
 
  */
  // Port B
  int offset=8;
  
  for(int i=0;i<7;i++)
  {
    if(DDRB & (1<<i))
    {
      InOut[offset+i]=OUTPUT;
      HighLow[offset+i]=PORTB & (1<<i);
    }
    else
    {
      InOut[offset+i]=INPUT;
      HighLow[offset+i]=PINB & (1<<i);
    }
  }
  // Port C
  offset=14;
  
  for(int i=0;i<6;i++)
  {
    if(DDRC & (1<<i))
    {
      InOut[offset+i]=OUTPUT;
      HighLow[offset+i]=PORTC & (1<<i);
    }
    else
    {
      InOut[offset+i]=INPUT;
      HighLow[offset+i]=PINC & (1<<i);
    }
  }
  // Port D
  offset=0;
  
  for(int i=0;i<8;i++)
  {
    if(DDRD & (1<<i))
    {
      InOut[0+i]=OUTPUT;
      HighLow[offset+i]=PORTD & (1<<i);
      
    }
    else
    {
      InOut[0+i]=INPUT;
      HighLow[offset+i]=PIND & (1<<i);
    }
  }
  // do PWM
  
  // set allto zero
  for (int i=0;i<20;i++)
  {
    PWM[i]=0;
  }
  //set the PMW pins
  PWM[6]=OCR0A;
  PWM[5]=OCR0B;
  PWM[9]=OCR1A;
  PWM[10]=OCR1B;
  PWM[11]=OCR2A;
  PWM[3]=OCR2B;
  
  // test  
 
  for(int i=0;i<20;i++)
  {
    Serial.print("pin ");
    Serial.print(i);
    Serial.print(" ");
    Serial.print(InOut[i]);
    Serial.print(" ");
    Serial.print(HighLow[i]);   
    Serial.print(" "); 
    Serial.println(PWM[i]);
  }
    
}

Here is a simple class that gives pinMode,value and PWM value for each pin

It has a small bit of output for testing at the end

Keep in mind that the arduino platform now supports many different types of AVR chips that have different port/pin/function setups and I suspect this class just works correctly on 328 based boards? So at least clear comments at the beginning to state which arduino boards this will run on correctly is probably a good thing to consider adding.

Lefty

I am sure you are correct. It works for the ATmega8 and ATmega168.

  State test=State();

You do NOT call the constructor directly. Ever.

I am new to this . What should I put:

What should I put:

State test;