Skip to main content
antonius
Associate III
January 2, 2014
Question

How to convert integer to string?

  • January 2, 2014
  • 31 replies
  • 8702 views
Posted on January 02, 2014 at 11:47

struct tm time;

   char year[50];

Guys,

I want to display year into LCD screen but I got 00 on LCD, any clues on how to do it ?

thanks

 printf(''Time: %d-%d-%d   %02d:%02d:%02d \r\n'', time.tm_year, \

                   time.tm_mon+1, time.tm_mday,\

                   time.tm_hour, time.tm_min, time.tm_sec);

    sprintf(year,''%.2f'',time.tm_year);

 

    lcd_string(year);

#!complicated
    This topic has been closed for replies.

    31 replies

    antonius
    antoniusAuthor
    Associate III
    January 3, 2014
    Posted on January 03, 2014 at 13:28

    would it be ?

    sprintf(year, ''%02d'', (time.tm_year + 1900) % 100);

         lcd_string(year);

    I got warning :

     warning:  #167-D: argument of type ''unsigned char *'' is incompatible with parameter of type ''char *restrict''

    antonius
    antoniusAuthor
    Associate III
    January 3, 2014
    Posted on January 03, 2014 at 13:32

    I tried to run it, it's freezed in the middle, any suggestions ??

    chen
    Associate II
    January 3, 2014
    Posted on January 03, 2014 at 13:53

    Hi again

    ''I tried to run it, it's freezed in the middle, any suggestions ??''

    Can you tell us what you do see?

    What happens when you step through the code, where does it stop?

    You did not answer my question :

    Where is the printf going to ?

    (Embedded systems do not normally have a console - so where is the printf directing to?)

    What happens in the lcd_string() when the char is not an ASCII char?

    Tesla DeLorean
    Guru
    January 3, 2014
    Posted on January 03, 2014 at 14:13

    I tried to run it, it's freezed in the middle, any suggestions ??

    You'll need to use a debugger, and be more specific.

    The utility of the time/year values would depend on them being correctly set and read from the RTC.

    printf() would require some kind of semi-hosting in Keil to push the data out a Serial or SWV connection. Absent that you'll likely end up in the Hard Fault routine, as the result of an BKPT/SWI instruction trying to call some support functions.

    You could implement a better Hard Fault handler, but again this would require some output device/mechanism.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    antonius
    antoniusAuthor
    Associate III
    January 3, 2014
    Posted on January 03, 2014 at 14:59

    printf is going to print a text on terminal (USART)

    antonius
    antoniusAuthor
    Associate III
    January 3, 2014
    Posted on January 03, 2014 at 15:01

    any suggestion on how to debug it ? I'm not familiar with keil...

    chen
    Associate II
    January 3, 2014
    Posted on January 03, 2014 at 15:13

    Why did you choose Keil then??

    Go to the line you want to stop on,

    Debug menu - insert breakpoint (or just press F9)

    Then run (F5). When the line of code is reached - the debugger should stop and wait for your next operation.

    You can single step (step into)

    or step over

    Step out

    or Run

    trevor23
    Associate III
    January 3, 2014
    Posted on January 03, 2014 at 17:00

    Is printf working as you expect? i.e. if you comment out the call to lcd_string does it work as expected? It's not obvious to me where your problem lies i.e. printf or lcd_string. It is always a good ides to cut a problem down to it's simplest form and be as specific as possible when describing the issue.

    And good advise above re single stepping to where the problem lies.
    antonius
    antoniusAuthor
    Associate III
    January 4, 2014
    Posted on January 04, 2014 at 13:45

    printf is working fine, lcd_string can not read the variable.....

    code :

    void Time_Display(void)

    {

       struct tm time;

       unsigned char year[50];

       time = Time_GetCalendarTime();

       printf(''Time: %d-%d-%d   %02d:%02d:%02d \r\n'', time.tm_year, \

                       time.tm_mon+1, time.tm_mday,\

                       time.tm_hour, time.tm_min, time.tm_sec);

         //sprintf(year, ''%02d'', (time.tm_year + 1900) % 100);

    //    lcd_string(time.tm_year);

    }

    Tesla DeLorean
    Guru
    January 4, 2014
    Posted on January 04, 2014 at 15:59

    Then you'll need to review the lcd_string() code, but it's clearly not going to accept an integer as a parameter if it is expecting a string.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..