Printing integers in PIC/MPLAB

Thread Starter

Brownout

Joined Jan 10, 2012
2,390
I can send char's to my LCD with no problem. However, sending integers is a different story. Here is the code for my routine that accepts strings and prints them. As a test for printing characters, I attempt to print the length of the previously printed string (in the 2nd 'for' loop). But I get nothing. Seems like this should have worked.

If you're going to answer "Read the manual", don't bother entering a response.

Rich (BB code):
void WriteStringXldc(char *wstring) {
  int slength, indx;
  char teststr[8];
  slength = strlen(wstring);
  for(indx = 0; indx < slength; indx++) {
    WriteDataXLCD_local(wstring[indx]);
    while (BusyXLCD() );
  }
  sprintf(teststr, "%d", indx);
  slength = strlen(teststr);
  for(indx = 0; indx < slength; indx++) {
    WriteDataXLCD_local(teststr[indx]);
    while (BusyXLCD() );
  }
  return;
To test the code, I send "Hello World" to get printed. The length should be 10, and should get printed, but all I get is the string sent to the function.
 
Last edited:

t06afre

Joined May 11, 2009
5,934
Which compiler do you use? If it is the HI-Tech C compiler. They have an good example on that in the LCDdemo folder. However in the XC8 compiler they removed all the examples for some reason. At least in the version I have installed on my computer
 

Attachments

Thread Starter

Brownout

Joined Jan 10, 2012
2,390
I use the C18 compiler. Thing about the example is, they aren't really converting ints into chars.


lcd_puts(
"12345678");
They are just creating a string with number chars. I can already do that.

 

t06afre

Joined May 11, 2009
5,934
Ah now I see. I got confused here. Does your compiler have the integer-string conversion function itoa()? That is the one I use. As it is included in my compiler
 

Thread Starter

Brownout

Joined Jan 10, 2012
2,390
Rich (BB code):
sprintf(teststr, "%d", indx);
I think you didn't mean to put indx here.
I am attempting to convert the int "indx" to a string to be printed. In this example, indx =11, as the string was "Hello World". This is only a test to try to convert integers to printable strings.
 

MrChips

Joined Oct 2, 2009
30,824
Here is a simple way of doing it:

Rich (BB code):
void WriteStringXldc(int value)
{
  char s[8];
  char *p;
  p = s;
  sprintf(s, "%d", value);
  while (*p)
    {
      WriteDataXLCD_local(*p++);
      while (BusyXLCD() );
    }
}
 

ErnieM

Joined Apr 24, 2011
8,377
I try to avoid any of the standard C libraries for number to text conversion after a bad time with printf() bloating my code by some 4K bytes (due to their ability to convert from most anything to most anything)

Instead see if your compiler has itoa() in it's stdlib dot h. It's an integer to ascii converter one trick pony, a very lightweight function.

If your compiler doesn't have it (it's not an official function of the C standard) Google up a copy.
 

Thread Starter

Brownout

Joined Jan 10, 2012
2,390
My prog has been working since t06afre suggested that in post #5. Thanks for the discussion, though. I didn't know that about how much space is wasted with standard libs.
 

GopherT

Joined Nov 23, 2012
8,009
LCDs take ASCII values for each character. Since the ASCII value for zero is 48(decimal), and ASCII value for each higher numeral is in sequence up to 9, you can just add 48 to your integer digit to get the ASCII value (char type).
 
Top