## How to convert a decimal number in a register into an ascii format?

17 posts / 0 new
Author
Message

Lets say you have the decimal number 44 stored in an 8 bit AVR register and need to display it on a  hd44780 lcd. I need to pull out each nibble (4bits) and convert it to 8bits and display them one digit at a time on the lcd. How would you do this?

Thanks

Roger

This topic has a solution.
Last Edited: Sun. Sep 22, 2019 - 02:02 PM
Total votes: 0

Theres any number of ways to do this.

ITOA comes to mind.

Mopar_512 wrote:
I need to pull out each nibble (4bits) and convert it to 8bits

Why?

'44' decimal is 00101100 in binary and 0x2C in hex.  ITOA would take the number in the register and do the work for you

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

"The critical shortage here is not stuff, but time." - Johan Ekdahl

"Step N is required before you can do step N+1!" - ka7ehk

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

Total votes: 0

Greetings -

(1) Is that "44" really decimal or is it hex? It likely does not exist in the register as one nibble of "4" and another nibble of "4"; because THAT would be BCD. It would only be BCD under unusual circumstances, such as you putting it there as BCD or from an external peripheral that outputs in BCD.

(2) If it is 0b01000100 (0x44) then use "itoa()" from avr-lbc to convert it to a string; you can specify whether the output string shows the decimal representation or the hex representation. If it is decimal 44, then the register will contain 0x2c = 0b00101100. You can use the same function, telling it to use base-10 as the output.

Jim

Until Black Lives Matter, we do not have "All Lives Matter"!

Total votes: 0

I do not have any of the C programming stuff available to me. I only have the avr assembler available and I like to see how it done with just the avr assembler.

Roger

Total votes: 0

Here's a routine found on the web (not tested) for binary to BCD in AVR assembly (Link).

Total votes: 0

I really don't think that Mopar_512 was looking for a BCD conversion. I think that he or she misunderstood how numbers are stored in an 8-bit register.

Jim

Until Black Lives Matter, we do not have "All Lives Matter"!

Total votes: 0

Here's a few good links

https://www.avrfreaks.net/forum/16bit-binary-ascii

http://www.avr-asm-tutorial.net/avr_en/calc/CONVERT.html#bin2asc

Note these tend to "save" all the ascii digits, until finished...if just sending to an lcd you can send them  one-by-one (assuming updating display left MSD to right LSD)

The one by Beeder assumes ZH:ZL (first of array of 5 digits)  is preset to where you want the results (ascii digits) stored

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Total votes: 0

First thing to note is I only have the avrasm2 assembler available. I am using an ldi rmp, \$44 just to get the number into the register so I can test code to see if I can get it to display on the lcd. To get it to display I have to extract out each nibble, convert it to ascii, and then display each one with a separate out operation.

I think I must probably use the lsl and lsr instructions to strip the bits out of the register as I need them.

The reason I asked this question is because I just thought some of the experienced programmers here might know a slick little trick to do this.

Thanks,

Roger

Total votes: 0

Mopar_512 wrote:
o get it to display I have to extract out each nibble, convert it to ascii, and then display each one with a separate out operation.

Lemme guess....you have the LCD running in 4 bit mode?

If so, then your LCD driver should handle the setting up of the data to send to the LCD for you.  Unless this is a home rolled driver.

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

"The critical shortage here is not stuff, but time." - Johan Ekdahl

"Step N is required before you can do step N+1!" - ka7ehk

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

Total votes: 0

What is the range of your register value?

Is it 0 to 99 or 0 to 255?

This reply has been marked as the solution.
Total votes: 0

The "trick" in those links essentially subtracts 100's & counts 'em up (that's the 100's digit) then takes the leftovers & does the same with 10's (the ten's digit) , then 1's digit.  Also, you have to ass 0x30 to each to form the ASCII character value for the display digits.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Total votes: 0

Doing that conversion nibble-wise gives you hex, in this case. what happens if you put 0b11111111 into that register? What do you see, then, on your LCD? If you are actually displaying decimal, you OUGHT to see 255.

Jim

Until Black Lives Matter, we do not have "All Lives Matter"!

Last Edited: Sun. Sep 22, 2019 - 05:39 AM
Total votes: 0

I think we need to hear what you actually want to appear on the display. Say your register contains 44{2C) then what should appear? How about 99(63) or 251(FB)?

Total votes: 0

The "trick" in those links essentially subtracts 100's & counts 'em up (that's the 100's digit) then takes the leftovers & does the same with 10's (the ten's digit) , then 1's digit.  Also, you have to ass 0x30 to each to form the ASCII character value for the display digits.

conversions here.

After reading this post the solution came to me. This is a hex to decimal conversion algorithm that leaves the converted numbers in the correct ascii format to display in the lcd. Problem solved.

Thanks,

Roger

Total votes: 0

Well, I assume that you want the data 0x44(\$44) stored in some register to appear in some display(like 16x2 character lcd) as 44.

Using AVR Assembly it's fairly simple and basically a two step process -

1. Separate the nibbles. In your case 0x44 will make two registers holding 0x04 and 0x04 each.

2. Oring them with 0b00110000(0x30), this will make them ascii equivalent of 4 and send them to the display

I've used this trick several times while displaying RTC data to LCD display.....

Good Luck....

PS: I appreciate your approach to write programs in Assembly.....

Total votes: 0

Sounds like you wanted binary to bcd. Mentioning hex just confuses the issue. There’s two common algorithms - divide by descending powers of 10 or a modified division that corrects the value by adding 3 to the nibble. There’s been plenty of discussion in the past regarding this. The key term is ‘binary to bcd’

Total votes: 0

But if it's an HD44780 then it wants ASCII ? (so BCD digits + '0')