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.

Two Digit Decrement Routine

Status
Not open for further replies.

Suraj143

Active Member
Hi I have this 2 Digit decrement code inside ISR. When both variables (Digit1 & Digit2) value = 0 it must turn ON the output. I want to confirm is it ok or not?

Thanks


Code:
	decf	Digit1,F	;decrement Digit1 value			
	movf	Digit1,W
	xorlw	00h		;check Digit1 = 0?
	btfss	STATUS,Z
	goto	Decr
	movf	Digit2,W	;yes then check Digit2 = 0?
	xorlw	00h
	btfss	STATUS,Z
	goto	Decr
	goto	Finish		;yes both are = 00 then goto output	
					
Decr	btfss	Digit1,7	;check Digit1 has gone negative?
	goto	Away
	movlw	.9
	movwf	Digit1
	decf	Digit2,F	;decrement Digit2 value	
	btfss	Digit2,7	;check Digit2 has gone negative?
	goto	Away
Output	clrf	Digit2
	clrf	Digit1						
	bsf	PORTA,3		;Yes both are zero turn ON RA3
 
Do Digit1 and Digit2 have the same range of values 0..9 ? If so, have you considered using a single variable with a range of 00..99?

Code:
;
        movf    Digit,W         ; Timer on?
        bz      Next            ; no, branch, else
        decf    Digit,F         ; decrement value
        skpnz                   ; zero?  no, skip, else
        bsf     PORTA,3         ; set RA3
Next
 
Last edited:
Hi Mike, K8LH I need to count down from 12 to 00.
When it reaches 00 the output must turn ON.my coding looks like too long.
 
Will easy if you use picbasic, here's the code when you decide to use picbasic


==============================
Dim digit as byte
digit = 12 'set initial digit to 12

Loop:
Digit = Digit - 1
if Digit = 0 then
Toggle portb.3 ' will on/off the portb
digit = 12 'set the digit back to 12
endif
goto Loop
==============================
 
There are probably many different ways to make it smaller and tighter;

Code:
;
        movf    Ones,W          ; W = Ones
        addlw   -1              ; subtract 1
        bc      Test            ; Borrow?  No, branch, else
        movlw   9               ; set Ones = 9
        decf    Tens,F          ; decrement Tens
Test    movwf   Ones            ; save new Ones value
        iorwf   Tens,W          ; Tens & Ones = '00'?
        skpnz                   ; no, skip, else
        bsf     PORTA,3         ; set RA3 pin
 
I always find it easier to keep tasks separate instead of combining them.

Is count zero?
Yes, turn on LED
No, Decrement count.

Code:
	movf	Digit1,W
	iorwf	Digit2,W	;test both digits
	btfss	STATUS,Z	;Zero?
	goto	Decr		;No so carry on and decrement
	bsf	PORTA,3		;yes, turn on LED
	goto	Away		;done

Decr	decf	Digit1,F	;decrement Digit1 value			
	btfss	Digit1,7	;check Digit1 has gone negative?
	goto	Away
	movlw	.9
	movwf	Digit1
	decf	Digit2,F	;decrement Digit2 value	
Away

Note, I removed the check for digit 2 being negative as this can never happen.

Mike.
 
Hi guys thanks for the expert codes.

I really missed that OR function. That’s the secret.
I still wonder why I missed that OR function? That is due to lack of my programming skills.

Anyway thanks for you compact superb codings. I’m going to use them.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top