I am using atmel studio 7. I have a key detection logic that works when optimization is turned off(-O0) but does not work when its turned on. I am detecting the key using an ISR there are four keys connected to PORTB 0..3 in a matrix fashion like this Image
PB2 and PB3 is always set as input-pullup. I am switching PB0 and PB1 between Input-Tristate and Output-Low(Playing with the DDRB0 and DDRB1 bits only). Following is the key detection logic(Inside an ISR):
uint8_t tmp=0,v=0;
for(uint8_t i=0;i<2;i++){
DDRB=(DDRB&0xFC)|(0x01<<i);
tmp=(~PINB)&0x0C;if(tmp!=0x00) v=v|tmp|(0x01<<i);
DDRB=DDRB&0xFC;while((PINB&0x0C)!=0x0C); //This line
}
'Optimize for size(-Os)' is selected. When i remove 5th line value of 'tmp' changes when I press any key. Otherwise tmp is always read as zero. I need 5th line for the debounce logic.
So, what am I missing here?