Positron Compiler Documentation

SetBit

Source: Positron16 Compiler User Manual, PDF page 357

Syntax

SetBit Variable, Index

Overview

Set a bit of a variable or register using a variable index to the bit of interest.

Parameters

Variable is a user defined variable, of type Byte, Word, or Dword. Index is a constant, variable, or expression that points to the bit within Variable that requires setting.

Example

' Clear then Set each bit of variable ExVar
  Device = 24HJ128GP502
                                     ' Select the device to compile for
  Declare Xtal = 16
  Dim ExVar as Byte
  Dim Index as Byte
  Cls
  ExVar = %11111111
  While
                                     ' Create an infinite loop
     For Index = 0 to 7
                                     ' Create a loop for 8 bits
       ClearBit ExVar,Index
                                     ' Clear each bit of ExVar
       Print At 1,1,Bin8 ExVar
                                     ' Display the binary result
       DelayMs 100
                                     ' Slow things down to see what's happening
     Next
                                     ' Close the loop
     For Index = 7 to 0 Step -1
                                     ' Create a loop for 8 bits
       SetBit ExVar,Index
                                     ' Set each bit of ExVar
       Print At 1,1,Bin8 ExVar
                                     ' Display the binary result
       DelayMs 100
                                     ' Slow things down to see what's happening
     Next
                                     ' Close the loop
  Wend
                                     ' Do it forever

Notes.

There are many ways to set a bit within a variable, however, each method requires a certain amount of manipulation, either with rotates, or alternatively, the use of indirect addressing. Each method has its merits, but requires a certain amount of knowledge to accomplish the task correctly. The SetBit command makes this task extremely simple using a register rotate method, however, this is not necessarily the quickest method, or the smallest, but it is the easiest. For speed and size optimisation, there is no shortcut to experience.

To set a known constant bit of a variable or register, then access the bit directly using PORT.n.

PORTA.1 = 1

or

Var1.4 = 1

If a Port is targeted by SetBit, the TRIS register is not affected.

See also : ClearBit, GetBit, LoadBit.