bvdb
May 7, 2017, 9:24pm
1
#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
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?
bvdb
May 8, 2017, 5:09am
5
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
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
while((PORTD&B01001000)==B01001000);
Works better if you read PIND instead of PORTD.
MarkT
May 8, 2017, 10:16am
9
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...
bvdb
May 20, 2017, 4:17am
10
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
system
May 20, 2017, 6:45am
11
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
Do you understand the difference between an output register (PO RT) and an input register (PI N)?