Continue to Site

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.

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

10 Bit Binary to Decimal Convertion Help

Status
Not open for further replies.
OK, I finished my subroutine. I tested it a bit and it seems to work well. Further testing may find bugs. We shall see...

It converts a 10-bit value (could be easily modified to do 16 bits too) to four ASCII bytes, thou, hund, ten and one, ready for outputting to display.

If you don't want ASCII then just comment out the 'addlw 0x30' lines.

I mooched the 16-bit subtract and addition routines from Chuck McManis' site. The rest of the code is mine.


Code:
	cblock	0x00
		count,dst2,dst1,src2,src1,thou,hund,ten,one
	endc

;***********************************************************
;* 10 bit converter - pass MSB in dst1, LSB in dst2
;* Returns digits as ASCII values in thou, hund, ten and one
;* Requires src2, src1 and dst2, dst1 variables in that 
;* little-endian order (LSB first)
fix10b	movlw	0x03			;src = 1000 ($3e8)
	movwf	src1
	movlw	0xe8
	movwf	src2
thous	call	sub16			;subtract 1000 from number
	btfsc	STATUS,C		;negative result?
	goto	thouyes			;no, go set thou=1
	call	add16			;add back failed subtract 
	movlw	0x30			;yes, set thou=0 
	movwf	thou
	goto	hundred			;and move on to hundreds
thouyes	movlw	0x31			;set thou=1
	movwf	thou

hundred	clrf	count			;init counter=0
	clrf	src1			;src = 100
	movlw	0x64
	movwf	src2
hund_1	call	sub16			;subtract 100 from number
	btfsc	STATUS,C		;negative result?
	goto	h_again			;no, go again
	movf	count,W			;yes, get count in W
	addlw	0x30			;add $30 to make it ASCII
	movwf	hund			;and store it in hund variable
	call	add16			;add back last subtract
	goto	tens			;and go do tens
h_again	incf	count			;increment hundreds count
	goto	hund_1			;and go again

tens	clrf	count			;init counter=0
	clrf	src1			;src=10
	movlw	0x0a
	movwf	src2
tens_1	call	sub16
	btfsc	STATUS,C		;negative result?
	goto	t_again			;no, go again
	movf	count,W			;yes, get count in W
	addlw	0x30			;add $30 to make it ASCII
	movwf	ten			;store tens value
	call	add16			;put last subtract back
	movf	dst2,W			;get remainder
	addlw	0x30			;add $30 to make it ASCII
	movwf	one			;store ones
	return
t_again	incf	count			;increment tens counter
	goto	tens_1			;and go again

sub16	movf	src2,W
	subwf	dst2
	movf	src1,W
	btfss	STATUS,C
	incf	src1,W
	subwf	dst1
	return

add16	movf	src2,W
	addwf	dst2
	movf	src1,W
	btfsc	STATUS,C
	incf	src1,W
	addwf	dst1
	return
 
Last edited:
Hi futz thanks for spending time.Your code looks good but it will take another couple of days for me to understand that:D

The 8 bit to 3 digits conversion is excellent I tried last night working great.
I'm thinking to update that routine to 10 bit as well but it won't come to mind how to do that.
 
Suraj143 said:
Hi futz thanks for spending time.
No problem. I was gonna have to do it eventually anyway. I'll use it somewhere for sure.

Your code looks good but it will take another couple of days for me to understand that:D
Well, one thing about my code is that it's simple. There's no fancy tricks in there. It's just dumb, simple brute force. Easy to understand that way.
 
Hi futz can you tell me whats the four result bits that I can move to four SSD.

Is it this four?
dst2,dst1,src2,src1
 
Suraj143 said:
Hi futz can you tell me whats the four result bits that I can move to four SSD.

Is it this four?
dst2,dst1,src2,src1
???????? :confused:
It converts a 10-bit value (could be easily modified to do 16 bits too) to four ASCII bytes, thou, hund, ten and one, ready for outputting to display.

Does that answer your question? That quote is from my post with the code. The comments in the code also say the same thing.
 
Suraj143 said:
Hi futz in this line where to put the result? subwf dst2
You've lost me. What do you mean by that?

Oh, you mean you think it should read subwf dst2,F? You can put that in if you want, but it's the default anyway, so I just leave it off. ,W is the optional one.
 
futz said:
You've lost me. What do you mean by that?

Oh, you mean you think it should read subwf dst2,F? You can put that in if you want, but it's the default anyway, so I just leave it off. ,W is the optional one.

It's really advisable to always include the destination, you're only going to give yourself nasty bugs if you don't!.
 
Nigel Goodwin said:
It's really advisable to always include the destination, you're only going to give yourself nasty bugs if you don't!.

I agree, I hate getting warnings and always try to rectify them.

Personally, I find that thinking is 95% of programming and typing is the other 5% (it may be 99 - 1). With this in mind, I have labels that are self explanatory and try to write code that is self documented.

To me, the time saved by not typing ,F is time lost in the 1 in 1000 times that it causes a bug.

Mike.
 
Pommie said:
To me, the time saved by not typing ,F is time lost in the 1 in 1000 times that it causes a bug.
Ok, I'll bite. Why would that cause a bug? And why only 1 in 1000 times? It's not like the cpu just decides to do something else once in a while. If ',F' is default then that's what happens unless I tell it otherwise (,W). Period. :)
 
Because once in a while you will inadvertently miss the ,W and the warning will be lost amongst the myriad others (or turned off).

Do you agree with the premise that thinking is the majority of the programming process and therefore any time spent making code more understandable is well spent?

Mike.
 
Pommie said:
Do you agree with the premise that thinking is the majority of the programming process and therefore any time spent making code more understandable is well spent?
Can't hurt. That's why I eliminate any unnecessary clutter whenever possible. :D I used to type the ,F always, but finally dropped it. I'm thinking it when I type the code in though.

So in future any code I post here will have ,F's, just to make you guys happy! :D
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top