PDA

View Full Version : Newbie question on reading port staus



SOTASOTA
- 6th December 2012, 21:38
I am sure this is real simple:

OK, so I have inputs on PortB.0 through PortB.7.

How can I read the status of this as a whole?

For example if PortB.0, PortB.1 and PortB.3 are all HIGH then I want to retun the digit "11"....and so forth from 0-127.

Thanks!

HenrikOlsson
- 7th December 2012, 06:08
Hi,
Just read the Port register:


myValue = PortB
That's it.

/Henrik.

EDIT: Make sure to check and setup stuff like ADCON, CMCON for any pins you're going to use.

SOTASOTA
- 8th December 2012, 03:51
Ah, if only all things in life were this easy!
Thanks!!!

SOTASOTA
- 8th December 2012, 14:24
Now, how do I get the value of the Port if the port is less than 8 bits?

Thanks!

HenrikOlsson
- 8th December 2012, 15:57
Exactly the same way as before.
On a 16F877, for example, PortA is 6bits. Reading it will return a byte with the state of the port pins in the lower 6 bits, the two high bits will always read zero.
PortE on the 16F877 is only 3bits. Reading it will return a byte with the state of the port pins in the lower 3 bits, the upper 5 bits will always read zero.

If you look at the register summary in the datasheet for the 16F877 (or whatever device you're actually using) it's all pretty clear.

/Henrik.

SOTASOTA
- 8th December 2012, 18:50
Yes, that makes good sense. But what if I only use say 4 bits of the PortA port in my code and make the other PortA bits as outputs? Would reading the port give me the correct result?
:-)

mister_e
- 8th December 2012, 20:05
it will also return the state of the output. Nothing hard to solve through a bitwise operation or simply by breading the bits you need.

SOTASOTA
- 8th December 2012, 21:25
Awesome!! THX!!