For an AVR (ATMEGA328Por ATTINY of some form) running at 16MHz clocking, is there any faster way to poll a pin until it changes than this (see below) subject to the following requirements:
1.Another pin is to be read as soon as possible after the monitored pin has risen to high.
2.If the pin doesn't go HIGH within a certain time limit, the code should continue, not tay stuck in a while loop forever.
3.After the pin has gone high the code should be able to tell whether the waiting function ended with the pin rising or with the timing out condition.
I tried using a pin change interrupt on the monitored pin, but the time between it rising and the reading of the other pin was pretty long. My use case doesn't need the AVR to be in this state too often, so can tolerate "wasting some clock cycles" doing nothing but waiting and polling the monitored pin, I don't need an interrupt here.
Thank you
//X,Y,Z and W to be set as appropriate for the pins chosen as A and B
#define FastPinARead ( (PINX & _BV (Y)) )
#define FastPinBRead ( (PINZ & _BV (W)) )
#define InwardTimeout 170
//wait until PinA is high
uint8_t value=0;
uint8_t InwardTiming=0;
for(InwardTiming=0; InwardTiming< InwardTimeout; InwardTiming++){//delays until pin rises, or until too much time elapses
if(FastPinARead){
break;
}
}
if(FastPinBRead){
value=1;
}else{
value=0;
}