cancel
Showing results for 
Search instead for 
Did you mean: 

Save my RTC

hervett
Associate II
Posted on January 14, 2009 at 13:25

Save my RTC

11 REPLIES 11
jj
Associate II
Posted on May 17, 2011 at 12:59

Quote:

I'M SO STUPIDD !!

You are NOT alone! And anyone working with ARM probably is not...

Thanks to you and to relaxe - saves many time/effort. (by the way - a calm ''relaxe'' is much appreciated...) 136 years somewhat exceeds the ''normal'' 30 days/30 feet warranty of some...

[ This message was edited by: jj.sprague on 14-01-2009 17:44 ]

relaxe
Associate II
Posted on May 17, 2011 at 12:59

Instead of building a whole calendar, try the code below. Just include time.h

And look at the doc here:

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

Will probably prevent you from reinventing the wheel...

-Relaxe.

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

* Function Name : Time_Regulate

* Description : Returns the time entered by user, using Hyperterminal.

* Input : None

* Output : None

* return : Current time RTC counter value

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

u32 Time_Regulate(void)

{

struct tm t;

time_t t_of_day;

printf(''\r\n==============Time Settings====================================='');

printf(''\nYear:'');

t.tm_year = USART_Scanf(2038)-1900;

printf(''\nMonth:'');

t.tm_mon = USART_Scanf(12)-1; //Number of months since January. [0-11] Go figure.

printf(''\nDay:'');

t.tm_mday = USART_Scanf(31);

printf(''\nHour:'');

t.tm_hour = USART_Scanf(24);

printf(''\nMinutes:'');

t.tm_min = USART_Scanf(60);

printf(''\nSeconds:'');

t.tm_sec = USART_Scanf(60);

t.tm_isdst = 0;

t_of_day = mktime(&t);

printf(''\n'');

printf(ctime(&t_of_day));

/* Return the value to store in RTC counter register */

return(t_of_day);

}

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

* Function Name : USART_Scanf

* Description : Gets numeric values from the hyperterminal.

* Input : None

* Output : None

* Return : None

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

u32 USART_Scanf(u32 value)

{

u32 index = 0;

u32 tmp[10] = {0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30};

u32 indexlength = 0;

u32 valuetmp;

//Calculate the length (in char) max of user input

valuetmp = value;

if(value)

{

indexlength++;

}

for(valuetmp = value; valuetmp > 9; ++indexlength)

{

valuetmp = (valuetmp / 10);

}

index = 10-indexlength;

while (index != 10)

{

/* Loop until RXNE = 1 */

while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)

{}

tmp[index++] = (USART_ReceiveData(USART1));

if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39))

{

printf(''\n\rPlease enter valid number between 0 and 9'');

index--;

}

else

{

USART_SendData(USART1, (u8) tmp[index-1]);

while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)

{}

}

}

/* Calculate the Corresponding value */

index = (tmp[9] - 0x30) +

((tmp[8] - 0x30) * 10) +

((tmp[7] - 0x30) * 100) +

((tmp[6] - 0x30) * 1000) +

((tmp[5] - 0x30) * 10000) +

((tmp[4] - 0x30) * 100000) +

((tmp[3] - 0x30) * 1000000) +

((tmp[2] - 0x30) * 10000000) +

((tmp[1] - 0x30) * 100000000) +

((tmp[0] - 0x30) * 1000000000);

/* Checks */

if (index > value)

{

printf(''\n\rPlease enter valid number between 0 and %d'', value);

return 0xFFFFFFFF;

}

return index;

}

[ This message was edited by: relaxe on 14-01-2009 18:27 ]