SetBit
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, Long, Long, 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 = 16F1829
Declare Xtal = 16
' Tell the compiler the device is operating at 16MHz
Dim ExVar as Byte
Dim Index as Byte
Cls
ExVar = 0b11111111
Do
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
Loop
' 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 using the FSR, and INDF registers. 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.