## HELP!! want to see \$35 as 53 but how?

7 posts / 0 new
Author
Message

Hi, maybe you have a faster solution for this problem than me...

I want to show \$35 or any other hex value (up to \$FF) as a decimal value in my LCD.
At this moment I have made a compare routine (quite large) but, i'm sure there must be a quicker way...
the proc is a 8515.

any suggestions?

Hi MusicMan,
How large is your code? I have mine down to 23 instructions (with return) but you will need 3 additional "high" registers. Lemme know if you can do better! Hope this helps!!!

eseah

;**********************************************************************************************
;HEX-TO-ASCII conversion subroutine
;"high" registers : 4 (3 if "value" can be "low" register)
;"low" registers : 0 (1 if "value" can be "low" register)
;program words : 23 (including return)
;
;description : convert 0x00=hundreds
sub value,hundreds ;remove 100s from value
subi ones,-0x30 ;?00=tens
sub value,tens ;remove 10s from value
subi ones,-0x30 ;?0<=x<=?9, 2nd digit is '?'
mov tens,ones

mov ones,value ;3rd digit is the remainder
subi ones,-0x30 ;'ones' in ASCII

ret

sub100: subi hundreds,100 ;for compare with value
subi ones,1 ;use ones as temp storage for 1st digit
rjmp a100

sub10: subi tens,10 ;for compare with value
subi ones,1 ;use ones as temp storage for 2nd digit
rjmp a10

Oops... Guess I need to learn how to format code postings... Here's another try... Just cut and paste to a text editor

;**********************************************************************************************
;E. Seah
;03/24/01
;HEX-TO-ASCII conversion subroutine
;"high" registers : 4 (3 if "value" can be "low" register)
;"low" registers : 0 (1 if "value" can be "low" register)
;program words : 22+1 (including return)
;
;description : convert 0x00=hundreds
sub value,hundreds ;remove 100s from value
subi ones,-0x30 ;?00=tens
sub value,tens ;remove 10s from value
subi ones,-0x30 ;?0<=x<=?9, 2nd digit is '?'
mov tens,ones

mov ones,value ;3rd digit is the remainder
subi ones,-0x30 ;'ones' in ASCII

ret

sub100: subi hundreds,100 ;for compare with value
subi ones,1 ;use ones as temp storage for 1st digit
rjmp a100

sub10: subi tens,10 ;for compare with value
subi ones,1 ;use ones as temp storage for 2nd digit
rjmp a10

Maybe not the fastest, but it's pretty small. The end results are three registers containing the ascii values you're after. Please note that I am assuming that you have defined the following registers:

NUM ( the number to convert - will be destroyed)
HUNDREDS ( will hold ascii value for hundreds place)
TENS ( will hold ascii value for tens place)
ONES (will hold ascii value for ones place)

-----------------------------------------------------------------------------

ldi HUNDREDS, 48 ; '0' is ascii 48 (decimal)
ldi TENS, 48
ldi ONES, 48

hundreds_loop:

cpi NUM, 100
brlo tens_loop
inc HUNDREDS
subi NUM, 100
rjmp hundreds_loop

tens_loop:

cpi NUM, 10
brlo ones_loop
inc TENS
subi NUM, 10
rjmp tens_loop

ones_loop:

hope this helps,

Robert

You could, of course, output the values to the LCD as they are calculated. That would save you a couple of registers...

Robert

Thanks guys, that's the solution i was after!

thanx a lot!

MusicMan

I think, this was the smallest (only 10 words).

;input: R16 = 8 bit value 0 ... 255
;output: R18, R17, R16 = digits
;bytes: 20
;
bcd:
ldi r18, -1 + '0'
_bcd1:
inc r18
subi r16, 100
brcc _bcd1
ldi r17, 10 + '0'
_bcd2:
dec r17
subi r16, -10
brcs _bcd2
sbci r16, -'0'
ret

Peter