cancel
Showing results for 
Search instead for 
Did you mean: 

can not display sprintf to LCD ?

antonius
Senior
Posted on January 11, 2014 at 22:28

Guys,

Why suddenly sprintf doesn't work ?

It was working yesterday, I shut down the power supply now, it's not giving me a good response

code :

uint32_t /*Tmp,*/THH,TMM,TSS,WEEKDAY,DATE,MONTH,YEAR;

 unsigned char date[10],day[10],month[10],year[10],hour[10],min[10],sec[10];

  struct tm *utcTimeSnapshot;

 Tmp = RTC_GetCounter();

TSS = utcTimeSnapshot->tm_sec;

sprintf(sec,''%02d'',TSS); 

lcd_string(sec);

If I commented out sprintf, now it's running properly, I can see RTC on USART, but if I use sprintf, it's running 2 seconds and freeze......any ideas ?

Thanks

#learn-to-code
48 REPLIES 48
Posted on January 12, 2014 at 13:33

memset(&foo, 0, sizeof(foo));

What value is in WEEKDAY? You really need to qualify the pointer isn't NULL, and that the content is within range.

char *weekday_label[]={''Sun'',''Mon'',''Tue'',''Wed'',''Thr'',''Fri'',''Sat''};
if (utcTimeSnapshot && (utcTimeSnapshot->tm_wday >= 0) && (utcTimeSnapshot->tm_wday <= 6))
{
lcd_string(weekday_label[utcTimeSnapshot->tm_wday]);lcd_string('','');
}
else
lcd_string(''???,'');
WEEKDAY = utcTimeSnapshot->tm_wday
sprintf(day,''%.2d'',WEEKDAY);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
antonius
Senior
Posted on January 12, 2014 at 14:08

I followed your suggestion, but I got weekday = Wed, it's supposed to be ''Sun'', anything I missed here ?

Thanks

antonius
Senior
Posted on January 12, 2014 at 14:09

Thanks, I've put it on NULL

Posted on January 12, 2014 at 14:24

I followed your suggestion, but I got weekday = Wed, it's supposed to be ''Sun'', anything I missed here ?

What is the value in WEEKDAY?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
antonius
Senior
Posted on January 12, 2014 at 15:33

the value = 3

Posted on January 12, 2014 at 16:20

Then it really should not be surprising that you get ''Wed'', right?

You need to look at why the values are initialized as they are. The library function that created the values is either doing it's job correctly, or it is not, but the problem here is NOT with sprintf() or the LCD functions. You need to look at the cause, not the symptom.

Consider using a PC based compiler, and do some experiments with the time and time conversion functions, and become familiar with how they should work, and some test values to demonstrate the conversion is functioning correctly.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
trevor23
Associate III
Posted on January 12, 2014 at 17:00

@h.rickThe problem is thatutcTimeSnapshot is pointing at random cr*p because you have not set it to point at anything therefore whether things work, don't work or crash is random. I already provided you with an example for this in one of your (many) related threads on the same problem. Here is an even fuller example with setup of RTC counter value included.

#include <
time.h
>
static void set_time(struct tm *time_struct_ptr)
{
time_t seconds_from_epoch = mktime(time_struct_ptr);
RTC_SetCounter(seconds_from_epoch);
}
struct tm time_struct;
struct tm *system_time_struct_ptr;
time_t number_of_seconds_from_epoch;
// Setup the time to 12-Jan-2014 15:20:30
time_struct.sec = 30; // seconds after the minute 0 - 61 (60 and 61 only used for leap seconds))
time_struct.min = 20; // minutes after the hour 0 - 59
time_struct.hour = 15; // hours since midnight 0 - 23
time_struct.mday = 12; // day of the month 1 - 31
time_struct.mon = 0; // months since January 0 - 11
time_struct.year = 2014 - 1900; // years since 1900
time_struct.isdst = 0; // Daylight Saving Time flag
set_time(&time_struct); // use the struct to set the RTC seconds counter value
// wait a few seconds here before reading the time back to see it increase
number_of_seconds_from_epoch = RTC_GetCounter(); // read time from RTC seconds counter
system_time_struct_ptr = gmtime( &number_of_seconds_from_epoch );
if (system_time_struct_ptr)
{
char buffer[51];
printf(buffer, ''date = %d/%d/%d'', system_time_struct_ptr->mday, system_time_struct_ptr-> mon + 1, system_time_struct_ptr->year % 100);
printf(buffer, ''time = %d/%d/%d'', system_time_struct_ptr->hour , system_time_struct_ptr->min , system_time_struct_ptr->sec);
}
else
{
// error
}

Note: I don't like the use of gmtime (or localtime) because it is not reentrant (not thread safe) as it returns a pointer to a global structure. Instead gmtime_r (or localtime_r) or equivalent should be used but I'm trying to keep it simple for you for now - lets solve that one later.
antonius
Senior
Posted on January 12, 2014 at 22:47

weekday=3, I reckon, it's because, I haven't set it properly on the first time I setup RTC, I'll have a look and return to you guys, anyway, thanks for the suggestions, opening my vision...

antonius
Senior
Posted on January 13, 2014 at 12:35

Why can't I set the weekday ?

The code :

  while (time.tm_wday >6 || time.tm_sec <0 )

  {

    time.tm_wday = USART_Scanf(1,6,2);

  }

  printf(''Set Weekday:  %d\r\n'', time.tm_wday);

it's not waiting for me inputting the wekday....??

chen
Associate II
Posted on January 13, 2014 at 12:48

Hi

''Why can't I set the weekday ?''

''it's not waiting for me inputting the wekday....??''

Have you debugged ''USART_Scanf()'' ?

What is it doing?

That will answer ''why it is not waiting?''.