LoadBit
Syntax
LoadBit Variable, Index, Value
Overview
Clear, or Set a bit of a variable or register using a variable index to point 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 accessing. Value is a constant, variable, or expression that will be placed into the bit of interest. Values greater than 1 will set the bit.
Example
' Copy variable ExVar bit by bit into variable PT_Var
Device = 24HJ128GP502
' Select the device to compile for
Declare Xtal = 16
Dim ExVar as Word
Dim Index as Byte
Dim Value as Byte
Dim PT_Var as Word
Do
PT_Var = %0000000000000000
ExVar = %1011011000110111
Cls
For Index = 0 to 15
' Create a loop for 16 bits
Value = GetBit ExVar, Index ' Examine each bit of variable ExVar
LoadBit PT_Var, Index, Value ' Set or Clear each bit of PT_Var
Print At 1,1,Bin16 ExVar
' Display the original variable
Print At 2,1,Bin16 PT_Var
' Display the copied variable
DelayMs 100
' Slow things down to see what's happening
Next
' Close the loop
Loop
' Do it forever
Notes.
There are many ways to clear or 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 LoadBit command makes this task extremely simple by taking advantage of the indirect 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 Clear a known constant bit of a variable or register, then access the bit directly using Port.n. i.e. PORTA.1 = 0
To Set a known constant bit of a variable or register, then access the bit directly using Port.n. i.e. PORTA.1 = 1
If a Port is targeted by LoadBit, the TRIS register is not affected.