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

Decrement Routine Code

Discussion in 'Microcontrollers' started by Suraj143, Nov 15, 2017.

  1. Suraj143

    Suraj143 Active Member

    Joined:
    Jan 11, 2007
    Messages:
    1,586
    Likes:
    2
    Location:
    South Mald Isld
    Need to decrement minutes & seconds down to zero. Is my code ok?

    Code (text):

    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
     
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,152
    Likes:
    339
    Location:
    Brisbane Australia
    ONLINE
    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.
     
  3. Suraj143

    Suraj143 Active Member

    Joined:
    Jan 11, 2007
    Messages:
    1,586
    Likes:
    2
    Location:
    South Mald Isld
    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
     
  4. dave

    Dave New Member

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


     
  5. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,152
    Likes:
    339
    Location:
    Brisbane Australia
    ONLINE

    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 (text):

              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: Nov 17, 2017
  6. Suraj143

    Suraj143 Active Member

    Joined:
    Jan 11, 2007
    Messages:
    1,586
    Likes:
    2
    Location:
    South Mald Isld
    Hi

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

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,152
    Likes:
    339
    Location:
    Brisbane Australia
    ONLINE
    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 (text):

    #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: Nov 17, 2017
  8. Suraj143

    Suraj143 Active Member

    Joined:
    Jan 11, 2007
    Messages:
    1,586
    Likes:
    2
    Location:
    South Mald Isld
    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 :)
     
  9. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    6,051
    Likes:
    519
    Location:
    Cleveland, OH, USA
    ONLINE
    I think if you rearrange a little it will do what you want:
    Code (MPASM):

    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
     
  10. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,152
    Likes:
    339
    Location:
    Brisbane Australia
    ONLINE
    I understand now. John's is an elegant solution.

    Mike.
     
  11. Suraj143

    Suraj143 Active Member

    Joined:
    Jan 11, 2007
    Messages:
    1,586
    Likes:
    2
    Location:
    South Mald Isld
    Hi,

    That was a very nice code change.

    Thanks to both of you.
     

Share This Page