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
trevor23
Associate III
Posted on January 13, 2014 at 13:15

@ h.rick The ironic thing is that you don't need to set the weekday, it will be worked out (by gmtime) as per my example above. But you seem intent on doing your own thing. Must soon be time to start a new thread?

antonius
Senior
Posted on January 13, 2014 at 14:44

gmtime ==> not changing anything, it's still the same...

trevor23
Associate III
Posted on January 13, 2014 at 15:28

show the code you have used

antonius
Senior
Posted on January 13, 2014 at 16:04

Here's the code

before the loop on main :

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

.

.

.

.

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];

 char *weekday_label[]={''Sun'',''Mon'',''Tue'',''Wed'',''Thr'',''Fri'',''Sat''};

 char *month_label[]={''January'',''Feb'',''Mar'',''Apr'',''May'',''Jun'',''Jul'',''Aug'',''Sep'',''Oct'',''Nov'',''Dec''};

  struct tm *utcTimeSnapshot;

  time_t Tmp;

 while(1)

  {

     /* Load the Counter value */

     Tmp = RTC_GetCounter();

    

     utcTimeSnapshot = localtime(&Tmp);

     //utcTimeSnapshot = gmtime(&Tmp);

     /* Compute day */

    // WEEKDAY = ((Tmp / 86400)%7)+1;

    WEEKDAY = utcTimeSnapshot->tm_wday;

    

    

     /* Compute date */

     //DATE = ((Tmp / 31556926)%365)-40;

     // DATE = ((Tmp / 86400)%365)-41;

    DATE = utcTimeSnapshot->tm_mday;

    

    /* Compute month */

     //MONTH = ((Tmp / 2629743)%12);

    MONTH = utcTimeSnapshot->tm_mon;

    

    

    

     /* Compute year */

    //YEAR = (Tmp / 31556926)+1970;

    YEAR = (utcTimeSnapshot->tm_year)+1900;

    

    

     /* Compute hours */

    // THH = ( Tmp / 3600 ) % 24;

    THH = utcTimeSnapshot->tm_hour;

    

     /* Compute minutes */

     //TMM = ( Tmp / 60 ) % 60;

    TMM = utcTimeSnapshot->tm_min;

    

     /* Compute seconds */

     //TSS = Tmp % 60;

    TSS = utcTimeSnapshot->tm_sec;

    

           //lcd_cmd(LCD_CLEAR);

          sprintf(day,''%02d'',WEEKDAY);

          sprintf(date,''%02d'',DATE);

          sprintf(month,''%02d'',MONTH);

          sprintf(year,''%02d'',YEAR);

          sprintf(hour,''%02d'',THH);  //convert integer to string

          sprintf(min,''%02d'',TMM);  //convert integer to string

          sprintf(sec,''%02d'',TSS);  //convert integer to string

          //lcd_string(''HOUR:MIN:SEC'');

          lcd_xy(0,0);

          lcd_string(weekday_label[WEEKDAY]);lcd_string('','');

          lcd_string(date);lcd_string(''-'');

          lcd_string(month_label[MONTH]);

          lcd_string(''-'');lcd_string(year);

          lcd_xy(1,0);

trevor23
Associate III
Posted on January 13, 2014 at 16:22

You are not using RTC_SetCounter anywhere so the time can never be correct. Hence the following in my example above.

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.tm_sec = 30; // seconds after the minute 0 - 61 (60 and 61 only used for leap seconds))
time_struct.tm_min = 20; // minutes after the hour 0 - 59
time_struct.tm_hour = 15; // hours since midnight 0 - 23
time_struct.tm_mday = 12; // day of the month 1 - 31
time_struct.tm_mon = 0; // months since January 0 - 11
time_struct.tm_year = 2014 - 1900; // years since 1900
time_struct.tm_isdst = 0; // Daylight Saving Time flag
set_time(&time_struct); // use the struct to set the RTC seconds counter value

Or how about just getting the RTC counting seconds as a start. Set the RTC and observe it increment by one every second. That would be a good start.

Posted on January 13, 2014 at 16:27

My head just hurts....

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 13, 2014 at 23:29 how about weekday ( w_day ) ?

Setup the time to Sunday,12-Jan-2014 15:20:30

antonius
Senior
Posted on January 13, 2014 at 23:43

I tested just now,

I put init_RTCTime(), before the main loop and the system stopped working, the code :

/*******************************************************************************

*

* RTC_SetTime

*

*******************************************************************************/

/******************************************************************************/

static void set_time(struct tm *time_struct_ptr)

{

    time_t seconds_from_epoch = mktime(time_struct_ptr);

    RTC_SetCounter(seconds_from_epoch);

}

init_RTCTime()

{

    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.tm_sec = 30; // seconds after the minute 0 - 61 (60 and 61 only used for leap seconds))

    time_struct.tm_min = 20; // minutes after the hour 0 - 59

    time_struct.tm_hour = 15; // hours since midnight 0 - 23

    time_struct.tm_mday = 14; // day of the month 1 - 31

    time_struct.tm_mon = 0; // months since January 0 - 11

    time_struct.tm_year = 2014 - 1900; // years since 1900

    time_struct.tm_isdst = 0; // Daylight Saving Time flag

    time_struct.tm_wday = 2;//Tuesday  

    set_time(&time_struct); // use the struct to set the RTC seconds counter value

}

trevor23
Associate III
Posted on January 14, 2014 at 00:32

As previously stated it's pointless setting the weekday as gmtime will calculate it from the RTC seconds count when you need it  (if the time library is working correctly) but don't take my word for it, google gmtime. And mktime does not use it to calculate the seconds count (seconds from epoch).

I can't help you with just ''the system stopped working'' to go on. You could at least say at what line it has stopped. 

h.rick I wish you luck and hope you find a solution but I'm going to bow out of this one now, I've feel I've done all I can here.

Regards

Trevor
antonius
Senior
Posted on January 14, 2014 at 08:36

How do you reckon of this article ?

http://www.cplusplus.com/reference/ctime/mktime/

Is it the right way for displaying weekday ?

thanks