LPC1769 Set date and time

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LPC1769 Set date and time

901 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by arturn on Thu May 30 09:49:13 MST 2013
Hi

I would like to sync date and time to LPC1769 board, because every timethat I disconnect board, I lost current date.

At boot time call to webserver to get current date, and set this to LPC_RTC structure with below code :
[FONT=Courier New][SIZE=2]
void SetStrTime(char * StrTime)
{
    char year[4];
    char month[2];
    char day[2];

    char minute[2];
    char hour[2];
    char second[2];

    strncpy(year,StrTime,4);
    StrTime+=5;

    strncpy(month,StrTime,2);
    StrTime+=3;

    strncpy(day,StrTime,2);
    StrTime+=3;

    strncpy(hour,StrTime,2);
    StrTime+=3;

    strncpy(minute,StrTime,2);
    StrTime+=3;

    strncpy(second,StrTime,2);
    StrTime+=3;


    LPC_RTC->YEAR = atoi(year);
    LPC_RTC->MONTH = atoi(month)+1;
    LPC_RTC->DOM = atoi(day) +1;
    LPC_RTC->HOUR = atoi(hour);
    LPC_RTC->MIN = atoi(minute);
    LPC_RTC->SEC = atoi(second);
}[/SIZE][/FONT]

This code runs well, but board "hangs out" after call...

Should I reboot board after set date?
Can date change affect to running tasks ? I set at start-up there isn't other runnig tasks...

How Can I reboot board by code?


Thanks!
0 Kudos
Reply
12 Replies

787 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ToBeFrank on Fri May 31 07:15:08 MST 2013

Quote: arturn
Finally I fix '\0' bug and code runs well...



It's still wrong.

    char year[4];
    strncpy(year,StrTime,4);
    year[4]='\0';
    
0 Kudos
Reply

787 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ToBeFrank on Fri May 31 07:11:47 MST 2013

Quote: kabriolin
Something hurt me:

char year[[B]4[/B]];
    char month[[B]2[/B]];
    char day[[B]2[/B]];

char minute[[B]2[/B]];
    char hour[[B]2[/B]];
    char second[[B]2[/B]];

...
year[[B]4[/B]]='\0';
...
month[[B]2[/B]]='\0';
....



I think you meant:

char year[[B]5[/B]];
year[4]=0;
0 Kudos
Reply

787 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Fri May 31 04:36:02 MST 2013

Quote: arturn
Sorry.. I'm newby...

Where and How I should perform setup clock?

I'm completly lost... :S



UM10360 is your friend. Read it:

Quote:

27.6.2.2 Clock Control Register (CCR - 0x4002 4008)
The clock register is a 4-bit register that controls the operation of the clock divide circuit. Each bit of the clock register is described in Table 509. [COLOR=Red]All NC bits in this register should be initialized when the RTC is first turned on.[/COLOR]

Table 509. Clock Control Register (CCR - address 0x4002 4008) bit description
Bit Symbol Value Description Reset value
0 CLKEN Clock Enable.
   [COLOR=Red] 1 The time counters are enabled.[/COLOR]
    0 The time counters are disabled so that they may be initialized.

0 Kudos
Reply

787 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by arturn on Fri May 31 04:24:44 MST 2013

Quote: stalisman
I don't think he got it  :-)



I solved... what a disaster!!!!!

Now It's sems to work... !
0 Kudos
Reply

787 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by arturn on Fri May 31 04:11:43 MST 2013
Sorry.. I'm newby...

Where and How I should perform setup clock?

I'm completly lost... :S
0 Kudos
Reply

787 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by stalisman on Fri May 31 04:11:33 MST 2013

Quote: kabriolin
Something hurt me:

char year[[B]4[/B]];
    char month[[B]2[/B]];
    char day[[B]2[/B]];

char minute[[B]2[/B]];
    char hour[[B]2[/B]];
    char second[[B]2[/B]];

...
year[[B]4[/B]]='\0';
...
month[[B]2[/B]]='\0';
....





I don't think he got it  :-)
0 Kudos
Reply

787 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by R2D2 on Fri May 31 03:38:57 MST 2013

Quote: arturn
But now clock its stopped... Always return the same time.. :S



Where's your setup? Your clock doesn't know that you want to run it :)
0 Kudos
Reply

787 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by kabriolin on Fri May 31 03:30:09 MST 2013
Something hurt me:

char year[[B]4[/B]];
    char month[[B]2[/B]];
    char day[[B]2[/B]];

char minute[[B]2[/B]];
    char hour[[B]2[/B]];
    char second[[B]2[/B]];

...
year[[B]4[/B]]='\0';
...
month[[B]2[/B]]='\0';
....
0 Kudos
Reply

787 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by arturn on Fri May 31 01:57:21 MST 2013
Finally I fix '\0' bug and code runs well...

But now clock its stopped... Always return the same time.. :S

void SetStrTime(char * StrTime)
{
    char year[4];
    char month[2];
    char day[2];

    char minute[2];
    char hour[2];
    char second[2];

    strncpy(year,StrTime,4);
    year[4]='\0';
    StrTime+=5;

    strncpy(month,StrTime,2);
    month[2]='\0';
    StrTime+=3;

    strncpy(day,StrTime,2);
    day[2]='\0';
    StrTime+=3;

    strncpy(hour,StrTime,2);
    hour[2]='\0';
    StrTime+=3;

    strncpy(minute,StrTime,2);
    minute[2]='\0';
    StrTime+=3;

    strncpy(second,StrTime,2);
    second[2]='\0';
    StrTime+=3;

    LPC_RTC->YEAR = atoi(year);
    LPC_RTC->MONTH = atoi(month)+1;
    LPC_RTC->DOM = atoi(day);
    LPC_RTC->HOUR = atoi(hour);
    LPC_RTC->MIN = atoi(minute);
    LPC_RTC->SEC = atoi(second);
}


void GetStrTime(char * StrTime)
{
    STRCLEAN(StrTime);
    sprintf(StrTime,"%04i-%02i-%02iT%02i:%02i:%02i",LPC_RTC->YEAR,LPC_RTC->MONTH - 1,LPC_RTC->DOM,LPC_RTC->HOUR,LPC_RTC->MIN,LPC_RTC->SEC);
}
0 Kudos
Reply

787 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ToBeFrank on Thu May 30 13:18:49 MST 2013
You're free to waste your time looking elsewhere, but I suggest you fix this bug first. From the strncpy documentation:


Quote:
No null-character is implicitly appended at the end of [I]destination[/I] if [I]source[/I] is longer than [I]num[/I]. Thus, in this case, [I]destination[/I] shall not be considered a null terminated C string (reading it as such would overflow).

What do you think atoi is doing with your not null terminated strings?
0 Kudos
Reply

787 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by arturn on Thu May 30 13:12:29 MST 2013
I'm not sure that this is the reason...


Structure fields are setting right...
0 Kudos
Reply

787 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ToBeFrank on Thu May 30 10:30:29 MST 2013
You don't have a null at the end of your char arrays. Thus, atoi is failing.
0 Kudos
Reply