fabianmoronzirfas
3/20/2014 - 1:32 PM

read_ports_of_microcontroller.ino

/**
 * see http://arduino.cc/en/Reference/PortManipulation#.Uyrot9wh6IF
 *
 * You can read what state a pin has by using bitRead and PORTD or PORTB or PORTC
 *
 * PORTD are pins 0 to 7
 * PORTB are pins 8 to 13 (bits (6 & 7) map to the crystal pins and are not usable)
 * PORTC are analog pins 0 to 5 (Pins 6 & 7 are only accessible on the Arduino Mini)
 *
 * so to get the current state of e.g. pin 13 you do a bitRead(PORTB,5)
 *
 * to get the state of pin 3 do bitRead(PORTD, 3) and so on
 */

int pin  = 13;
void setup(){
  pinMode(pin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  delay(1000);
  digitalWrite(pin, HIGH);
  Serial.println(bitRead(PORTB,5)); //Reads bit 5 of register PORTD which contains the current state (high/low) of pin 13.
  delay(1000);
  digitalWrite(pin, LOW);
  Serial.println(bitRead(PORTB,5)); //Reads bit 5 of register PORTD which contains the current state (high/low) of pin 13.

}