SECTION III-5: The COMF Instruction in the PIC micro-controller

The "COMF fileReg, d" instruction complements (inverts) the contents of fileReg and places the result in WREG of fileReg. The is an example of what is called Read - Modify - Write.

In the following program, we put 55H into WREG and then send it out to SFR location of port B. Then the content of Port B is complemented, which becomes AA in hex. The 01010101 (55H) is inverted and becomes 10101010 (AAH).

MOVWF PORTB ;move WREG to Port B SFR (PB = 55H
COMF PORTB, F ;complement Port B (PB = AAH)


Simple Program to Toggle the SFR of PORT B continuously forever.
        MOVLW 55H ;WREG = 55H
        MOVWF PORTB ;move WREG to Port B SRF (PB = 55H)
B1    COMF PORTB, F ;complement Port B and place it in Port B
         GOTO B1 ;repeat forever


More From