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

Decrement Routine Code

Status
Not open for further replies.

Suraj143

Active Member
Need to decrement minutes & seconds down to zero. Is my code ok?

Code:
Do_Count_Down     movf   Seconds,W
                  btfss  STATUS,Z
                  goto   Dec_Secs
                  movf   Minutes,W
                  btfsc  STATUS,Z
                  goto   Both_Zero
                  decf   Minutes,F
                  call   Split_Minutes
                  movlw  .59
                  movwf  Seconds
Show_Secs         call   Split_Seconds
                  goto   Wait_Sec_Flag
Dec_Secs          decfsz Seconds,F
                  goto   Show_Secs
                  movf   Minutes,W
                  btfss  STATUS,Z
                  goto   Show_Secs
Both_Zero         call   Split_Seconds
                  call   Split_Minutes
 

Pommie

Well-Known Member
Most Helpful Member
In the Dec_Secs path, you decrement the seconds but never decrement minutes. You also decrement minutes if both seconds and minutes are not zero. Neither of these looks correct.

What are you trying to achieve?

Mike.
 

Suraj143

Active Member
This is a count down timer (traditional Timer). Let say Timer value is 02:56 (2 minutes & 56 Seconds). Seconds decrement first.When seconds go beyond value zero then decrement 1 minute. Likewise it will decrement down to 00:00.

Maximum minutes = 99
Maximum seconds = 59
 

Pommie

Well-Known Member
Most Helpful Member
I see what you're doing now. Looks like it should work as expected.
However, it's a little difficult to follow jumping back and forward.
Have you considered something like,
Code:
          movf     Seconds,w            ;get seconds
          iorwf    Minutes,w            ;ior minutes
          btfsc    STATUS,Z             ;if it's zero
          goto     AllDone              ;time is up
          decf     Seconds,f            ;decrement seconds
          btfss    Seconds,7            ;has it gone negative
          goto     Wait_Sec_Flag        ;no so continue
          movlw    .59                  ;yes
          movwf    Seconds              ;so set to 59
          decf     Minutes,f            ;and decrement mins
          goto     Wait_Sec_Flag        ;continue
AllDone               
          ;time has ellapsed
 
Last edited:

Suraj143

Active Member
Hi

That's short but there is a small problem. The timeout (All Done) will achieve in next second, not in the last second :(
 

Pommie

Well-Known Member
Most Helpful Member
It will only go to AllDone when both minutes and seconds are zero.

Why do you think it will be in next second?

Mike.
Edit,
Try running this in the simulator, it should dec seconds 62 times and it does.
Code:
#include "p16F84a.inc"
        cblock    0x20
Seconds
Minutes
        endc
        org    0
        movlw    1
        movwf     Minutes
        movlw    2
        movwf    Seconds
Wait_Sec_Flag
        movf    Seconds,w            ;get seconds
        iorwf    Minutes,w            ;ior minutes
        btfsc    STATUS,Z            ;if it's zero
        goto    AllDone                ;time is up
        decf    Seconds,f            ;decrement seconds
        btfss    Seconds,7            ;has it gone negative
        goto    Wait_Sec_Flag        ;no so continue
        movlw    .59                    ;yes
        movwf    Seconds                ;so set to 59
        decf    Minutes,f            ;and decrement mins
        goto    Wait_Sec_Flag        ;continue
AllDone                
        ;time has ellapsed
        end
 
Last edited:

Suraj143

Active Member
Hi thanks for the view. This is what I mean.Every second I'm getting the 1 second flag bit.
Just load Minutes = 0, Seconds = 1.

The time out will not achieve in 1 second, It will achieve in 2 seconds. Both zero must also check after decrement seconds as well. That's what I mean :)
 

jpanhalt

Well-Known Member
Most Helpful Member
I think if you rearrange a little it will do what you want:
Code:
Wait_Sec_Flag

        movf   Minutes,w 
        decf    Seconds,f            ;decrement seconds
        iorwf  Seconds,w
        btfsc  STATUS,2   

;        movf    Seconds,w            ;get seconds
;        iorwf    Minutes,w            ;ior minutes
;        btfsc    STATUS,Z            ;if it's zero
        goto    AllDone                ;time is up
;        decf    Seconds,f            ;decrement seconds
        btfss    Seconds,7            ;has it gone negative
        goto    Wait_Sec_Flag        ;no so continue
        movlw    .59                    ;yes
        movwf    Seconds                ;so set to 59
        decf    Minutes,f            ;and decrement mins
        goto    Wait_Sec_Flag        ;continue
John
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top