ClearBit
Syntax
ClearBit Variable, Index
Overview
Clear a bit of a variable or register using a variable index to the bit of interest.
Parameters
Variable is a user defined variable. Index is a constant, variable, or expression that points to the bit within Variable that requires clearing.
Example
' Clear then Set each bit of variable ExVar
Device = 16F1829
Declare Xtal = 4
Dim ExVar as Byte
Dim Index as Byte
Cls
ExVar = 0b11111111
Do
' 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
Loop
' Do it forever
Notes
There are many ways to clear 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 ClearBit 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 Clear a known constant bit of a variable or register, then access the bit directly using Port.n.
PORTA.1 = 0
or
Var1.4 = 0
If a Port is targeted by ClearBit, the TRIS register is not affected.