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.

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 ??
 
I checked it but I didn't find something releated to converting numbers

????

Tutorial 11.1:

This tutorial reads a single channel of the A2D converter and displays it on the LCD. First the program sets up the registers explained above, then the rest of the program runs in an endless loop. It reads the analogue input, which gives a value from 0-$3FF (in hexadecimal), then calls a routine which converts that to decimal, giving 0-1023 (representing 0-10.23V). Lastly this decimal value is displayed on the LCD, followed by a space, and the same value in hexadecimal. The program then wait for 100mS and jumps back to the start of the loop and runs again, this gives roughly 10 readings a second.

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:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top