Continue to Site

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.

  • 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.

btfsc AND btfss

Status
Not open for further replies.

emaney

New Member
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
 
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.
 
please can u explain using the example i stated......especially i dont undersatnd what status in being checked for z.
 
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:
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
 
emaney said:
please can u explain using the example i stated......especially i dont undersatnd what status in being checked for z.
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.
 
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.
 
iam still a beginner in this language but all this has cleared alot of doubt about btfss/btfsc..............
 
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"...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top