Printing integers in PIC/MPLAB

Discussion in 'Embedded Systems and Microcontrollers' started by Brownout, May 12, 2013.

  1. Brownout

    Thread Starter Well-Known Member

    Jan 10, 2012
    2,375
    998
    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.

    Code ( (Unknown Language)):
    1.  
    2. void WriteStringXldc(char *wstring) {
    3.   int slength, indx;
    4.   char teststr[8];
    5.   slength = strlen(wstring);
    6.   for(indx = 0; indx < slength; indx++) {
    7.     WriteDataXLCD_local(wstring[indx]);
    8.     while (BusyXLCD() );
    9.   }
    10.   sprintf(teststr, "%d", indx);
    11.   slength = strlen(teststr);
    12.   for(indx = 0; indx < slength; indx++) {
    13.     WriteDataXLCD_local(teststr[indx]);
    14.     while (BusyXLCD() );
    15.   }
    16.   return;
    17.  
    18.  
    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: May 12, 2013
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  3. Brownout

    Thread Starter Well-Known Member

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

    They are just creating a string with number chars. I can already do that.

     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,420
    3,355
    Code ( (Unknown Language)):
    1. sprintf(teststr, "%d", indx);
    I think you didn't mean to put indx here.
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  6. Brownout

    Thread Starter Well-Known Member

    Jan 10, 2012
    2,375
    998
    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.
     
  7. Brownout

    Thread Starter Well-Known Member

    Jan 10, 2012
    2,375
    998
    Well, somb*ch. That worked!
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,420
    3,355
    Here is a simple way of doing it:

    Code ( (Unknown Language)):
    1.  
    2. void WriteStringXldc(int value)
    3. {
    4.   char s[8];
    5.   char *p;
    6.   p = s;
    7.   sprintf(s, "%d", value);
    8.   while (*p)
    9.     {
    10.       WriteDataXLCD_local(*p++);
    11.       while (BusyXLCD() );
    12.     }
    13. }
    14.  
     
    ErnieM likes this.
  9. Brownout

    Thread Starter Well-Known Member

    Jan 10, 2012
    2,375
    998
    Thanks Mr Chips. I'll try that. So far, sprintf hasn't produced any pritable strings for me.
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  11. Brownout

    Thread Starter Well-Known Member

    Jan 10, 2012
    2,375
    998
    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.
     
  12. GopherT

    AAC Fanatic!

    Nov 23, 2012
    5,986
    3,730
    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).
     
Loading...