Binary to Decimal conversion

Status
Not open for further replies.

zeeba

New Member
Hi

I want an assembly code for pic16f876a to convert from 16 bit binary number to 5 digit decimal and display it on LCD
can any one help me ??
 
Check my PIC tutorials, many of which do exactly that - try the analogue input one for a start.
 
I checked it but I didn't find something releated to converting numbers

????

Tutorial 11.1:


The code is in the ZIP file at the top of the page.
 
I'd bet it's a safe assumption that Nigel's method will be better than mine, but here's what I do with an 8 bit number -- I'm sure you can adapt it to a 16 bit number easily.

I of course have all my CALLs for NUM1, NUM2, NUMx, etc already coded.

I take the 8 bit number and divide by 100. This gives an answer of either zero, one, or two with a remainder of whatever.

My division routine splits the quotient into a register called ANSWER and remainder the remainder into a register called REMAINDER.

So, 235/100 in my divide by 100 routine leaves me with user register ANSWER = 2 and user register REMAINDER = 35.

I then multiply ANSWER by two. I'm not 100% on this because I'm not looking at my code and its been a while since I wrote it, but I think you can just clear the carry flag and then use RLF (one time only) on ANSWER.

There's a reason I don't just divide by 50 above. It may seem silly to divide by 100 and then multiply by 2, but this method keeps you from getting odd numbers in the ANSWER register (again, this is just a user register I assigned for the quotient of my division routine) which will mess up your lookup table.

Code:
MOVF      ANSWER,W
CALL       LUTABLE

.
.
.
(FAR FAR AWAY IN MY CALL ROUTINE SECTION)
LUTABLE
ADDWF    PC,f
CALL   NUM0
RETURN
CALL   NUM1
RETURN
CALL   NUM2
RETURN
CALL NUM3
RETURN
...
..
..
..
CALL NUM9
RETURN

When that's done, move REMAINDER into the NUMERATOR of your division routine and divide by 10. So remember with our example above we had 235 to start with.

We already displayed "2" on the LCD -- and the remainder we were left with was 35. Dividing by 10 gives us 3 in ANSWER and 5 in REMAINDER. Clear the carry flag and RLF on ANSWER (I think, triple check me here) to multiply by two. Call the look up table.

When you get to the ones place, DO NOT DIVIDE BY ANYTHING. It is already in your REMAINDER from when you divided by 10. Multiply it by two and call the lookup table.

The LCD will now have "235" displayed.

As a side note, be careful of where you are in memory when you add w to the program counter. I think it's PCALTH that needs to be pre-loaded occasionally to keep you from jumping off somewhere wierd. Also double check me on the lookup table where I ADDWF PC,f -- run the code in MPLAB SIM to confirm it goes where it should.
 
Last edited:
I'd bet it's a safe assumption that Nigel's method will be better than mine, but here's what I do with an 8 bit number -- I'm sure you can adapt it to a 16 bit number easily.

I got my routines off the PICList a LONGGGGG! time ago

I've used them for many years, they are fast, flawless and easy to use.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…