Loading...
Pages: [1]   Go Down
Author Topic: How to read a port?!  (Read 418 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: 138
Posts: 19067
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.

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: 278
Posts: 25577
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
Offline Offline
Tesla Member
***
Karma: 73
Posts: 6841
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 http://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
Sr. Member
****
Karma: 6
Posts: 400
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
Melbourne, Australia
Offline Offline
Shannon Member
*****
Karma: 219
Posts: 13896
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
Melbourne, Australia
Offline Offline
Shannon Member
*****
Karma: 219
Posts: 13896
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


Austin, TX
Offline Offline
Faraday Member
**
Karma: 41
Posts: 5176
CMiYC
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

www.cmiyc.com - A guide to being an Enginerd

Pages: [1]   Go Up
Print
 
Jump to: