Go Down

Topic: How to read a port?! (Read 862 times) previous topic - next topic

njdurkin

I understand that all ports may not have 8 bits but I looked it up and in fact, PORTA has 8 bits (:

As far as an input or output, you're saying I don't have to make it an input?

So If I connect 5v to pin 22 I would be able to do a

value = PORTA

and value should now be 1?

even though PORTA is not set as an input?

Delta_G

You don't have to make it an input to read it, but if you read an output it will tell you the value the output is set to.  If it's not set to an input, then it won't read the external signal.

Nick Gammon

For reading, it is PINA. For writing it is PORTA.

Quote
So If I connect 5v to pin 22 I would be able to do a

value = PORTA

and value should now be 1?

even though PORTA is not set as an input?


Er, the pin can't have two values. To read you use PINA. I think, but am not certain, that if you read PORTA (as you did above) you would find only the last value you wrote to PORTA. That isn't necessarily the current state of PINA.

Nick Gammon

In principle, assuming ports are set the right direction and so on, you could do this:

Code: [Select]
PORTB = PINA;

This would copy 8 inputs to 8 outputs.

James C4S


value = PORTA

and value should now be 1?/quote]

As Nick mentions, you need to read PINSA and not PORTA.  Keep in mind that if the pins are floating you'll be reading random values on the unconnected pins.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Go Up