Reading pin state

Hi guys!

If I want to check pin state (HIGH or LOW) when pinMode for that pin is set to output, can I do this with digitalRead, or digitalRead only woks when pinMode is set to input?

Also, if I want to check pin state in pure AVR C, where DDR for that pin is set to output, can I just read PIN register for that pin or I need to read PORT register and work with return value?

Thanks!

can I do this with digitalRead

Yes, you can read the state of OUTPUT pins. Not that it makes sense. The state of the pin is what you set it to. Remember that.

can I just read PIN register for that pin

Can you guess what the IN part of that name means?

On the ATmega chips you can not only read the pin state, but it is meaningful - it actually is detecting the
pin state. If a pin happened to be short circuited to ground you could for instance detect this by reading
as LOW when the pin was outputing HIGH. Further to this on the ATmega chips when you write to a pin
set as an output which has an interrupt attached, the interrupt will fire since the interrupt hardware doesn't
care why the pin voltage changed.

On the Due I think things are different - a pin set as an output is no longer monitored as an input, but
I'm not certain (it won't interrupt I'm pretty sure). In general this sort of thing is to be looked up in the
datasheet for the device in question.

PaulS:
Yes, you can read the state of OUTPUT pins. Not that it makes sense. The state of the pin is what you set it to. Remember that.
Can you guess what the IN part of that name means?

Pin INput register.

But, should I read pin state from PIN or PORT register when DDR register for that pin is set to output? What's the difference?

PINx is connected to the pins, PORTx drives the output flip-flops and so will always read back the same you wrote(*). DDRx just enables the output driver from flip-flops to pins.

(*) Not quite true: PINx actually has a second function - if you write to it is magically toggles the flip-flops
where you have a bit set in the byte you wrote.

This is all explained in the relevant section of the ATmega datasheet. Other architectures do things differently
(the Due for instance).

const byte switchPin = 8;

void setup()
{                
  Serial.begin(9600);
  pinMode(switchPin,INPUT_PULLUP);
}
//****************** END of setup() *******************

void loop()
{
  //Get D8 on a UNO
  byte x = PINB & B000001;
  Serial.println(x,BIN);
  
  delay(500);
}
//****************** END of loop() *******************
  //Get D8 on a UNO
  byte x = PINB & B000001;
  Serial.println(x,BIN);
 
  delay(500);

Using non-portable code, for speed, and then delay()ing forever... Why?

Using non-portable code

Yes it is none portable, that is why I mention UNO.

and then delay()ing forever… Why?

Just to slow the serial monitor printout in this very simple example :wink: