Hello everyone.
I'm kind of newbie in MCU programming, and recently I was doing something with 8 bit PIC18 micro, and wanted to do a MCU runtime counter.
When it is powered on, every second interrupt is fired and 1st register counts seconds from 0 to 3Ch (60d). 2'nd register counts minutes (0 to 3Ch). 3'rd register count hours 0 to 18h (24d). and 4'th register counts days 0 - FFh.
MCU runtime is saved to EEPROM every minute. Also I wanted to send this information to USART by typing some command. But instead of hex numbers I wanted to see decimal numbers in terminal. So I made a look-up table where values from all 5 registers was converted to ASCII coded decimal numbers.
Table was for numbers 0d - 255d (this is kind of bulky I think). For ex minutes in hex 37h was decoded in table and sent to USART as ASCII number "055" ("\x30\x35\x35\r\n").
I wanted to get rid of that bulky table. So next step was to make some kind of HEX to DEC converter. And Now my ASCII look-up table shrinked to ten entries (for numbers 0 to 9) and it could decode numbers from 0h to FFh. I know there are ready HEC to DEC converters in C language, but as i am still learning many things, I wanted to get some practice in assembly and made my own converter (which I did).
And it works by taking hex number in WREG, converting it to decimal equivalent and putting out to three registers. Then it is possible to use ASCII look up table and to send it to USART. For ex number 0Eh is converted to 0, 1, 4 or number FFh to 2, 5, 5. I attach algorithm of this converter in PDF file. And if someone thinks it's not that useless and may be needs something like this in assembly language for PIC18 I will post code here.
And finally my question. This converter is usable only in range of numbers 0h - FFh (which is good for converting minutes or hours (max value 60d)). But I thought it would be nice to make (or just to understand how it works) another one for numbers in range of FFh to FF FFh or even to FF FF FFh. For example if I want to send 4 byte data from sensor to USART and display it in decimal format.
I know one can use 32 bit MCU and C for this task and something like that and some ready available solutions and don't bother. But I don't need it for anything right now. This is like interesting puzzle to me and I'm just curious and want to understand how this can be accomplished (I am trying to do it in assembly for 8 bit MCU). Does anyone know some sort of algorithm or equation? is it possible to do it using standard math functions like add, subtract, multiply which are available in PIC18. I tried to think of it myself but for now nothing simple and elegant comes to my mind and my algorithm is too complex. Also I done a lot of searching and nothing. And yes I know how to do it easily using calculator or (sometimes) my brain which is decimal (most of the time)