0

According to Wikipedia:

GPIO capabilities may include:

  • GPIO pins can be configured to be input or output

  • GPIO pins can be enabled/disabled

  • Input values are readable (typically high=1, low=0)

  • Output values are writable/readable

To my understanding, if a pin is set for input, then it cannot be written. In other words, it is not valid to write a value to an input pin. However, I am not sure whether an output pin is readable.

For example, given a pin numbered 8, after I call function write_pin_value(pin_8, 1);, does the function call read_pin_value(pin_8) always return the value 1?

xmllmx
  • 39,765
  • 26
  • 162
  • 323

2 Answers2

2

You should look at datasheet of a target chip you use, because different ICs may have different input/output buffers structure. Usually documents contain full description of IO ports.

Generally talking firmware can write and read pin in any state, doesn't matter if it is in input or output mode. But writing a value to an input pin will not affect its physical state. Same, if read output pin you may receive undermined result.

You can't write value 0x23 to a pin, because pin's register is 1 bit long, until you work with digital-to-analog peripheral.

1

It depends on the MCU. Some of them has an output latch that gives you a feedback of real output status. You can read it to be sure your output is set t the correct state.

This is also useful in some MCU where you can invert output value (normal high, normal low) and you can check that the real output state is what is expected.

LPs
  • 16,045
  • 8
  • 30
  • 61
  • Hi, LPs, I am a newbie in electronics. Here is the Rsapberry Pi's GPIO circuit specification. [http://www.mosaic-industries.com/embedded-systems/microcontroller-projects/raspberry-pi/gpio-pin-electrical-specifications] Could you determine whether Raspberry Pi's GPIO can read from an output pin after having written to it? Thanks in advance. – xmllmx Apr 13 '16 at 12:09
  • @xmllmx You can always read a gpio port. Pins configured as output will be read at the value you set them last time, or the default value. In you specific case you have not the feature to read back the real state of output, but the logical one. – LPs Apr 13 '16 at 13:59