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.

Decimal to ASCII conversion progam in C

Status
Not open for further replies.

thannara123

Advanced Member level 5
Joined
Jan 7, 2010
Messages
1,580
Helped
122
Reputation
244
Reaction score
114
Trophy points
1,353
Location
India
Activity points
10,388
Hello Experts <
I want a C program decimal to ASCII conversion (8051) .
or Binary to ASCII ?
thanks in advance >?
 

Code:
        int hundreds, tens, ones;
	unsigned char buffer[16];
	ones = adc_data%10;
	adc_data = adc_data/10;
	tens = adc_data%10;
	hundreds = adc_data/10;
//Now convert this into ASCII by ORing it with 0x30
ones = ones | 0x30;
tens = tens | 0x30;
hundreds = hundreds | 0x30;

This is a Small Sample Code which Converts the ADC Value in to the ASCII..

Hope u understand
 
temp = from adc

a[0] = temp/100;
temp = temp%100;
a[1] = temp/10;
a[2] = temp%10;
for(i=0;i<3; i++)
{
bcdNum = a;
t = 0x30 + BCDToDecimal(bcdNum);
writedat(t);
}

uint8 BCDToDecimal (uint8 bcdByte)
{
return (((bcdByte & 0xF0) >> 4) * 10) + (bcdByte & 0x0F);
}

here only displaying corresponding decimal value only not ascii please help me
 

I don't understand ur code...
What is this adc means its type ...

And you code contains library functions, what they do i don't know...
give me your variable and its value any guess that is contains
i will write a code to convert it into ascii..
 

i am trying to convert the binary data form adc 0804 to asci value to display
 

okay let me give you a simple way to so some thing...

Code:
#include<stdio.h>

//create a string variable 
unsigned char buffer[16];

sprintf(buffer,"%d",temp);
//This will convert the temp value to ASCII and these values are stored in buffer..
//don't forget to inculde stdio.h header file.

Hope this works

---------- Post added at 23:03 ---------- Previous post was at 22:57 ----------

i am trying to convert the binary data form adc 0804 to asci value to display

Okay Lets me share u a link...
having Similar example..

ADC Interfacing - MATLAB ACADEMY

Code:
#include <REGX51.H>
#include<stdio.h>
#include<string.h>

sbit INTR = P3^5;
sbit RD_ADC = P2^4;
sbit WR_ADC = P2^3;
sfr MYDATA = 0x80;        //Port-0
sfr LcdData = 0x90;        //Port-1
sbit RS =  P2^5;
sbit RW =  P2^6;
sbit EN =  P2^7;
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void lcdstr(unsigned char msg[]);
void Delay(unsigned int itime);
void SerTX(unsigned char x);
void SerTX_str(unsigned char msg[]);
unsigned char msg[] = "VOLTAGE --->";
void main()
{
    unsigned int temp;
    int hundreds,tens,ones;
    unsigned char buffer[10];
    TMOD = 0x20;    //Timer-1, 8-Bit Auto Reload Mode
    TH1 = 0xFD;        //9600 Baud Rate When Crystal Used is 11.0592MHZ
    SCON = 0x50;
    TR1 = 1;        //Start Timer
    MYDATA = 0xFF;    //Making P1 as Input Port
    INTR = 1;
    RD_ADC = 1;        //Set RD high
    WR_ADC = 1;        //Set WR high
    lcdcmd(0x38);
    Delay(1);
    lcdcmd(0x0E);
    Delay(1);
    lcdcmd(0x01);
    Delay(1);
    lcdcmd(0x80);
    lcdstr("VOLTAGE");
    lcdcmd(0x0C);
    while(1)
    {     
        lcdcmd(0xC0);                                                               
        WR_ADC = 0;        //Send WR Pulse
        Delay(1);
        WR_ADC = 1;        //Low-High Pulse means Start of Conversion
        while(INTR == 1);    //Wait until End of Conversion
        //When Conversion Gets Completed the INTR Pin Goes Low we get out of the Loop
        RD_ADC = 0;        //Send RD Pulse
        temp = MYDATA * 2;
        //Conversion Process Starts Here       
            ones = temp%10;
            temp = temp/10;
            tens = temp%10;
            hundreds = temp/10;
            sprintf(buffer,"%d.%d%d Volts",hundreds,tens,ones);
            lcdstr(buffer);
            SerTX_str(msg);
            SerTX_str(buffer);
            SerTX(13);
            SerTX(13);
            Delay(10);
            Delay(10);
            RD_ADC = 1;
    }
}
void lcdcmd(unsigned char value)
{
    LcdData = value;
    RS = 0;
    RW = 0;
    EN = 1;
    Delay(1);
    EN = 0;
}
void lcddata(unsigned char value)
{
    LcdData = value;
    RS = 1;
    RW = 0;
    EN = 1;
    Delay(1);
    EN = 0;
}
void Delay(unsigned int itime)
{
    unsigned int i,j;
    for(i=0;i<1275;i++)
    for(j=0;j<itime;j++);
}
void lcdstr(unsigned char msg[])
{
    unsigned short int len,i;
    len = strlen(msg);
    for(i=0;i<len;i++)
    {
        lcddata(msg[i]);
        Delay(1);
    }
}
void SerTX(unsigned char x)
{
    SBUF = x;
    while(TI == 0);
    TI = 0;
}
void SerTX_str(unsigned char msg[])
{
    unsigned short len,i;
    len = strlen(msg);
    for(i=0;i<len;i++)
    {   
        SerTX(msg[i]);
    }
}


---------- Post added at 23:05 ---------- Previous post was at 23:03 ----------

That site is very good just have a look on that...
and they are also using ADC0804 i think u will get some idea..

Hope this helps you..
 
  • Like
Reactions: thannara123

    thannara123

    Points: 2
    Helpful Answer Positive Rating
    V

    Points: 2
    Helpful Answer Positive Rating
temp = from adc

a[0] = temp/100;
temp = temp%100;
a[1] = temp/10;
a[2] = temp%10;
for(i=0;i<3; i++)
{
bcdNum = a;
t = 0x30 + BCDToDecimal(bcdNum);
writedat(t);
}

uint8 BCDToDecimal (uint8 bcdByte)
{
return (((bcdByte & 0xF0) >> 4) * 10) + (bcdByte & 0x0F);
}

here only displaying corresponding decimal value only not ascii please help me



I think that arunsharma0731 has already pointed out the solution. Supposing that the writedat() function works OK, then there is no need for the BCD to decimal convertion, the desired ASCII values are already there.

Code:
a[0] = temp/100;
temp = temp%100;
a[1] = temp/10;
a[2] = temp%10;
for(i=0;i<3; i++)
{
  t = 0x30 | a[i];  //it is better than addition (+)
  writedat(t);
}


Hope this solved it.
 
I think he is asking for the way to convert the ADC result to the actual measured voltage and then this voltage to the ascii representation in order to send it to LCD.
 
P2 = 0xff; // from adc 11111111

bcdNum = P2;

temp = BCDToDecimal(bcdNum);

a[0] = temp / 100;

temp = temp % 100;

a[1] = temp / 10;

a[2] = temp % 10;

for (i = 0; i < 3; i++) {

t = 0x30 | a[i]; //it is better than addition (+)

writedat(t);

}


When i put 0x00 to P2 from adc is ok dispalying zero ,
if the value is 13 then the display 13 no problem
when it comes hex values like (a,b,c,d,e,f) ie 0xa1,0xff etc .not showing actual value
i need to convert 0xff to display as 256
please help me thanks,

 
Last edited:

0xa1is decimal 161 so

Code:
a[0] = 161/100 = 1
a[1] = 161%100 = 61 , then 61/10 = 6
a[2] = 61%10 = 1

so 

a[0]=0x01
a[1]=0x06
a[2]=0x01

if you add 0x30 to these values then

a[0] | 0x30 =0x31 this is ascii character "1"
a[1] | 0x30 =0x36 this is ascii character "6"
a[2] | 0x30 =0x31 this is ascii character "1"

Why do you add the three array bytes into the variable t?
to show the three characters to the lcd you need to use the array and not the sum of the array bytes.
I have no idea what writedat is supposed to do but to show an array there is ususally a lcd_puts or something like that.

Alex
 
thannara123 said:
P2 = 0xff; // from adc 11111111

bcdNum = P2;

temp = BCDToDecimal(bcdNum);

a[0] = temp / 100;


Also get rid of the bcd to decimal convertion. Try this as a first step:

Code:
P2 = 0xff; // from adc 11111111 

temp = P2;

a[0] = temp / 100;

//.......................The rest of the code stays unchanged
 
The standard method provided by the C language to convert numbers to decimal formatted strings is sprintf() or itoa(). It would be my first choice for the discussed problem.
 

how can i find the functions like sprint() in header file ?
how much functions in each header files ?
how much stranded header files ?
But the working and logic can be learnt (me too low in that field)
 

sprintf is included in stdio.h,
you can check your compiler manual

itoa is in the stdlib.h
 
Last edited:
In my post(above)i have given you the code with sprintf function and in the above link it uses the header file stdio.h


But make sure that it will increase the code size...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top