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, Long, 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 = 16F1829
Declare Xtal = 4
' Tell the compiler the device is operating at 4MHz
Dim ExVar as Word
Dim Index as Byte
Dim Value as Byte
Dim PT_Var as Word
Do
PT_Var = 0b0000000000000000
ExVar = 0b1011011000110111
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 using the FSR, and INDF registers. 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 using FSR, and INDF, 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.