Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Convert binary number to Decimal number C code using PIC micrcontroller

Status
Not open for further replies.

erfan.khalilian69

Member level 4
Member level 4
Joined
Feb 7, 2011
Messages
71
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
Kualalampur
Activity points
1,916
Hi everyone can anyone tell me how to convert binary number to decimal value,
i just need sample code
thanks
 

Here's the basic algorithm:

Code:
   x = binary number     // assume the number is unsigned
   d[1..5] = 0               // will contain 5 bcd digits of decimal number
   digits = 5                 // a 16-bit binary number can only produce 5 digits

   repeat
      d[digits] = x % 10   // extract the lowest digit
      x = x / 10               // shift down the next digit
      digits = digits - 1
   until x = 0

I can also dig out another algorithm that doesn't require the division and modulo operations if needed.
 
Last edited:
Hi actually this algorithm I can't got the logic , can you give me the sampe ?
I am using dip switch and i want to display it in LCD and count it down until zero.
I just write the code as :
dip_read = PORTB ;
now I think I need to convert the dip_read value to decimal
right ?
do you have any idea ?
 

No, the conversion does no exist, it is just the way you threat the variable he you are programming. You dont nee any conversion.. let say you have 00000011 on your port, your varible is going to be 0b00000011 and at the same time 0x03 and 3 (decimal). The microcontoller just know binary, all the rest is handled by the compiler to create that abstraction layer to the programmer. The conversion you need is just to convert to an ASCII to print on the display....
 
If you want convert dip switch, can do like this
i=0;
if(IN1==1) i=i+1;
if(IN2==1) i=i+2;
if(IN3==1) i=i+4;
if(IN4==1) i=i+8;
if(IN5==1) i=i+16;
if(IN6==1) i=i+32;
if(IN7==1) i=i+64;
if(IN8==1) i=i+128;
return i;
 
Hi everyone can anyone tell me how to convert binary number to decimal value,
i just need sample code
thanks

Do you mean you need the code written in assembly?
And it seems that your maximum number (one byte) could be 255 or less, so it needs 3 decimal digits only... right?

Edited:
"grieblm" gave a good answer if you replace:

x = binary number with x = dip_read
and
digits = 5 with digits = 3
 
Last edited:

HI what is IN1-8 ? is this my array or pins of PIC ?

I think it is equivalent to
dip_read = PORTB ;

Added:
I am curious to know what could be the missing point to you in this conversion.
I wished to give you the code, but I write my programs in assembly only and for Atmel C51 MCUs.

Added:
You start with just one byte (that is a number in 8-bit binary, from 0 to 255)... right?
This byte will be converted to 3 bytes (or 1 byte + a nibble or half byte) to hold 3 decimal digits. A decimal digit needs 4 bits.
 
Last edited:

Dim An_Integer as Integer ' First you declare the variable for your integer

An_Integer = Integer(PORTB) ' This takes the values of all 8 pins on portb and converts them into an integer

Think and improve further
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top