1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

btfsc AND btfss

Discussion in 'Microcontrollers' started by emaney, Jun 11, 2007.

  1. emaney

    emaney New Member

    Joined:
    Jun 3, 2007
    Messages:
    9
    Likes:
    0
    can any one tell me differenc between the two, iam realy confused about it for example following code snippet

    lets say is n1=5 and n2=3, wat would be the result if its btfsc/btfss
    NUM1: EQU 0x20
    NUM2: EQU 0x21
    Test: CLRF PortB
    MOVF NUM1, w
    SUBWF NUM2,w
    BTFSS Status, c
    GOTO CASE1
    BTFSS Status, z
    GOTO CASE2
    CASE0: BSF PortB, 0
    GOTO EXTPOINT
    CASE1: BSF PortB, 1
    GOTO EXTPOINT
    CASE2: BSF PortB, 2
    GOTO EXTPOINT
     
  2. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,235
    Likes:
    641
    Location:
    Derbyshire, UK
    One is branch if the bit is SET, the other is branch if it's CLEAR - they do the opposite of each other. Along with BSF and BCF they are really the instructions which make the PIC exceptionally useful as a micro-controller - micro-procesors tend not to have bitwise instructions.
     
  3. emaney

    emaney New Member

    Joined:
    Jun 3, 2007
    Messages:
    9
    Likes:
    0
    please can u explain using the example i stated......especially i dont undersatnd what status in being checked for z.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. donniedj

    donniedj Banned

    Joined:
    Jan 16, 2004
    Messages:
    336
    Likes:
    3
    Location:
    So Cal

    This is just checking if the difference between two values is 0, negative, or positive. Take a look at the datasheet for the PIC16F628 at microchip.com. Look at the instruction SUBWF. It gives an example of how Carry and Zero flags indicate 0, negative, or positive.

    Code (text):

    Test:
        ;-------------------------
        ; DO AN OPERATION
        ;-------------------------
        CLRF PortB      ;PORTB = 0
        MOVF NUM1, w        ;W = NUM2 - NUM1
        SUBWF NUM2,w


        ;-------------------------
        ; DETERMINE RESULT OF OPERATION
        ;-------------------------
        BTFSS Status, c     ;IF CARRY IS SET,
        GOTO CASE1      ;ELSE- GOTO CASE1

        BTFSS Status, z     ;THEN IF ZERO IS SET
        GOTO CASE2      ;ELSE- GOTO CASE2



        ;------------------------------------------
        ; SELECT CASE BASED ON RESULT OF OPERATION
        ;   -CASE0 BECAUSE: NUM2 - NUM1 IS 0 (EQUALITY)
        ;   -CASE1 BECAUSE: NUM2 - NUM1 IS NEGATIVE
        ;   -CASE2 BECAUSE: NUM2 - NUM1 IS POSITIVE
        ;------------------------------------------


    CASE0:  ;--I AM HERE AND ONLY HERE BECAUSE C=1, Z=1
        BSF PortB, 0       
        GOTO EXTPOINT       ;GOTO A COMMON PLACE


    CASE1:  ;--I AM HERE AND ONLY HERE BECAUSE C=0, Z="I DO NOT CARE"
        BSF PortB, 1
        GOTO EXTPOINT       ;GOTO A COMMON PLACE


    CASE2:  ;--I AM HERE AND ONLY HERE BECAUSE C=1, Z=0
        BSF PortB, 2
        GOTO EXTPOINT       ;GOTO A COMMON PLACE

     
     
  6. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Z is the ZERO bit of the STATUS register. If the returned value is zero, this bit will be set.
    btfss STATUS, Z ;if the returned value is zero, Z bit is set so the next instruction is skipped. if the returned value is not zero, just continue executing next instruction.
    It is the other way round for btfsc STATUS, Z.
     
  7. emaney

    emaney New Member

    Joined:
    Jun 3, 2007
    Messages:
    9
    Likes:
    0
    thank you so much all for fantstic help........thanks
     
  8. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    So.. do you understand them finally? :)
     
  9. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,024
    Likes:
    317
    Location:
    Brisbane Australia
    The thing to remember when it comes to subtraction is that the carry flag is actually a borrow flag. It is set before the subtraction takes place and if a borrow is required it gets cleared.

    So,
    12-11 = 1 no borrow required. Carry = 1
    11-12 = -1 borrow needed. Carry = 0

    Another way to think about it is that the carry/borrow bit is the 9th bit of the subtraction. The 11-12 then becomes (256+11)-12=255 and bit 9, the borrow bit = 0.

    If your not confused now then your doing well.:D

    Mike.
     
  10. emaney

    emaney New Member

    Joined:
    Jun 3, 2007
    Messages:
    9
    Likes:
    0
    iam still a beginner in this language but all this has cleared alot of doubt about btfss/btfsc..............
     
  11. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,235
    Likes:
    641
    Location:
    Derbyshire, UK
    Well your question seemed to end up more about status flags than the bit test instructions? :D
     
  12. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    I guess it's all in the way you look at it. After looking at his post I thought immediately about "greater than", "less than", and "equal"...
     
  13. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,024
    Likes:
    317
    Location:
    Brisbane Australia
    Mike,
    Your posts are becoming more cryptic and less useful.

    Mike.
     
  14. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA
    I was just commenting on Nigel's comment (grin).
     

Share This Page