Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science

Bit Orientated Instructions

Another very interesting set of instructions is the ones that have to do with the bits of a byte. The 16F88 is an 8-bit micro controller. This means that each byte will have 8 bits. Just to remind you that the most left bit is called MSB (Most Significant Bit) while the most right is called LSB (Less Significant Bit). Also, the numbering of the bits starts from zero base and counts form the LSB to the MSB. Thus, the LSB bit is the bit nmber 0 and the MSB is the bit number 7.

 Instruction Description BSF f , b The Bit 'b' in file register 'f' is Set (1). BCF f , b The Bit 'b' in file register 'f' is Cleared (0). BTFSS f , b If the bit 'b' in file register 'f' is 1, then the following instructions is not executed and a 'NOP' is executed instead. If the bit is 0, the instructions is executed normally. BTFSC f , b If the bit 'b' in file register 'f' is 0, then the following instructions is not executed and a 'NOP' is executed instead. If the bit is 1, the instructions is executed normally. SWAPF f , d The upper and lower nibbles of file register 'f' are exchanged. If d is 0 the result is stored in the W register, if d is 1 the result is stored back to the file register 'f'.

Now, let's see the instructions one by one:

BSF f , b

With this instructions you can set a bit within a register. The Status is not affected with this instruction. The 'b' can be a number from 0 to 7 corresponding to a bit position.

Example
```;This example requires that you have declare a file register for example
;TempRegister          equ 0x20
bsf TempRegister,3     ;The bit in place 3 in register 'TempRegister' is SET (1)
bsf PORTA,0            ;The bit in place 0 in register 'PORTA' is SET (1). If this
;port pin is output, the corresponding pin will become HIGH
```

BCF f , b

With this instructions you can clear a bit within a register. The Status is not affected with this instruction. The 'b' can be a number from 0 to 7 corresponding to a bit position.

Example
```;This example requires that you have declare a file register for example
;TempRegister          equ 0x20
bcf TempRegister,3     ;The bit in place 3 in register 'TempRegister' is CLEARED (0)
bcf PORTA,0            ;The bit in place 0 in register 'PORTA' is CLEARED (0). If this
;port pin is output, the corresponding pin will become LOW
```

BTFSS f , b

This instruction can be used for conditional execution of instructions. The two last lettes 'SS' stand for Skip if Set. The very next instruction after the BTFSS will be executed only if the bit 'b' withing register 'f' is NOT SET. Otherwise it will be replaced with a 'NOP' instruction and shall not be executed. The Status is not affected with this instruction. The 'b' can be a number from 0 to 7 corresponding to a bit position.

Example
```        btfss PORTA,4          ;Check bit '4' in register PORTA
goto Bit_Was_Zero      ;If '0' goto Bit_Was_Zero
.
.
```

BTFSC f , b

This instruction can be used for conditional execution of instructions. The two last lettes 'SC' stand for Skip if Clear. The very next instruction after the BTFSC will be executed only if the bit 'b' withing register 'f' is SET. Otherwise it will be replaced with a 'NOP' instruction and shall not be executed. The Status is not affected with this instruction. The 'b' can be a number from 0 to 7 corresponding to a bit position.

Example
```        btfsc PORTA,4          ;Check bit '4' in register PORTA
goto Bit_Was_Zero      ;If '1' goto Bit_Was_Zero
.
.
```

SWAPF f , d

This instruction is used to swap the nibbles of a register. When executed, the bits 0-3 will be swapped with the bits 4-7. If d is 0 the result is stored in the W register, if d is 1 the result is stored back to the file register 'f'. The Status is not affected with this instruction.

Example
```;This example requires that you have declare a file register for example
;TempRegister          equ 0x20
movlw b'11110000'      ;W has the binary number '11110000'
movwf TempRegister     ;TempRegister has the binary number '11110000'
swapf TempRegister,1   ;The nibbles in TempRegister are swaped
;Now TempRegister has the value '00001111'
```

There is an online test to check your knowledge on this page. You may reveal the test with the following button:

Previous page ---- Next page

Go back to the book contents

Go to the discussion forum of this book

 At 24 August 2013, 23:25:48 user ravi wrote:   [reply @ ravi]Thank you for explaining all the nuances in the pic assembler programming. A well presented explanation of the workings of pic devices.

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise without the prior written permission of the author.

All trademarks used are properties of their respective owners.

 HOT in heaven!

 Disclaimer Book Contents Discussion forum

 NEW in heaven! New Theory: AC electric motor working principle