Electronic – How to we read two digital inputs simultaneously from ATxMega128

avrfirmwareinputsensor

I am new here..

I have two IR sensors that give digital output (0 when there is something in front of it and 1 when there isn't).

I am using a ATxMEGA128 to run motors using these sensors but I am unable to read them simultaneously.

The way I am reading 1 pin is :

if(PORTD.IN & (1<<3))

this works perfectly fine but I do not know how to accept two inputs, For instance if my two sensors are on bit 1 and bit 3?

Can anyone please tell me the syntax?

Best Answer

Hassan's answer is not quite accurate. If you really want to read both inputs at the same time, you can do that so long as they both reside on the same port.

uint8_t sample = PORTD.IN;
uint8_t bit3   = (sample >> 3) & 1;
uint8_t bit7   = (sample >> 7) & 1;

Now you can be asured that bit3 and bit7 are from the same time sample. If you use PORTD.IN in multiple places, they are all distinct readings from the pins. Read the PORT.IN into a temporary variable and base your decisions on that sample.