William Roth - 2016-04-01

This note only applies to PIC chips with LATx registers.

What I mean by "toggle" is... if the Pin is ON , then Turn it off
and if the Pin is OFF, the Turn it on.

GCB has no Toggle command to quickly toggle the state of an output pin. There is a high level bitwise function called "FnNotBIT" that could be used, but is it rather slow.

DIR PORTC.0 OUT
Do
    Setwith (LATC0, FnNotBit (PORTC.0)  
Loop

This will toggle PORTC.0 at about 210Khz with a 32Mhs System clock

A straight forward and faster method can be done as follows:

DIR PORTC.0 OUT
Do

     If LATC.0 = 1 then  'Test Port
        SET PORTC.0 OFF
     Else 
        Set PORTC.0 ON
     END IF
Loop

This will toggle the Pin ON/OFF at about 620Khz with a system clock of 32Mhz

However to nearly double the speed it can be done as follows using in line ASM with "XORWF"

DIR PORTC.0 OUT
Do
    MOVLW b'00000001' 
    XORWF LATC, F
Loop

This will toggle PORTC.0 pin on/off at 1 MHz with a 32Mhz System Clock and is probably the fastest possible method on 8- bit PIC Chips with LAT registers. If you know of a faster method I would like to see it.

The Value after MOVLW is a "bit mask" so with bit0 set to "1"
we are telling it to XOR BIT 0 which is PORTC.0.

This method can also be used to quickly toggle multiple pins on the same Port.

TRISC = 0    'All PortC Pins are outputs
Do
       MOVLW b'00101001'     Toggle PORTC.0,C.3 and C.5 
       XORWF LATC, F
Loop

Enjoy

Bill