Bcd to bin routine

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,302
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.
 

Papabravo

Joined Feb 24, 2006
21,225
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.
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,302
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???
 

Papabravo

Joined Feb 24, 2006
21,225
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.
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,302
I have it working now , done a bin/bcd routine from microchip web site. thanks


Rich (BB code):
LEFT JUSTIFIED, UPPER 8 BITS IN ADRESH, LOWER 2 BITS IN ADRESL


Unpack
        swapfw   bcdH                 ; unpack BCD digits 
        andlw   0x0F 
        movwf   thou 
        movfw    bcdH 
        andlw   0x0F 
        movwf   huns 
        
        swapfw   bcd 
        andlw   0x0F 
        movwf   tens 
        movfw    bcdL 
        andlw   0x0F 
        movwf   units
        return
;------------------------------- 
; 
; Upper 10-bit Binary To Packed BCD Digits 
; 
Convert_bcd 

        clrf    bcdL            ; clear result 
        clrf    bcdH 
        movlw   .10             ; set bit counter 
        movwf   count 
        
bcd_lp  movlw   0x33            ; adjust BCD result 
        addwf   bcdL ,f
        btfsc   bcdL,3 
        andlw   0xF0 ,f
        btfsc   bcdL,7 
        andlw   0x0F,f 
        subwf   bcdL ,f
        
        movlw   0x03 
        addwf   bcdH 
        btfss   bcdH,3 
        subwf   bcdH,,f 
        
        rlf     ADRESL ,f           ; shift out a binary bit 
        rlf     ADRESH,f
        rlf     bcdL ,f           ; into BCD result 
        rlf     bcdH ,f
        
        decfsz  count ,f          ; repeat for all bits 
        goto    bcd_lp 
        return                  ; exit
 
Last edited by a moderator:
Top