[SOLVED !] bcd to decimal


Closed Thread
Results 1 to 4 of 4

Thread: bcd to decimal4813 days old

  1. #1
    ADLIN SYSTEMS
    Guest ADLIN SYSTEMS's Avatar

    0 Not allowed!

    Smile bcd to decimal

    I am using a rtc pcf8583 which outputs its data in bcd. This is nice when you want to print the data. After rotating the correct bits then all you do is to print in hex2 format. Now i need that byte in decimal format. Is there an easy way of doing it. I am rotating the upper nibble into the lower nibble multiplying it by 10 and adding the lowwer nibble. Can anyone show me a nice short piece of code to do it shorter.

    Thanks

    Anton

  2. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  3. #2
    Fanatical Contributor Tim's Avatar
    Join Date
    Jan 2003
    Posts
    7,754
    Thumbs Up
    Received: 51
    Given: 96
    Total Downloaded
    1.33 GB

    0 Not allowed!

    Default

    There is I think a BCD to bin conversion in the library commands.

    Here is another one, I think its like the one you described

    Code:
    '-----------------------------------------------
    ' BCD CONVERSION ROUTINES
    ' 
    '-----------------------------------------------      
           
    BCD_TO_BIN1:
        TEMP1 = BCD2BIN_VAL & $F                    ' CONVERT THE VALUES FROM BCD TO BIN
        TEMP2 = BCD2BIN_VAL & $F0                   ' MASK OF EITHER SIDE
        TEMP2 = TEMP2 >>4                           ' DIVIDE BY 16
        TEMP2 = TEMP2 * 10                          ' X BY 10
        BCD2BIN_VAL = TEMP1 + TEMP2                 ' NOW ADD THE FIRST NUMBER YOU THOUGHT OF
        Return

    But I though I would save the best to last. It was written By Xor and here is the Proton implementation as a macro. The whole code will run in Isis to show it in action.

    Code:
        Include "Proton18_4.inc"
    
        GoTo OverSubs
    
        BCDToBin MACRO  P1
        #IF (PRM_COUNT > 1)
        	#ERROR "BCDToBin - Too many parameters"
        #ELSE
        	#IF (PRM_COUNT < 1)
        		#ERROR "BCDToBin - Too few parameters"
        	#ELSE
        		#IF(PRM_1 != Byte) && (PRM_1 != NUM8) && (PRM_1 != NUM16) && (PRM_1 != NUM32)
        			#ERROR "BCDToBin - BCDVal(Param1) Should be a Byte variable or number"
        		#ENDIF
        		#IF (PRM_1 == Byte)
        			BYTE_BYTE P1, pBCD
        		#ENDIF
        		#IF (PRM_1 == NUM8)
        			NUM_BYTE P1, pBCD
        		#ENDIF
        		#IF (PRM_1 == NUM16)
        			NUM_BYTE P1, pBCD
        		#ENDIF
        		#IF (PRM_1 == NUM32)
        			NUM_BYTE P1, pBCD
        		#ENDIF
        		GoSub BCDToBinSub
        	#ENDIF
        #ENDIF
        #IF (BCDToBin_RETURN != 1)
        	#ERROR "BCDToBin - Mandatory return parameter misssing"
        #ELSE
        	#IF(RETURN_TYPE != Byte) && (RETURN_TYPE != Word) && (RETURN_TYPE != DWord) && (RETURN_TYPE != Float)
        		#ERROR "BCDToBin - Return variable should be a Byte variable or number"
        	#ELSE
        		#IF (RETURN_TYPE == Byte)
        			BYTE_BYTE WREG, RETURN_VAR
        		#ENDIF
        	#ENDIF
        #ENDIF
        ENDM
    
    
    
    BCDToBinSub:
    Dim pBCD As Byte
       ASM
          rrcf       pBCD, W      ; BCD/2 in WREG
          rrcf       WREG, W      ; BCD/2/2 in WREG
          andlw      0x3C         ; mask upper nibble as 4 * tens
          subwf      pBCD, F      ; BCD = (16 * tens) - (4 * tens) = 12 * tens + ones
          rrncf      WREG, W      ; BCD/2/2/2 in WREG = (2 * tens)
          subwf      pBCD, W      ; BCD = (12 * tens) - (2 * tens) = 10 * tens + ones
       ENDASM
       Return
       
    OverSubs:    
    
    
        Dim BCD As Byte
        Dim result As Byte
        
        BCD = $12
        
        result = BCDToBin [BCD]
        Cls
        Print At 1,1,"Result = ", Dec result
        
        Stop
    Its so good I think I will make it a macro library command
    Tim

  4. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  5. #3
    Mark Rodgers
    Guest Mark Rodgers's Avatar

    0 Not allowed!

    Default

    Here is how mine is done using nothing but BASIC, not as efficient as other methods but very easy for me to understand!

    Code:
        'BCD to BINARY conversion
        'The byte to be converted is loaded into the variable bcdval
        'and is returned in the variable decval
        '##################################
    bcdbin:
        If bcdval>=0 And bcdval <=9 Then decval=bcdval
        If bcdval>=16 And bcdval <=25 Then decval=bcdval-6
        If bcdval>=32 And bcdval <=41 Then decval=bcdval-12
        If bcdval>=48 And bcdval <=57 Then decval=bcdval-18
        If bcdval>=64 And bcdval <=73 Then decval=bcdval-24
        If bcdval>=80 And bcdval <=89 Then decval=bcdval-30
        If bcdval>=96 And bcdval <=105 Then decval=bcdval-36
        If bcdval>=112 And bcdval <=121 Then decval=bcdval-42
        If bcdval>=128 And bcdval <=137 Then decval=bcdval-48
        If bcdval>=144 And bcdval <=153 Then decval=bcdval-54
        
        Return
        '##################################
        
        
        
        
        'BINARY to BCD conversion
        'The byte to be converted is loaded into the variable decval
        'and is returned in the variable bcdval
        '##################################
    binbcd: 	  	 
        a=decval//10                                    'get lsb,same for BIN and BCD(modulous of a)
        b=decval/10                                     'get msb
        c=b*16                                          'covert msb to BCD
        bcdval=c+a                                      'add BCD msb and lsb together
    
        Return
        '##################################

  6. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  7. #4
    ADLIN SYSTEMS
    Guest ADLIN SYSTEMS's Avatar

    0 Not allowed!

    Default bcd to decimal

    Thanks guys now to dechiper it just joking will have a look and see what suits my need.

  8. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Unpacked BCD Maths
    By AlbertoFS in forum Projects discussion
    Replies: 9
    Last Post: 20th September 2018, 12:08
  2. BCD Calculations please
    By fanie in forum The Lounge
    Replies: 12
    Last Post: 14th March 2010, 13:56
  3. [SOLVED !] BCD to BIN and BIN to BCD, a new look ?
    By Kapitan in forum Proton Plus Compiler v3
    Replies: 16
    Last Post: 9th August 2009, 00:27
  4. BCD and BIN
    By eleckits in forum Proton Plus Compiler v3
    Replies: 3
    Last Post: 17th September 2007, 10:33
  5. Word value to BCD
    By Peter D in forum Proton Plus Compiler v3
    Replies: 10
    Last Post: 4th May 2007, 05:23

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts