1
\$\begingroup\$

Still learning on pic programming.

The code has no meaning only for looking at the behaviour of BTFSC When using BTFSC in a loop construction the testing bit is changed the next instruction is not skipped when the tested bit is 0.What is wrong in my approach?

 AGAIN
    MOVLW  B'01010100'
    MOVWF   BYTE
    MOVLW H'07'
    MOVWF LOOPCOUNT
LOOP

    BTFSC BYTE,LOOPCOUNT
    BCF PORTA,OUTPUT
    BSF PORTA,OUTPUT
    DECFSZ LOOPCOUNT    
    GOTO LOOP

    GOTO AGAIN
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

BTFSC tests a particular bit of a particular file register. It's described in the literature as

BTFSC f, b

The first argument, 'f', is the number of a file register. In the code you posted, 'f' is what you called 'BYTE', presumably declared as one of those registers. The second argument, 'b', you have specified as 'LOOPCOUNT', which is also a register into which you stored the number 7.

But the instruction doesn't work that way. 'b' must be a number, or a symbol or label that is equivalent to a bit number. They way you used it, it is using the value associated with the register number. For example, if LOOPCOUNT is register #26, then it thinks you said,

BTFSC BYTE, 26

Note that when you see an instruction like

BTFSC STATUS, Z

It looks like magic, but 'STATUS' really is a register with its own special number, and 'Z' is a symbol whose value just happens to describe which bit of 'STATUS' is the 'Z' bit. 'Z' is defined somewhere in the assembler or in an include file.

\$\endgroup\$
1
  • \$\begingroup\$ I intended to modify the second argument to change the bit selection. If that does not work then I have to go the alternative route and use RLF on the register to find the next bit \$\endgroup\$
    – Decapod
    Commented Feb 20, 2017 at 10:04
0
\$\begingroup\$

I believe part of your problem is cause by the fact that BCF and BSF are read-modify-write instructions. If you read Microchip document DS33023 section - 9.10 I/O Programming Considerations you will see what is going wrong.

\$\endgroup\$
1
  • \$\begingroup\$ The question does not concern BCF or BSF. But only the BTFSC instruction. So only the missing skip. \$\endgroup\$
    – Decapod
    Commented Feb 20, 2017 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.