SetWith

Syntax:

    SetWith(TargetBit, Source)

Command Availability:

Available on all microcontrollers.

Explanation:

SetWith is an extended version of Set that sets or clears a single bit by evaluating the result of Source, rather than requiring a literal On/Off. Use SetWith whenever TargetBit is an I/O pin and Source is the result of a function or expression, rather than writing the pin in two steps (evaluate, then If…​Then Set) — doing it in one call avoids a brief window where the pin could glitch to its old state before your code catches up, an effect commonly called I/O jitter.

Source can be a variable of type Bit, Byte, Word, or Long, a constant, an expression, or a function’s result. SetWith sets TargetBit to 1 if Source evaluates to anything other than zero, and to 0 otherwise — TargetBit is always driven to exactly 1 or 0, regardless of `Source’s variable type or magnitude.

Example:

    ' This program will reflect the state of SW1(RA3) on LED DS1(RC0) of the Microchip
    ' Low Pin Count Demo Board. Notice that because SW1 is normally High the state has to
    ' be inverted to turn on the LED (DS1) when SW1 is pressed.

    #chip   16f690    ' declare the target Device

    #Define SW1 PORTA.3
    #Define DS1 PORTC.0

    DIR DS1 Out
    DIR SW1 In

    Do
      ' set the Bit DS1 to equal the Bit SW1
      SetWith( DS1, !SW1 )          ' <<< the SetWith instruction
    Loop
    END

Key line: SetWith( DS1, !SW1 ) — drives DS1 to the logical inverse of SW1 in a single, jitter-free step, rather than reading SW1 and writing DS1 as two separate statements.

See Also:

  • Bitwise Operations Overview — background on GCBASIC’s bitwise operations
  • Set — the simpler On/Off form that SetWith extends
  • Dir — setting the pin directions used in the example above