ON 20050124@7:21:17 PM at page: http://techref.massmind.org/techref/microchip/math/bit/cuabrr.htm#38376.8064467593 James Newton[JMN-EFP-786] Questions: Question:
I have a register that holds a 3 bit number <0:2>. How do I put the value of those three bit on PORTC <7:5> without changing the rest of the bits on PORTC?
Andrew Warren answers
If you had stored the 3 bits in SOURCE bits 5-7 instead of bits 0-2, you'd have been able to do it in 4 lines:
    MOVF    SOURCE,W   ;W = SOURCE.
    XORWF   PORTC,W    ;Copy W bits 5-7 to PORTC bits 5-7.  This
    ANDLW   11100000B  ;code only works if the upper three bits
    XORWF   PORTC      ;of PORTC are guaranteed not to change
                       ;between the two XORs.
Since the data's stored in bits 0-2, though, you have to do a little more... If you have an extra register to spare, you can do it in 6 lines:
    SWAPF   SOURCE,W   ;Copy SOURCE bits 0-2 to W bits 5-7.
    MOVWF   TEMP       ;
    RLF     TEMP,W     ;

    XORWF   PORTC,W    ;Copy W bits 5-7 to PORTC bits 5-7.  This
    ANDLW   11100000B  ;code only works if the upper three bits
    XORWF   PORTC      ;of PORTC are guaranteed not to change
                       ;between the two XORs.
Without a TEMP register, you can do it in 8 lines. Unfortunately, this code glitches PORTC bits 5-7 low, and it switches the pins one at a time:
    MOVLW   00011111B  ;Clear the upper 3 bits of PORTC.
    ANDWF   PORTC      ;

    BTFSC   SOURCE,2   ;Copy SOURCE bits 0-2 to PORTC bits 5-7.
    BSF     PORTC,7    ;
    BTFSC   SOURCE,1   ;
    BSF     PORTC,6    ;
    BTFSC   SOURCE,0   ;
    BSF     PORTC,5    ;
The shortest way (9 cycles/instructions) that glitchlessly switches all three PORTC pins simultaneously without using any additional registers:
    BTFSC   SOURCE,2   ;Copy SOURCE bits 0-2 to W bits 5-7.
    ADDLW   10000000B  ;
    BTFSC   SOURCE,1   ;
    ADDLW   01000000B  ;
    BTFSC   SOURCE,0   ;
    ADDLW   00100000B  ;

    XORWF   PORTC,W    ;Copy W bits 5-7 to PORTC bits 5-7.  This
    ANDLW   11100000B  ;code only works if the upper three bits
    XORWF   PORTC      ;of PORTC are guaranteed not to change
                       ;between the two XORs.
Another way, also 9 cycles/instructions:
    MOVF    PORTC,W    ;W = PORTC.
    ANDLW   00011111B  ;Clear W bits 5-7.

    BTFSC   SOURCE,2   ;Copy SOURCE bits 0-2 to W bits 5-7.
    ADDLW   10000000B  ;
    BTFSC   SOURCE,1   ;
    ADDLW   01000000B  ;
    BTFSC   SOURCE,0   ;
    ADDLW   00100000B  ;

    MOVWF   PORTC      ;PORTC = W.
ON 20050124@7:22:02 PM at page: http://techref.massmind.org/techref/microchip/math/bit/cuabrr.htm# James Newton[JMN-EFP-786] edited the page. Difference: http://techref.massmind.org/techref/diff.asp?url=H:\techref\microchip\math\bit\cuabrr.htm&version=1