|
|
View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
__TIME__ & __DATE__ |
Posted: Fri Jul 13, 2012 2:52 pm |
|
|
Hi,
I have a datalogger project (.... who doesnt).... and i was thinking of using
DATE and TIME to set the RTC automatically upon compile...
I'm sure its old news...
How can I load the time values to variables?
I have globals for Hour and minutes.. for example.
is there a way to do something like:
Code: | int minutes = (__TIME__, 2); |
where 2 is the minute "field"... I'm just improvising... I have no idea.
Thanks.
G- _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
jgschmidt
Joined: 03 Dec 2008 Posts: 184 Location: Gresham, OR USA
|
|
Posted: Sat Jul 14, 2012 12:54 am |
|
|
The __DATE__ and __TIME__ defines are strings so you could probably parse them for the values you want. But, what happens when you cycle power? Seems like you would reset the time in your logger. I run into this on my logger projects. I have code which initializes the (usually battery backed) RTC. I compile, power up to initialize the RTC, and then comment out the RTC initialization and reload the code.
For the projects with SD cards I put the date and time in a config file that gets deleted after it's read. _________________ Jürgen
www.jgscraft.com |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Sat Jul 14, 2012 6:25 am |
|
|
So, you are saying that:
Code: | char time_string[10]=_TIME_; |
or something like that?... ill give it a try
... yeah i know what you mean with the reset... i just want to avoid setting the clock manually...
there are ways around a reset to avoid resetting the clock...
i have a logger with only one button... the reset button... you control all parameters by resetting or not the logger...
Thanks for the info. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Sat Jul 14, 2012 7:52 am |
|
|
Some code ...not the most efficient but then again it only runs at compile time. I place the call to convert datetime and compile the external rtc DS1307 is posted after a dec to bcd conversion. The call to convert datetime is removed and recompiled ( this prevents a PIC restart from regressing back to the compile time).
EX.
#IFDEF INIT_RTC
compiledatetime_conv(); /// using the compile time date it populates RTC
/// RTC is in decimal format not BCD
write_DS1307regs(); /// takes RTC then converts to BCD and writes
#ENDIF
Code: |
struct d_t{ /// PIC internal values are integer not bcd
int8 sec;
int8 min;
int8 hr;
int8 dow;
int8 day;
int8 mth;
int8 year;
};
union {
int8 reg[7];
struct d_t datetime;
} RTC;
int8 day_of_week(int8 day,int8 mth,int8 yr)
{int8 val,tmp;
//// yr is last two digits Ex 2007 is 07
/// mth is month Jan=1.....Dec=12
/// explanation
/// 365 days is 1 mod 7 so a non leap year begins on the next day of the week
// to the prior year and another day added if a leap year
// the calc is ahead of itself during an actual leap year hence the
// adjusting down for jan and feb in a leap year
// the table is derived as follows 1/1/01 is a monday so yr=1 day=1 +x mod 7
// has to yield 1 (Monday) so table[0] must be 6 ( 6+1+1=8 and 8 mod 7 is 1)
val=yr+yr/4; /// leap year adj good 2007 to 2099
val=val+table[mth-1]; /// table contains modulo 7 adjustments for mths
val=val+day; /// actual day 1..31
tmp=yr%4;
if(tmp==0) { //leap year
if(mth<3) val=val-1; /// adjust jan and feb down one for leap year
}
val=val%7; /// modulo 7
if(val==0)val=7;
/// val is now the day of week 1=Mon .......7=Sun
return(val);
}
int8 dls_adj(int8 hr,int8 day,int8 mth,int8 yr)
{
int8 adj;
int16 year;
int8 BeginDay,Endday;
//// day light savings law info eX 11,1 1st sun in Nov or 3,2 2nd sun Mar
//// is stored in rom
//// const int8 dls_start_date[2]={11,1}; // Ist sun in Nov
//// const int8 dls_end_date[2]={3,2}; /// 2nd Sun in Mar
//// and only resetable by recompiling
////
//// local clock springs forward in march falls back in Nov
/// for Nov thru March local time is GMT -5 RTC is always on GMT-4
/// adj to local time is 0 for March thru Nov and 1 Nov thru March
////US daylight savings always begins on
// the 2nd Sunday in March, and ends on
// the 1st Sunday in November.
// The code will determine the day in March, and the day in November
year=(int16) yr+2000L;
BeginDay = (14L - ((Year * 5L) / 4L + 1L) % 7L);
EndDay = BeginDay - 7L;
//// is the current date in dls between March and November
adj=0;
if((mth==11) && (day<EndDay)) adj=1;
if((mth==11) && (day==EndDay) && (hr<2)) adj=1;
if((mth>3) && (mth<11)) adj=1;
if((mth==3) &&(day>BeginDay)) adj=1;
if( (mth==3) && (day==BeginDay) && (hr>2)) adj=1;
return(adj);
}void compiledatetime_conv()
{ int8 month,hr,dls;
char date[10],time[10];
//////// assign date day of week and time in 24hr format
//// datetime
// 0 int8 sec;
// 1 int8 min;
// 2 int8 hr;
// 3 int8 dow;
// 4 int8 day;
// 5 int8 mth;
// 6 int8 year;
sprintf(date,__DATE__); //// get compile date ex 03-JAN-07
RTC.datetime.day=(date[0]-'0')*10+date[1]-'0'; //day
//// parse the month
month=1;
if((date[3]=='J') && ( date[4]=='a') && (date[5]=='n')) month=1;
if((date[3]=='F') && (date[4]=='e') && (date[5]=='b')) month=2;
if((date[3]=='M') && (date[4]=='a') && (date[5]=='r')) month=3;
if((date[3]=='A') && (date[4]=='p') && (date[5]=='r')) month=4;
if((date[3]=='M') && (date[4]=='a') && (date[5]=='y')) month=5;
if((date[3]=='J') && (date[4]=='u') && (date[5]=='n')) month=6;
if((date[3]=='J') && (date[4]=='u') && (date[5]=='l')) month=7;
if((date[3]=='A') && (date[4]=='u') && (date[5]=='g')) month=8;
if((date[3]=='S') && (date[4]=='e') && (date[5]=='p')) month=9;
if((date[3]=='O') && (date[4]=='c') && (date[5]=='t')) month=10;
if((date[3]=='N') && (date[4]=='o') && (date[5]=='v')) month=11;
if((date[3]=='D') && (date[4]=='e') && (date[5]=='c')) month=12;
RTC.datetime.mth=month; /// month
RTC.datetime.year=(date[7]-'0')*10+date[8]-'0'; ///year
/// day of week
RTC.datetime.dow=day_of_week(RTC.datetime.day,RTC.datetime.mth,RTC.datetime.year);
sprintf(time,__TIME__); //// get compile time hh:mm:ss
/// compiler time stamp is military 00:00 23:59 time
// and is in local time daylight savings adjusted time
//// TIME is already adjusted by PC compiler to dls
//dls=dls_adj(RTC.datetime.hr,RTC.datetime.day,RTC.datetime.mth,RTC.datetime.year);
dls=0; //// no adjustment since dls is already in thetime
hr=(time[0]-'0')*10+time[1]-'0'; //hr
if (dls==1)
{
if (hr<23) hr=hr+dls;
else hr=0;
}
RTC.datetime.hr=hr; //hr
RTC.datetime.min=(time[3]-'0')*10+time[4]-'0';
RTC.datetime.sec=(time[6]-'0')*10+time[7]-'0';
} |
|
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Sat Jul 14, 2012 9:42 am |
|
|
.... ahh
... in it lies the key!
thanks...
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|