16BIT BIN TO BCD 5 digits

WBahn

Joined Mar 31, 2012
30,077
Wbahn,i have no idea what the hell your on about with the cards, How do i alter my programme to make add two 8bit files, and put them into five decimal ones, THOUK, THOU,HUNS,TENS,UNITS,
The cards have a one-to-one correspondence with your variables. Just write your code so that it does the same things to the variables that I described doing to the cards.

But if youi are just adding two 8-bit values, then how can you end up needing five decimal values? The largest value you can get is 255+255 = 510, so your THOUK (which used to be TENK, why the change?) and THOU will always be zero.
 

MrChips

Joined Oct 2, 2009
30,824
Why not scrap that program and write your own with either of the two algorithms I have given you?

Try both and see which you prefer.
 

WBahn

Joined Mar 31, 2012
30,077
22 Posts and still not got an answer......
Because you still haven't put forth any effort of your own. Normally, we want to see some effort before we even give hints. In your case, we've given you entire algorithms and you aren't willing to even attempt to implement them. Instead, you want someone to do ALL of your work for you and just hand it over without you needing to make any effort to understand any of it.

LETS EXPLAIN IT , in my asm pic i have two files adresh and adresl both are 8 bits, so the total will be 65535 (255x 255) all i want to do is to put the results of these files into

FIVE FILES THOUK, THOU, HUNS, TENS, UNITS, SO I CAN PUT THEM ON A SEVEN SEG DISPLAY,
Okay. That is what I originally thought you wanted, but then you said that you wanted to add two 8-bit values together and convert that into BCD.

the asm file i posted does this for FOUR FILES, it converts 10 bits into ThOU, HUNS,TENS,UNITS how do i convert 16 bits into THK, THO,HUNS,TENS,UNITS
Are you willing to make ANY effort to understand how the code you posted works?

With the level of effort you are showing you are willing to put in, it is hard to guess what level of detail we would have to explain it at. For instance, do you have the faintest idea how to subtract a 16-bit value from a 16-bit value if all you have available are 8-bit operations?
 

mitko89

Joined Sep 20, 2012
127
Guess the other guys will hate me for that... This is part of the tutorials written by Nigel Goodwin. I guess that is what you are looking for?

Rich (BB code):
;This routine downloaded from http://www.piclist.com
Convert:                        ; Takes number in NumH:NumL
                                ; Returns decimal in
                                ; TenK:Thou:Hund:Tens:Ones
        swapf   NumH, w
	iorlw	B'11110000'
        movwf   Thou
        addwf   Thou,f
        addlw   0XE2
        movwf   Hund
        addlw   0X32
        movwf   Ones

        movf    NumH,w
        andlw   0X0F
        addwf   Hund,f
        addwf   Hund,f
        addwf   Ones,f
        addlw   0XE9
        movwf   Tens
        addwf   Tens,f
        addwf   Tens,f

        swapf   NumL,w
        andlw   0X0F
        addwf   Tens,f
        addwf   Ones,f

        rlf     Tens,f
        rlf     Ones,f
        comf    Ones,f
        rlf     Ones,f

        movf    NumL,w
        andlw   0X0F
        addwf   Ones,f
        rlf     Thou,f

        movlw   0X07
        movwf   TenK

                    ; At this point, the original number is
                    ; equal to
                    ; TenK*10000+Thou*1000+Hund*100+Tens*10+Ones
                    ; if those entities are regarded as two's
                    ; complement binary.  To be precise, all of
                    ; them are negative except TenK.  Now the number
                    ; needs to be normalized, but this can all be
                    ; done with simple byte arithmetic.

        movlw   0X0A                             ; Ten
Lb1:
        addwf   Ones,f
        decf    Tens,f
        btfss   3,0
        goto   Lb1
Lb2:
        addwf   Tens,f
        decf    Hund,f
        btfss   3,0
        goto   Lb2
Lb3:
        addwf   Hund,f
        decf    Thou,f
        btfss   3,0
        goto   Lb3
Lb4:
        addwf   Thou,f
        decf    TenK,f
        btfss   3,0
        goto   Lb4

        retlw	0x00
 

WBahn

Joined Mar 31, 2012
30,077
Guess the other guys will hate me for that...
Nope. Certainly don't hate you. I just hope that you realize that you haven't really helped him very much. You've just enabled him to get one thing working without having gained any knowledge of how or why it works. You're enabling him to continue being a random monkey. Not even a code monkey, because code monkeys generally understand the mechanics of the code they write at least at the level they are writing it. He is closer to the proverbial monkey that types randomly on the keyboard, except the randomness here is that he sticks his hand out and hopes some random person will give him a piece of code that just happens to do exactly what he wants, because he's not going to be willing or able to make the slightest modification to it on his own if it isn't an exact match because he doesn't have the slightest idea how or why it works. All he's learned from this instance is that if he keeps saying "just tell me" long enough, some random person will come along and stick a pacifier in his mouth (i.e., give him what he wants).
 

WBahn

Joined Mar 31, 2012
30,077
So you present another chunk of code that you got from somewhere that doesn't do exactly what you want and, instead of making any attempt to understand it, you just put your hand out and say, "just tell me".

Yep, you learned exactly what I said you would learn.

I gave you all the information you needed to answer that question in Post #15

http://forum.allaboutcircuits.com/showpost.php?p=631975&postcount=15

You won't even try to apply it and just want a handout.

Sorry. I'm done with you.
 
@Dodgydave
If you are using MPLAB IDE, there is a very good simulator called MPLAB SIM.
You will be able to go through the code step by step and watch the registers (files) as you go along, to see what is happening exactly.
If you only do this with the code you would like to examine it will be easy to make small adjustments, recompile and run the simulator again.

But for now I am unsure what you are looking for. Do you want to pack/unpack digits, or do want convert binary to ascii characters?
I'm guessing the latter

regards
 

MrChips

Joined Oct 2, 2009
30,824
Bare in mind that binary to BCD conversion is a classic exercise in any introductory course in assembly language programming.
 

MrChips

Joined Oct 2, 2009
30,824
That is because you are seeking the wrong solution. This is an exercise given to students after one lesson on assembly language programming.
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,307
OK this is what i have from other web sites its for two 8 bit binary files, which is what i have so great,
but whatever value i put into resultlo or resulthi the thou10 is always 7, it dont work.and i dont know how to correct it,
i am putting 255 into both resulthi and resultlo, thats 65355, so it should show 6 in thou10, 5 in thou, 3 in huns, 5 in tens, 5 in units,

BUT when i run it in the simulator

i get 7 in thou10, fc in thou, ff in huns, 4 in tens, 6b in units ???

this is the link for the asm file

PIC CODE




Rich (BB code):
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
; NEW ROUTINE FOR FIVE FILES THOU10, THOU, HUNS, TENS, UNITS,
;This routine downloaded from http://www.piclist.com
Convert5                        ; Takes number in RESULTHI:RESULTLO
                                ; Returns decimal in
                                ; TenK:Thou:Hund:Tens:Ones
        swapf   RESULTHI, w
		iorlw	B'11110000'
        movwf   THOU
        addwf   THOU,f
        addlw   .226
        movwf   HUNS
        addlw   .50
        movwf   UNITS

        movf    RESULTHI,w
        andlw   .15
        addwf   HUNS,f
        addwf   HUNS,f
        addwf   UNITS,f
        addlw   .233
        movwf   TENS
        addwf   TENS,f
        addwf   TENS,f

        swapf   RESULTLO,w
        andlw   .15
        addwf   TENS,f
        addwf   UNITS,f

        rlcf    TENS,f
        rlcf    UNITS,f
        comf    UNITS,f
        rlcf    UNITS,f

        movf    RESULTLO,w
        andlw   .15
        addwf   UNITS,f
        rlcf    THOU,f

        movlw   .7
        movwf   THOU10

                    ; At this point, the original number is
                    ; equal to
                    ; TenK*10000+Thou*1000+Hund*100+Tens*10+Ones
                    ; if those entities are regarded as two's
                    ; complement binary.  To be precise, all of
                    ; them are negative except TenK.  Now the number
                    ; needs to be normalized, but this can all be
                    ; done with simple byte arithmetic.

        movlw   .10                             ; Ten
Lb1:
        addwf   UNITS,f
        decf    TENS,f
        btfss   STATUS,C
        goto   Lb1
Lb2:
        addwf   TENS,f
        decf    HUNS,f
        btfss   STATUS,C
        goto   Lb2
Lb3:
        addwf   HUNS,f
        decf    THOU,f
        btfss   STATUS,C
        goto   Lb3
Lb4:
        addwf   THOU,f
        decf    THOU10,f
        btfss   STATUS,C
        goto   Lb4

        retlw	0x00

end
 
Last edited:
It should work I've got your script from #36 here right in front of me in MPLAB, and it works here.

Are you sure RESULT registers get loaded right?
Is the right register bank selected for these registers?

It must be something very officious.
 
My mistake, I was simulating as a PIC16F1503, as that's more common for me.
I just changed it to 18F4550 and got some odd results.

According to the data sheet of 18F4550 the intruction DECF affects all of the status flags.
This is a problem since the script is testing the Carry flag set in the ADDWF instruction.

Solution:
addwf UNITS,f
decf TENS,f
vs
decf TENS,f
addwf UNITS,f
 
The code is working for the 18f2321 just like a 18F4550.(I just tried)

did you get the code working now? (after swapping the addwf and decf instructions)
 
Here you go !

Rich (BB code):
        swapf   RESULTHI, w
		iorlw	B'11110000'
        movwf   THOU
        addwf   THOU,f
        addlw   .226
        movwf   HUNS
        addlw   .50
        movwf   UNITS

        movf    RESULTHI,w
        andlw   .15
        addwf   HUNS,f
        addwf   HUNS,f
        addwf   UNITS,f
        addlw   .233
        movwf   TENS
        addwf   TENS,f
        addwf   TENS,f

        swapf   RESULTLO,w
        andlw   .15
        addwf   TENS,f
        addwf   UNITS,f

        rlcf    TENS,f
        rlcf    UNITS,f
        comf    UNITS,f
        rlcf    UNITS,f

        movf    RESULTLO,w
        andlw   .15
        addwf   UNITS,f
        rlcf    THOU,f

        movlw   .7
        movwf   THOU10

                    ; At this point, the original number is
                    ; equal to
                    ; TenK*10000+Thou*1000+Hund*100+Tens*10+Ones
                    ; if those entities are regarded as two's
                    ; complement binary.  To be precise, all of
                    ; them are negative except TenK.  Now the number
                    ; needs to be normalized, but this can all be
                    ; done with simple byte arithmetic.

        movlw   .10                             ; Ten
Lb1:
        decf    TENS,f
        addwf   UNITS,f
        btfss   STATUS,C
        goto    Lb1
Lb2:
        decf    HUNS,f
        addwf   TENS,f
        btfss   STATUS,C
        goto    Lb2
Lb3:
        decf    THOU,f
        addwf   HUNS,f
        btfss   STATUS,C
        goto    Lb3
Lb4:
        decf    THOU10,f
        addwf   THOU,f
        btfss   STATUS,C
        goto    Lb4
edited, cause some how lost the last two lines of my post:
Do you see where I swapped the ADDWF and DECF instructions?
Not sure how microchip could change the behavior of DECF like that. In the smaller chips DECF only changes the Z flag.
 
Last edited:

MaxHeadRoom

Joined Jul 18, 2013
28,700
I'm not sure if it will confuse the issue, but I have this routine for early 17C pic.
Should convert easily?
I can post if you think it might be useful?
;********************************************************************
; Binary To BCD Conversion Routine (16 Bit)
; (LOOPED Version)
;
; This routine converts a 16 Bit binary Number to a 5 Digit
; BCD Number.
;
; The 16 bit binary number is input in locations Hbyte and
; Lbyte with the high byte in Hbyte.
; The 5 digit BCD number is returned in R0, R1 and R2 with R0
; containing the MSD in its right most nibble.
;
; Performance :
; Program Memory : 32
; Clock Cycles : 750
;
;*******************************************************************;

Max.
 
This is the code as you send did modified as a full .asm file.
If this doesn't work for you I'll give up.

Rich (BB code):
    list        p=18f2321
	#include    <p18f2321.inc>

                 UDATA         
RESULTHI       RES        1
RESULTLO       RES        1
THOU10         RES        1
THOU           RES        1
HUNS           RES        1
TENS           RES        1
UNITS          RES        1

    CODE    0x0000
start
    movlw   .255
    movwf   RESULTHI
    movwf   RESULTLO

    call    Convert
    goto    start


;This routine downloaded from http://www.piclist.com
Convert:                        ; Takes number in NumH:NumL
                                ; Returns decimal in
                                ; TenK:Thou:Hund:Tens:Ones
        swapf   RESULTHI, w
		iorlw	B'11110000'
        movwf   THOU
        addwf   THOU,f
        addlw   .226
        movwf   HUNS
        addlw   .50
        movwf   UNITS

        movf    RESULTHI,w
        andlw   .15
        addwf   HUNS,f
        addwf   HUNS,f
        addwf   UNITS,f
        addlw   .233
        movwf   TENS
        addwf   TENS,f
        addwf   TENS,f

        swapf   RESULTLO,w
        andlw   .15
        addwf   TENS,f
        addwf   UNITS,f

        rlcf    TENS,f
        rlcf    UNITS,f
        comf    UNITS,f
        rlcf    UNITS,f

        movf    RESULTLO,w
        andlw   .15
        addwf   UNITS,f
        rlcf    THOU,f

        movlw   .7
        movwf   THOU10

                    ; At this point, the original number is
                    ; equal to
                    ; TenK*10000+Thou*1000+Hund*100+Tens*10+Ones
                    ; if those entities are regarded as two's
                    ; complement binary.  To be precise, all of
                    ; them are negative except TenK.  Now the number
                    ; needs to be normalized, but this can all be
                    ; done with simple byte arithmetic.

        movlw   .10                             ; Ten
Lb1:
        decf    TENS,f
        addwf   UNITS,f
        btfss   STATUS,C
        goto    Lb1
Lb2:
        decf    HUNS,f
        addwf   TENS,f
        btfss   STATUS,C
        goto    Lb2
Lb3:
        decf    THOU,f
        addwf   HUNS,f
        btfss   STATUS,C
        goto    Lb3
Lb4:
        decf    THOU10,f
        addwf   THOU,f
        btfss   STATUS,C
        goto    Lb4

        retlw	0x00

    END
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
11,307
Now its working perfect now, thanks Mark.

.i think its because i am using the Run mode and should be using Animate.
 
Last edited:
How did you get everything converted to lower-case?
STATUS, C has to be upper-case.

I'm lost now, and yes, I am getting 6, 5, 5, 3, 5, and the code is already putting the .255 in both 8 bit registers.

I'm lost
 
Top