unexpected behavior with xor
I have port rc set to output to leds, and port ra is hooked up to switches. I expect the code to wait until rb.1 is activated. Then I want the information from rb put into location 12, and I want bits 7 to 4 cleared. Then I want this outputed to rc. If I remove the xor line it works except that bits 7 to 4 need to be cleared. However when I have the xor statement, bits 7 to 4 are cleared, and 3 to 0 are set. I was hoping someone could tell me where I'm going wrong. The final code I'm trying to write, will take bits 0 to 3 and then activate an external chip select on ra.0 and then move bits rb.5 to location $12.5, rb8 to location $12.6.
mov W,#$1F ;Allow Direction configuration mov M,W mov !rb,#%11111111 ;Set port B bits 0-7 to input mov !ra,#%00000000 ;Set port A bits 0-7 output bit 0 is select mov !rc,#%00000000 ;Set port C bits 0-7 output bwait jnb rb.1,bp0 jmp bwait ;keep repeating until rb.1 activated bp0 clrb ra.0 ;turn select off ;-------------------bit copy mov W, rb mov $12, W xor $12, #%00001111 ;"1" to Copy the Bit, "0" clears the Bit mov rc, W ;-------------------bit copy jmp bp0
Comments
p q x
F F F
F T T
T F T
T T F
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
0 XOR 0 = 0
1 XOR 0 = 1
0 XOR 1 = 1
AND on the other hand requires both bits to be 1.· So...
1 AND 1 = 1
0 AND 0 = 0
1 AND 0 = 0
0 AND 1 = 0
Also in your code you did an
xor·····$12,·#%00001111······;"1"·to·Copy·the·Bit,·"0"·clears·the·Bit
········mov····rc,·W
This will ALWAYS put %00001111 in rc.··· The way you used XOR is not supporterd by the chip so the assymbler brakes this into two instructions.
MOV W, #%0000111
XOR $12, W
At this point $12 contains the new XORed value, and W contains %00001111.
Also you do not even need to use memory for this one.·· Try.....
MOV W, RB
AND W, #%00001111
MOV RC, W
Post Edited (natpie) : 7/14/2009 10:25:07 PM GMT
aren't we great!? I gave you a hint, hoping you might see the answer - and then Natpie not only answered it, but gave you code too [noparse]:)[/noparse])
Please don't take it as condescending if I say that it helps to hand-write or print out these basic logic tables and pin them on the wall (or what ever works for you) until you've got them memorized. They will come up again and again: in the logic of code flow control, in 'bit-twiddling' values as you need to do, and down in the hardware of chips you'll have to interface. XOR and NAND are the easiest to get wrong or misapply.
cheers!
-Howard
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thanks.
(And sometimes we give more answer than you need! [noparse]:)[/noparse])
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You also might want to look at your JNB RB.1 Unless you've excluded some code here this only runs once then it will will run your XOR code over and over again. Also the the condition you have listed will wait only if RB.1 = 1. JNB is Jump if bit NOT set. So if RB.1 = 0 it will jump to bp0, and never return. If RB.1 = 1 it will continue to your JMP wait condition. So your code will wait while RB.1 is active. JNB is also really two commands. SB somebit and jmp your address (not important in this discussion)
EDIT:
I've been thinking about what you've said, and I think this is along the line of what you are trying to do.
Begin:
SB RB.1
JMP $-1
CLR RA
MOV W, RB
XOR W, #%00001111
AND W, #%00001111
MOV RC, W
MOV $12, W
JMP Begin
Post Edited (natpie) : 7/15/2009 3:59:53 PM GMT