Go Down

Topic: Difference between digitalRead and PORTD read? (Read 227 times) previous topic - next topic

bvdb



#define LineAIn 6
#define LineBIn 3
 
pinMode(LineAIn,INPUT_PULLUP);
pinMode(LineBIn,INPUT_PULLUP);
 

The following two statements do not work the same.  Can someone tell me why?


while(digitalRead(LineAIn)&&digitalRead(LineBIn));

while(PORTD&B01001000==B01001000);
 
Regards,

Bert


Coding Badly


Delta_G

When you run the code, what difference do you see?
If at first you don't succeed, up - home - sudo - enter.

johnwasser

The Arduino functions read one pin at a time so the two pins might be sampled several microseconds apart.  PORTD reads all 8 pins at the same time.  Is that important?
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

bvdb

Not a test or quiz.  This is a real problem.

if would have thought that this statement

while(PORTD&B01001000==B01001000);

would cause the micro to loop there until one of the pins goes low then process the next statement.


This code works: 

while(digitalRead(LineAIn)&&digitalRead(LineBIn));

but this code does not wait for one of the lines (pin 6 or pin 3) to drop low:

while(PORTD&B01001000==B01001000);

it goes directly to the next statement.

I'm trying to get this code to run as fast as possible and testing using digitalRead is very slow.

Regards,

Bert






 

sterretje

Use an extra set of ().

The code (probably) compares the first B01001000 with the second B01001000, it does not compare the result of the & operation with the second B01001000.

Try
Code: [Select]
while((PORTD&B01001000)==B01001000);
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Delta_G

Look here.  http://en.cppreference.com/w/cpp/language/operator_precedence

And you'll see that the == does indeed outweigh the &.  Parenthesis will indeed fix it. 
If at first you don't succeed, up - home - sudo - enter.

oqibidipo

Works better if you read PIND instead of PORTD.

MarkT

The fact == binds tighter than & and | is a constant source of annoyance in C, its just counter-intuitive and
wrong, but we have to live with it...
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

bvdb

Hi All,

The statement:

while((PORTD&B01001000)==B01001000);  did not work properly but the statement:

while((PIND&B01001000)==B01001000);   did work.

Thank you all for your suggestions.

Regards,

Bert

AWOL

Hi All,

The statement:

while((PORTD&B01001000)==B01001000);  did not work properly but the statement:

while((PIND&B01001000)==B01001000);   did work.

Thank you all for your suggestions.

Regards,

Bert

Do you understand the difference between an output register (PORT) and an input register (PIN)?
"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.

Go Up