Shop OBEX P1 Docs P2 Docs Learn Events
unexpected behavior with xor — Parallax Forums

unexpected behavior with xor

yllawwallyyllawwally Posts: 23
edited 2009-07-15 04:37 in General Discussion
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

  • CounterRotatingPropsCounterRotatingProps Posts: 1,132
    edited 2009-07-14 21:06
    XOR truth table:
    p q x
    F F F
    F T T
    T F T
    T T F

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • yllawwallyyllawwally Posts: 23
    edited 2009-07-14 21:17
    I have bit rb.1 active and bits rb.0,rb.2, and rb.3 as zero, 00000010. Which I believe should make the output to rc be 00000010. However the output is 00001111.
  • natpienatpie Posts: 35
    edited 2009-07-14 22:07
    It sounds to me like you don't want XOR.·· If you want to keep bits 0 to 3 as they are and clear 4 to seven you would AND 00001111.·· Xor (exclusive or)· sets a bit as 1 ONLY if ONE and ONLY ONE of the bits is 1.·· 1 XOR 1 = 0

    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
  • CounterRotatingPropsCounterRotatingProps Posts: 1,132
    edited 2009-07-14 22:16
    yllawwally,

    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

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • yllawwallyyllawwally Posts: 23
    edited 2009-07-14 23:44
    XOR is the instruction I wanted, because it would flip the bits at the same it cleared the unwanted bits, because the LED's light on zero out. It took me a moment to understand what your saying about the 2 instructions. I didn't realize that this statement is broken into two statements modifying the W register. So I need to load $12 back into W, and then load W into rc.

    Thanks.
  • CounterRotatingPropsCounterRotatingProps Posts: 1,132
    edited 2009-07-14 23:51
    great!

    (And sometimes we give more answer than you need! [noparse]:)[/noparse])

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • natpienatpie Posts: 35
    edited 2009-07-15 04:37
    I'm still not convinced what you are describing is XOR. In you example you stated RB = 00000010. You then expected to see the result 00000010 after you XOR the value 00001111. However 000000010 XOR 00001111 = 00001101. Bits 4 to 7 will remain unchanged. In your original post you stated you want to clear the upper 4 bits. However if RB = 01011111, and you XOR that by 00001111 your result would be 01010000. In your original code you placed a comment 1 to copy bit 0 to clear. That is true for AND it is not true for XOR. For XOR it would be 1 to toggle 0 to copy. I'm sorry if I'm misunderstanding what you are trying to do but what you described sounds like you want to use AND. IF you want to flip bits as you stated in your last post XOR will do that, it will however not clear bits. If you want to flip and clear bits that is two steps.

    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
Sign In or Register to comment.