Pages: [1]   Go Down
Author Topic: direct access of PORTD?  (Read 727 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm connecting a DTMF decoder and trying to access pins 4-7 (that's PORTD 4-7) at the same time to get the value.  digitalRead() works just fine and I can read the data correctly one bit at a time, but if I try to access PORTD I get all 0s.  Here's the code:

Code:
 byte tone;

  tone = (PORTD) >> 4;
  Serial.print(tone, HEX);
  Serial.print(' ', BYTE);
  Serial.print(PORTD, HEX);
  Serial.print(' ', BYTE);
  Serial.print(DDRD, HEX);
  Serial.print(' ', BYTE);

  Serial.print(digitalRead(TONE_DETECT) ? '1':'0', BYTE);
  Serial.print(' ', BYTE);
  Serial.print(digitalRead(Q4) ? '1':'0', BYTE);
  Serial.print(digitalRead(Q3) ? '1':'0', BYTE);
  Serial.print(digitalRead(Q2) ? '1':'0', BYTE);
  Serial.println(digitalRead(Q1) ? '1':'0', BYTE);

Sample output is "0 0 0 1 1110".

What am I doing wrong?

Arduino 07, Mac OS X PPC.

-j
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 11
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Not sure.  Did you try something like:

Code:
tone = PORTD;
tone >>= 4;

Maybe the compiler only recognizes the constants when they're alone?  Not sure.
Logged

0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep, did "tone = PORTD;" and printed it, same result.  PORTD is always 0.  Just for grins I tried DDRD, too, and it's always 0, which diesn't seem right because at least one pin on PORTD is the serial output.

-j
Logged

Florida, USA
Offline Offline
Full Member
***
Karma: 0
Posts: 146
meow!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

DDRD will not work because that is for defining whether each pin is input/output.
PORTD does not work because that is for reading/writing the current output state (HIGH/LOW).
What you want is: PIND, which is the array of input state bits.
Logged

0
Offline Offline
Faraday Member
**
Karma: 8
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
DDRD will not work because that is for defining whether each pin is input/output.
Right, I was just trying to read something that was non-zero for a sanity check.  I can read DDRD to see what the direction bits are set to, right?

Quote
PORTD does not work because that is for reading/writing the current output state (HIGH/LOW).
I'll buy that, but I'm sure I've seen examples that say "PORTD ^= PORTD;" or something along those lines. Of course, the example (or my memory) could be faulty.  smiley

Quote
What you want is: PIND, which is the array of input state bits.
That's the ticket!

thanks.

-j
Logged

Pages: [1]   Go Up
Jump to: