INT to ASCII for LCD

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Me being the simple minded person I am. There must be a simple/easy way to convert an integer into ASCII to be sent to a LCD. Would you share this knowledge with yours truly.

Thanks,
Mike

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi,

if you send 1 (int) to the LCD, simply add 48 an you get a 1

INT HEX SIGN

48 30 0
49 31 1
50 32 2

etc ......

Is it that what you want ?

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I appreciate the response but..

In a C program I need to convert integers like 1234 to ASCII, ready to be sent to an LCD. On a PC it is done like this.

i=1234;
printf("The number is %d\n", i);

How is it done using an STK500 with a 8515?

Mike

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If I understand your question correctly, its a simple matter of using the sprintf() standard I/O library function. Change your code sample to:

#include // This was probably already in your program

char LCDBuffer[16]; // The number is the maximum possible length of the output string

i = 1234;
sprintf(LCDBuffer,"The number is %d\n", i);

/* The text string is now in LCDBuffer[], ready to send to the LCD with whatever routine
you have for that purpose */

Note that the above assumes regular ANSI C. GIven the harvard architecture of the AVR, your compiler may require some type casting or use an alternate form of the library function in order to deal with the fact that one of the strings passed will be in RAM, and the other in FLASH.

Erik

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sprintf() is very useful but also quite large. If you have no other requirement for the sprintf() library function you might consider a function along the lines of 'f()' in the sampe below.

Replace the call to 'putchar()' with what ever function you use to emit a character at the current cursor position on your LCD. Also move the cursor backwards after each character is printed. putchar('\b') may work here.

// test.c

#include

void f(int n)
{
int minus = (n < 0);
if (minus) n = 0 - n;
while(n)
{
int r = n % 10;
r += '0';
putchar(r);
n /= 10;
}
if (minus) putchar('-');
putchar('\n');
}

void main()
{
f(123);
f(-456);
}

admin's test signature