# DECFSZ rolls over 16f628

Status
Not open for further replies.

#### deansmith

##### New Member
Hello everyboby, I'm having a problem with the instruction DECFSZ.
Here is my code:
' inc P16F628'
delay EQU 0x20
count EQU 0x21
...
...

main
movlw 0x0F
movwf PORTB ; rb3 to rb0 LEDs ON
movlw 0x3 ;set value 3 to count register
movwf count ;
bcf PORTB,3 ; rb3 LED off
loop
CALL delay ; 1 sec
RRF PORTB ; rb2 LED off....
decfsz count,f ;
goto loop ;
.
. .carry on here when LED rb3, rb2, rb1 are off AND rb0 is on
When the count has decremented to zero it doesnt skip to the next part of the program. It rolls over to 0xff and carries on looping. Im sure there is a simple answer.Please help. Thankyou

#### kpatz

##### New Member
Shouldn't it be decfsz count,1 to put the result back in the count register?

#### Pommie

##### Well-Known Member
You are calling delay and delay is equated to 0x20.

Code:
		inc	P16F628'
[COLOR="Red"]delay[/COLOR]		equ	0x20
count		equ	0x21
...
...

main
movlw	0x0F
movwf	PORTB		; rb3 to rb0 LEDs ON
movlw	0x3		;set value 3 to count register
movwf	count		;
bcf	PORTB,3		; rb3 LED off
loop
call	[COLOR="red"]delay[/COLOR]		; 1 sec
rrf	PORTB		; rb2 LED off....
decfsz	count,f		;
goto	loop		;
The decfsz count,f is correct.

Mike.

#### Hakachukai

##### New Member
Also keep in mind that you are using rrf.

That doesn't always work out the way that you might think it would, because it rotates using the Carry Flag.

So anything that affects the Carry Flag will also affect PORTB when you execute the rrf instruction. It will cause either a 0 or 1 to be added to the MSB of PORTB (depending on the status of the Carry Flag)

So... you can either deal with it (and possibly not care)... or you can clear the carry flag every time, right before you perform the rrf on PORTB

#### deansmith

##### New Member
I have rewritten my code and cleared the carry flag. It works now.
Thank you all for the support, Much appreciated.

Status
Not open for further replies.