Pages: [1]   Go Down
Author Topic: How to read a port?!  (Read 809 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How do you read an entire port (8 bits) and store that value?

For example, I want to read port A since I am using a Mega and I think I am using port A in parallel so pins 22-29, 22 being the LSB. I cannot find anything on google! Has google actually failed me? Or have I failed myself smiley-sad
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26495
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"direct port manipulation"
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I looked on their port manipulation page and from what I understand, you have to make the port an input:

DDRA = 0;

and then read the port

value = PORTA

Am I right?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 634
Posts: 34547
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
you have to make the port an input:
No, you can read both input and output bits at the same time.
Logged

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 129
Posts: 8598
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That will work, but even if a pin is set as an output you can still read it's value.

Also bear in mind that not all ports have 8 pins.

EDIT: As Nick points out below, use PINx to read, PORTx to write.

______
Rob
« Last Edit: May 05, 2012, 10:01:12 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
God Member
*****
Karma: 19
Posts: 785
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 499
Posts: 19070
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 499
Posts: 19070
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
PORTB = PINA;

This would copy 8 inputs to 8 outputs.
Logged


Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Pages: [1]   Go Up
Jump to: