EmbeddedRelated.com
Forums

IAR compiler and the time.h lib

Started by "hen...@ymail.com" April 1, 2009
Can someone explain to me how I connect the RTC registers in a LPC213x
to the time structures in ansi C.
I have the RTC up and running but I would like to do some time
calculations that require the time lib.

I guess there are stubs that I have to program myself to connect the RTC
with the C library time functions.

/BR, Henrik


An Engineer's Guide to the LPC2100 Series

--- In l..., "henrikstenquist@..." wrote:
>
> Can someone explain to me how I connect the RTC registers in a LPC213x
> to the time structures in ansi C.
> I have the RTC up and running but I would like to do some time
> calculations that require the time lib.
>
> I guess there are stubs that I have to program myself to connect the RTC
> with the C library time functions.
>
> /BR, Henrik
>
>

Here is how I did it. Keep in mind that my get_time() function was
written to pack the time efficiently for the application I was
working on so you do not need to follow this exactly. It should be
a good starting point though. Also I was using Keil MDK tools not
IAR but it should be the same.

void get_time(DWORD *time, BYTE *sec)
{
DWORD temp, temp2;

temp = CTIME0;
temp2 = CTIME1;

*sec = (temp & 0x0000001F); /* Seconds */
*time = ((temp & 0x00003F00) >> 8); /* Minutes */
*time |= ((temp & 0x001F0000) >> 10); /* Hours */
*time |= ((temp2 & 0x0000001F) << 11); /* Day of month */
*time |= ((temp2 & 0x00000F00) << 8); /* Month */
*time |= ((temp2 & 0x0FFF0000) << 4); /* Year */

}

/*
******************************************************************************
* - get_event_time()
*
*
* - returns the time in seconds for the event to be logged
******************************************************************************
*/
DWORD get_event_time(DWORD *evnt_time,BYTE *evnt_sec)
{
DWORD time;
BYTE sec;
DWORD time_diff;
struct tm time1, time2;
time_t time_t1, time_t2;

get_time(&time,&sec);

time2.tm_sec = sec;
time2.tm_min = (time & 0x0000003F);
time2.tm_hour = (time & 0x000007C0) >> 6;
time2.tm_mday = (time & 0x0000F800) >> 11;
time2.tm_mon = (time & 0x000F0000) >> 16;
time2.tm_year = ((time & 0xFFF00000) >> 20) - 1900;

time1.tm_sec = *evnt_sec;
time1.tm_min = (*evnt_time & 0x0000003F);
time1.tm_hour = (*evnt_time & 0x000007C0) >> 6;
time1.tm_mday = (*evnt_time & 0x0000F800) >> 11;
time1.tm_mon = (*evnt_time & 0x000F0000) >> 16;
time1.tm_year = ((*evnt_time & 0xFFF00000) >> 20) - 1900;

time_t1 = mktime(&time1);
time_t2 = mktime(&time2);
time_diff = (DWORD)difftime(time_t2, time_t1);

return time_diff;
}
Regards,

Frank