Bit operations allow us to manipulate a single bit within a word. They allow us to move, set and clear single bits in registers or numbers that we specify. At the end of this tutorial I will show you a program that will produce a set of running lights that go one way, then the other way. We saw this done previously when we looked at the exclusive OR function, where we Exclusively ORed the ports with a word
We have already seen a couple of bit operations when we set up the ports on the PIC, and I will repeat their use here.
This instruction will clear a bit that we specify in a register that we specify. The syntax is:
We used this previously to change from page 1 to page 0 by clearing a bit in the STATUS register. We can also use it to set a bit to 0 in any other register/location. For example, if we wanted to set the third bit in 11001101 stored in l o c a t i o n. 0C to 0, we would enter:
This instruction will set any bit we specify to 1 in any register that we specify. We used this previously to go from Page 0 to Page 1. The syntax is:
BSF <register>,<bit>, and is used in exactly the same way as BCF above.
So far we have set or cleared a bit in a register. But what if we want to just simply test if a bit is a 1 or a 0 in a register? Well, we can use BTFSC. It says Bit Test Register F, and Skip If It Is Clear. This instruction will test the bit we specify in the register. If the bit is a 0, the instruction will tell the PIC to skip the next instruction. We would use this instruction if we wanted to test a flag, such as the carry flag. This saves us having to read the STATUS register and looking at the individual bits to see which flags are set. For example, if we wanted to test if the Carry flag had been set to 1 after we have added two numbers, then we would enter the following:
carry on here if set to 1
or here if set to 0
If the status of the bit is a 1, then the instruction immediately following BTFSC will be carried out. If it is set to a 0, then the next instruction is skipped. The following section of code shows where it might be used:
In the above code, the PIC will only come out of the loop if bit 0 of the STATUS register (or the Carry flag) is set to 0. Otherwise, the goto command will be carried out
This instruction says Bit Test Register F, And Skip If Set. This is similar to the BTFSC instruction, except that the PIC will skip the next instruction if the bit we are testing is set to 1, rather than 0.
This instruction will set the entire contents of a register to 0. The syntax is:
We used this previously to set the output of the Ports to 0, by using CLRF 85h. We also used it to set the Ports to have all pins to output by using CLRF 05h.
This is similar to the CLRF instruction, except is only clears the W register. The syntax is quite simply:
RLF And RRF
These commands will move a bit in a register one place to the left (RLF) or the right (RRF) in a register. For example, if we had 00000001 and we used RLF, then we would have 00000010. Now, what happens if we have 10000000 and carried out the RLF instruction? Well, the 1 will be placed in the carry flag. If we carried out the RLF instruction again, the 1 will reappear back at the beginning. The same happens, but in reverse, for the RRF instruction. The example below demonstrates this for the RLF instruction, where I have shown the 8 bits of a register, and the carry flag :
RLF 0 00000010
RLF 0 00000100
RLF 0 00001000
RLF 0 00010000
RLF 0 00100000
RLF 0 01000000
RLF 0 10000000
RLF 1 00000000
RLF 0 00000001