2014-01-11 01:28 PM
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-code2014-01-13 04:15 AM
@ 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?
2014-01-13 05:44 AM
gmtime ==> not changing anything, it's still the same...
2014-01-13 06:28 AM
show the code you have used
2014-01-13 07:04 AM
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);2014-01-13 07:22 AM
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.
2014-01-13 07:27 AM
My head just hurts....
2014-01-13 02:29 PM
Setup the time to Sunday,12-Jan-2014 15:20:30
2014-01-13 02:43 PM
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 }2014-01-13 03:32 PM
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.RegardsTrevor2014-01-13 11:36 PM