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
antonius
Senior
Posted on January 14, 2014 at 11:52

I used mktime, but it's not yet displaying the proper weekday, I don't understand ?

code :

 uint32_t THH,TMM,TSS,WEEKDAY,DATE,MONTH,YEAR;

 unsigned char date[20],day[30],month[20],year[20],hour[20],min[20],sec[32];

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

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

  struct tm *utcTimeSnapshot = NULL;

  time_t Tmp;

 /* Load the Counter value */

 Tmp = RTC_GetCounter();

 utcTimeSnapshot = localtime(&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)-1;

 

 /* 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;

mktime(utcTimeSnapshot);

        // printf(''GET TIME'');

      //lcd_cmd(LCD_CLEAR);

      sprintf(day,''%.2d'',WEEKDAY);

      sprintf(date,''%.2d'',DATE);

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

      sprintf(year,''%.2d'',YEAR);

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

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

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

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

      lcd_xy(0,0);

      if (utcTimeSnapshot && (utcTimeSnapshot->tm_wday >= 0) && (utcTimeSnapshot->tm_wday <= 6))

        {

          lcd_string(weekday_label[utcTimeSnapshot->tm_wday]);lcd_string('','');

           printf (''That day is a %s.\n'', weekday_label[utcTimeSnapshot->tm_wday]);

        }

        else

          lcd_string(''???,'');

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

      //lcd_string(day);

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

      lcd_string(month_label[MONTH]);

      //lcd_string(month);

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

      lcd_xy(1,0);

      

      

      lcd_cmd(SECOND_ROW);

      lcd_string(hour);lcd_string('':'');

      lcd_string(min);lcd_string('':'');

      lcd_string(sec);

     

}

antonius
Senior
Posted on January 16, 2014 at 14:18

RTC_SetCounter(seconds_from_epoch);

===> doesn't work for me...
antonius
Senior
Posted on January 18, 2014 at 04:58

Guys, where can I find time.c on Keil directory structure ? I can find time.h but time.c can not be found ? any clues ?

thanks

Posted on January 18, 2014 at 06:04

Likely you do not have source, but only a library in object form.

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 18, 2014 at 23:16

where can I find the source ?

is it related :

http://www.keil.com/support/man/docs/armlib/armlib_Chdbbgcd.htm

The source for the Rogue Wave Standard C++ Library is not freely distributable. It can be obtained from Rogue Wave Software Inc., or through ARM, for an additional license fee.

thanks

antonius
Senior
Posted on January 30, 2014 at 11:41

How can I adjust the year with button and then save it into RTC register ?

any ideas ? thanks

1.
time
.tm_year=YEAR++;
2.
Time_SetCalendarTime(
time
);
3.
lcd_string(year);

trevor23
Associate III
Posted on January 30, 2014 at 12:50

As per my example(s) above. Manipulate the time structure to what you want and then call set_time which uses the time struct to set the RTC time in seconds from epoch. So iftime_struct is setup already as you need (parameters filled in as per my example) then to increment the year by one and set the new time all you would do is:

time_struct.tm_year++;
set_time(&time_struct);

antonius
Senior
Posted on February 17, 2014 at 04:32

When I pushed the button, it didn't increase the year, any comments ?

1.
if
(button1)
2.
{
3.
time_struct.tm_year++;
4.
lcd_string(year); 
5.
}

trevor23
Associate III
Posted on February 17, 2014 at 14:09

You are displaying ''year'' and you didn't increment ''year''.

You do realise that you are not changing the RTC counter here?
Posted on February 17, 2014 at 14:24

Observe there is no relationship established in your code between time_struct.tm_year and year

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