clrf PORTB ;Turn off all LEDs
movfw PORTA ;Read port A
andlw b'00001110' ;keep only bits 1,2 and 3
xorlw b'00000010' ;invert bit 1
btfsc STATUS,Z ;if it's zero then PortA was 00000010
bsf PORTB,1 ;Turn on LED1
xorlw b'00000100' ;invert bit 2 - bits 1 and 2 are now inverted
btfsc STATUS,Z ;if it's zero then PortA was 00000110
bsf PORTB,2 ;Turn on LED2
xorlw b'00001000' ;invert bit 3 - all three bits now inverted
btfsc STATUS,Z ;if it's zero then PortA was 00001110
bsf PORTB,3 ;Turn on LED3
Not immediately obvious but if you work out what it's doing it should give you a better understanding of the xor instruction.
No, because W is not reloaded, the previous bits stay inverted. If Port A contained '00000110' then after xorlw '00000010', W will contain '00000100'. After xorlw '00000100', W will contain zero. If you reload W from port A then your code would be correct.
Some people write out those operands a little differently (below) where the 'test' value is XOR'ed with the previous 'test' value to cancel out or reverse the effects of the previous 'test' value. This might seem more intuitive since you can see the real 'test' value in the expression (the 1st value).
Code:
clrf PORTB ; Turn off all LEDs
[LEFT] movfw PORTA ; Read port A
andlw b'00001110' ; keep only bits 1,2 and 3[/LEFT]
[LEFT] xorlw b'00000010' ;
skpnz ; 00000010? no, skip, else
bsf PORTB,1 ; Turn on LED1
xorlw b'00000110'^b'00000010'
skpnz ; 00000110? no, skip, else
bsf PORTB,2 ; Turn on LED2
xorlw b'00001110'^b'00000110'
skpnz ; 00001110? no, skip, else
bsf PORTB,3 ;Turn on LED3[/LEFT]
No, because W is not reloaded, the previous bits stay inverted. If Port A contained '00000110' then after xorlw '00000010', W will contain '00000100'. After xorlw '00000100', W will contain zero. If you reload W from port A then your code would be correct.
Hi,
You should update w register from PORTA everytime you've changed the value in the w register. Get what I mean? Before checking button 1 and button 2, load again PORTA to w.
Oh I see then I should add ANDLW b’01110000’ before checking the three buttons. I have added the ANDLW only for the first button. I have to add the other two also before checking them.