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

BCD Subtract Code Help

Status
Not open for further replies.

Suraj143

Active Member
I'm reading time from DS1307 in 24Hours mode.I have a trim value ranging from 1---59 Seconds.
What I want is to subtract the Trim value from current time variables & save it to S_Hours,S_Minutes,S_Seconds registers.

Ex: Time is = 13.59.00
Trim Val = 5
Output = 13.58.55

I did a code but it seems that my code is too long.I need to compact my code little abit.

Code:
Current Time = Hours,Minutes,Seconds
Trim_Val = 0,1...59 (seconds)
must return the following
S_Hours,S_Minutes,S_Seconds = Current Time - Trim_Val

Save_Sec_uP        call        I2C_Read_Time    ;read DS1307
                movf        Minutes,W
                btfss        STATUS,Z
                goto        Save_Trim_H_M
                call        Dec_Hours
                call        B2_BCD
                movwf        S_Hours
                movlw        .59
                call        B2_BCD
                movwf        S_Minutes
                goto        Save_Trim_Secs
                ;           
Save_Trim_H_M    movf        Hours,W
                movwf        S_Hours
                movf        Minutes,W
                call        BCD_To_Binary
                movwf        S_Minutes
                decf        S_Minutes,W
                call        B2_BCD
                movwf        S_Minutes
                ;           
Save_Trim_Secs    movf        Trim_Val,W
                andlw        b'00111111'
                sublw        .60
                call        B2_BCD
                movwf        S_Seconds
                call        I2C_Write_Trim    ; save check time
                return
           
Dec_Hours        movf        Hours,W
                btfss        STATUS,Z
                goto        $+3
                movlw        .23                ;23.00
                return
                movf        Hours,W
                andlw        b'00111111'
                call        BCD_To_Binary
                movwf        S_Hours
                decf        S_Hours,W
                return
 

BobW

Active Member
What you have done looks reasonable. You can do subtraction directly in BCD, but it's messy and I doubt that it would be any shorter than what you have.
 

Suraj143

Active Member
Hi, Yes you are correct I need a subtraction direct on BCD instead of using B2_BCD and BCD_To_Binary subroutines :)
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top