Go Down

Topic: direct access of PORTD? (Read 798 times) previous topic - next topic

kg4wsv

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: [Select]
 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

mellis

Not sure.  Did you try something like:

Code: [Select]

tone = PORTD;
tone >>= 4;


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

kg4wsv

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

CosineKitty

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.

kg4wsv

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.  :)

Quote
What you want is: PIND, which is the array of input state bits.

That's the ticket!

thanks.

-j

Go Up