Bcd to bin routine

Discussion in 'Embedded Systems and Microcontrollers' started by Dodgydave, Jul 18, 2013.

  1. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,998
    745
    I want to add the total in an A/D conversion in adresh and adresl files and then split them into hundreds tens units, i know how to do the a/d routine, just need help with adding the two files using the conversion, maximum would be 500, anybody got any ideas how to do it in ASM using a pic16f690.
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    Sure. You can do division by repeated subtraction. You start by subtracting 100 from the A/D result until the result goes negative. Each time the result is positive you increment a counter. When the result goes negative you add 100 back and that is the reminder.

    Now you switch to 10. Repeatedly subtract 10 until the dividend goes negative. Increment the 10's counter each time the result is positive. When the result goes negative add the 10 back in and the result will be the remainder.
     
  3. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,998
    745
    I ready have that working, for adresl, i need to add the contents of addresh to the Huns tens units, its the adding of two 8bit files hows that done???
     
  4. bance

    Member

    Aug 11, 2012
    315
    34
    Here's a tutorial for you!

    HTH Steve
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,148
    1,791
    It's pretty easy
    1. Add the low bytes together
    2. Add the high bytes together WITH CARRY
    All 8 bit processors have a carry flag that is used in multi-byte arithmetic. When the carry is 0, then the add with carry instruction is just like a normal add. When it is a 1, then the add with carry adds two bytes plus the carry bit in a single operation. You can extend this concept to operands with any number of bytes.
     
  6. Dodgydave

    Thread Starter Distinguished Member

    Jun 22, 2012
    4,998
    745
    I have it working now , done a bin/bcd routine from microchip web site. thanks


    Code ( (Unknown Language)):
    1.  
    2. LEFT JUSTIFIED, UPPER 8 BITS IN ADRESH, LOWER 2 BITS IN ADRESL
    3.  
    4.  
    5. Unpack
    6.         swapfw   bcdH                 ; unpack BCD digits
    7.         andlw   0x0F
    8.         movwf   thou
    9.         movfw    bcdH
    10.         andlw   0x0F
    11.         movwf   huns
    12.        
    13.         swapfw   bcd
    14.         andlw   0x0F
    15.         movwf   tens
    16.         movfw    bcdL
    17.         andlw   0x0F
    18.         movwf   units
    19.         return
    20. ;-------------------------------
    21. ;
    22. ; Upper 10-bit Binary To Packed BCD Digits
    23. ;
    24. Convert_bcd
    25.  
    26.         clrf    bcdL            ; clear result
    27.         clrf    bcdH
    28.         movlw   .10             ; set bit counter
    29.         movwf   count
    30.        
    31. bcd_lp  movlw   0x33            ; adjust BCD result
    32.         addwf   bcdL ,f
    33.         btfsc   bcdL,3
    34.         andlw   0xF0 ,f
    35.         btfsc   bcdL,7
    36.         andlw   0x0F,f
    37.         subwf   bcdL ,f
    38.        
    39.         movlw   0x03
    40.         addwf   bcdH
    41.         btfss   bcdH,3
    42.         subwf   bcdH,,f
    43.        
    44.         rlf     ADRESL ,f           ; shift out a binary bit
    45.         rlf     ADRESH,f
    46.         rlf     bcdL ,f           ; into BCD result
    47.         rlf     bcdH ,f
    48.        
    49.         decfsz  count ,f          ; repeat for all bits
    50.         goto    bcd_lp
    51.         return                  ; exit
    52.  
     
    Last edited by a moderator: Jul 19, 2013
    absf likes this.
Loading...