Re: [AVR club] STK button detection
2004-01-12 by David VanHorn
> > >Can someone explain to me why the keys is compared by using 2, 4, 8, 0x10, >0x20, 0x40, 0x80!!! And is there other way of detecting it? > > If you look at the port bits as hex numbers, they are 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 So if I wanted to look for bits 0 and 7, I would read the port, and and it with 0x81 The anding operation causes all "1" bits in the input except those that are 1 in the mask, to become zeroes. So if the PIN read in 0xA0: 10100000 in R16,PINB (R16 now contains 0xA0h) and we and with 0x80, we get 10000000 andi R16,0x80 (R16 now contains 0x80) Now if bit 7 was zero, then the result of this operation would have set the zero flag, otherwise zero is cleared, so the next bit of code might be: brne Button_Pressed ;(Branch Non Equal (or non zero)) Clear as mud? :) The other way, is to do sbic PINB,7 ;(Skip if bit in I/O is clear) rjmp Button_Pressed ;Assuming the button gives a high when pressed. Note: Make sure to always read the pin states on the PIN register, and make sure that the bits in the corresponding DDR register are zero to make those pins inputs.