Searching \ for '[PIC]: Time diff code' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/time.htm?key=time
Search entire site for: 'Time diff code'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Time diff code'
2001\01\08@204558 by Sam Linder

flavicon
face
I've searched the PIC archive but couldn't find anything like what I need.
I'm using a Dallas DS1305 Real Time Clock to obtain the time. I need to be
able to tell if 10 minutes has passed whether it be in the same hour or
whether it be across a new day, or new month, or new year, or new century.
(e.g. if time1 is Dec 31, 2000 at 11:59:24pm and time2 is Jan 01, 2001 at
12:04:42am, then that's less than 10 minutes of elapsed time.)

Has anyone written any PIC code (either asm or C) to do this - or can you
point me at a web site or a FAQ? I'd hate to have to reinvent the wheel.
       Sam....

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2001\01\08@224414 by Ray Gardiner

flavicon
face
Use a count-DOWN timer, initialize to 600 then all you have to do is test for
zero after decrementing each second.

Otherwise convert the current date/time to seconds sime the calendar
started (usually 1980,1904 or whatever) add 600 to arrive at the new
seconds and then convert back to date/time to get the completion date/time

{Quote hidden}

Ray Gardiner spam_OUTrayTakeThisOuTspamdsp.com.au
mail from: dsp systems

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2001\01\09@090733 by Olin Lathrop

face picon face
> I've searched the PIC archive but couldn't find anything like what I need.
> I'm using a Dallas DS1305 Real Time Clock to obtain the time. I need to be
> able to tell if 10 minutes has passed whether it be in the same hour or
> whether it be across a new day, or new month, or new year, or new century.
> (e.g. if time1 is Dec 31, 2000 at 11:59:24pm and time2 is Jan 01, 2001 at
> 12:04:42am, then that's less than 10 minutes of elapsed time.)

Why not just count clock pulses in the code?  Use your main oscillator if
the PIC is contantly running, or use a watch crystal on timer 1 if you need
to sleep.


*****************************************************************
Olin Lathrop, embedded systems consultant in Devens Massachusetts
(978) 772-3129, .....olinKILLspamspam@spam@embedinc.com, http://www.embedinc.com

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2001\01\09@145632 by Sam Linder

flavicon
face
Thanks for the replies, but it isn't quite that simplistic. I'm afraid I
wasn't as clear about the problem as I should have been. This is the
situation.

The PIC is woken up and reads in data from an external EEPROM - part of
which is date/time information in BCD format. (e.g. Dec. 31, 2000 11:59:42
will be stored in memory as: |12|31|00|11|59|42|)

The PIC then gets the current time from its onboard Real Time Clock - also
in BCD format. (e.g. Jan 1, 2001 00:04:00 will be stored in memory as:
|01|01|01|0x00|04|00|)

As you can see, the time difference in the above set of numbers is only 4
minutes and 18 seconds.

The trick is to create an algorithm to test the two sets of values to see if
ten minutes or less has passed. This has to take into account leap years, 30
and 31 day months, change of century (e.g. 2000 to 2001), etc.

It appears to me that a fairly lengthy chunk of code would be required to
implement the algorithm to thoroughly test all possibilities.

Accordingly, I was hoping that someone had already done something like this,
or could suggest a methodology that I could code in asm or C without
inventing the whole thing from scratch myself.

Any ideas? - thanks, Sam....

> Why not just count clock pulses in the code?  Use your main oscillator if
> the PIC is contantly running, or use a watch crystal on timer 1 if you
need
> to sleep.

> *****************************************************************
> Olin Lathrop, embedded systems consultant in Devens Massachusetts
> (978) 772-3129, olinspamKILLspamembedinc.com, http://www.embedinc.com


> {Original Message removed}

2001\01\09@154125 by Don Hyde

flavicon
face
What you want is a julian date conversion function such as is used in Unix.
A 32-bit integer is big enough to store the number of seconds in almost a
century.  A function that takes month, day, year, hour, minute, and second
and yields number of seconds since some arbitrary day 1 is about 10 lines of
C.  This is a standard part of any Unix or Linux system.  Find that and you
have the tool you need.

> {Original Message removed}

2001\01\09@154750 by Ken Godee

flavicon
face
What about converting to Julian Date?

{Quote hidden}

The Perfect Image Graphics Co.
2429 W. 12th St.
Tempe, AZ 85281

800-533-8732

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2001\01\09@160827 by Oliver Broad

flavicon
face
How critical is it that you take care of every eventuality, eg if you just
tested to the 1 day level then unless the PIC was off for exactly 24 hours
it would make the decision OK.

Thinking about it it's better still, if you test minutes using a modulo 60
test that will deal with most conditions, if that test says more than 10
minutes have passed then more than 10 minutes have passed.

If that test returns false then either:
1: less than ten minutes have passed
2: more than ten minutes have passed but a multiple of 60 such that the
minutes test is fooled.

To distinguish: Jjust compare the hours, if the same then compare the date,
if the same then compare the month, if you can be bothered compare the year,
if all are the same as previously stored then less than ten minutes time has
passed, if any one of them is different then more than ten minutes have
passed. You can't tell how much time has passed but it sounds as if you
don't care, it's more than ten minutes.

Length of the month: no problem, Leap year no problem. No special tables
required, just simple compares.

Oliver.

----- Original Message -----
From: "Sam Linder" <.....SamLKILLspamspam.....IN-INC.COM>
To: <EraseMEPICLISTspam_OUTspamTakeThisOuTMITVMA.MIT.EDU>
Sent: 09 January 2001 19:52
Subject: Re: [PIC]: Time diff code


{Quote hidden}

if
> ten minutes or less has passed. This has to take into account leap years,
30
> and 31 day months, change of century (e.g. 2000 to 2001), etc.
>
> It appears to me that a fairly lengthy chunk of code would be required to
> implement the algorithm to thoroughly test all possibilities.
>
> Accordingly, I was hoping that someone had already done something like
this,
> or could suggest a methodology that I could code in asm or C without
> inventing the whole thing from scratch myself.
>
> Any ideas? - thanks, Sam....
>
> > Why not just count clock pulses in the code?  Use your main oscillator
if
> > the PIC is contantly running, or use a watch crystal on timer 1 if you
> need
> > to sleep.
>
> > *****************************************************************
> > Olin Lathrop, embedded systems consultant in Devens Massachusetts
> > (978) 772-3129, olinspamspam_OUTembedinc.com, http://www.embedinc.com
>
>
> > {Original Message removed}

2001\01\09@161151 by jamesnewton

face picon face
I think you need a routine to convert the BCD data to a binary "number of
minutes past 2001" value which can then be stored and later subtracted from
the next value, also processed by the routine. Saves storage space as well.

That routine would use a 24 bit multiplication routine to calculate the
number of minutes in the unit being summed into the result.
http://www.piclist.com/techref/microchip/math/mul/24x24b-tk.htm

You will need to convert the following pseudo-C to PIC assembly:

unsigned long days_this_century(unsigned int since_year) {
 register int  year;
 register int  sum;
 sum = 0;
 for( year=1; year <= since_year; year++ ) {
       if ((year&0x3)==0 && ((year&0xF)==0 || year%100))
           sum += 366;
       else
           sum += 365;
       }
  return(sum);
  }

unsigned months_to_days (unsigned int month) {
  return (month * 3057 - 3007) / 100;
  }

unsigned int days_this_year (unsigned int since_month) {
 register int  month;
 unsigned int  sum;

 for( month=1; month <= since_month; month++ ) {
   sum += months_to_days (month);
   }
 return(sum);
 }

minutes_past_2001 =
((days_this_century(RTCYear)+days_this_year(RTCMonth)+RTCDay) * 24 +
RTCHour) * 60 + RTCMin

http://www.snippets.org/snippets/SCALDATE.C
http://www.rocketaware.com/

Please consider posting the result for future list members use.

---
James Newton (PICList Admin #3)
@spam@jamesnewtonKILLspamspampiclist.com 1-619-652-0593
PIC/PICList FAQ: http://www.piclist.com or .org


{Original Message removed}

2001\01\09@164724 by Drew Vassallo

picon face
>The PIC is woken up and reads in data from an external EEPROM - part of
>which is date/time information in BCD format. (e.g. Dec. 31, 2000 11:59:42
>will be stored in memory as: |12|31|00|11|59|42|)
>
>The PIC then gets the current time from its onboard Real Time Clock - also
>in BCD format. (e.g. Jan 1, 2001 00:04:00 will be stored in memory as:
>|01|01|01|0x00|04|00|)

I'm assuming also that when you awaken, you save off the current time
information after computing the time differential, no?
Also, how are you handling AM/PM?  Why not work off 24-hour time?  This
would solve half your problem.

{Quote hidden}

It depends on what you consider "lengthy."  Is it 1k of program memory?  No.
 It may be a few hundred lines, but I wouldn't even consider that "fairly
lengthy."  This would be a challenge, but not impossible.  Check each EEPROM
location in sequence:
1) year - find difference numerically and save
2) month - if different, it's >10mins.  If same and year is different,
it's >10 mins.  If same and year is same, check day.
3) day - if same but month or year is different, it's >10mins.  Otherwise,
check time.
-- Here's where 24hour time would help:
4) hour - if same but day is different, it's >10mins.  If same day and hour
the same, check minutes.  If you're working with 12hour time, I really don't
know how you could determine if you're AM or PM to check the time.
5) check minutes, etc. using the same logic.

To me, this doesn't seem that tough.  It just takes a little work to get the
logic straight before you code.  But what project doesn't?

--Andrew

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2001\01\10@004151 by rich+piclist

flavicon
face
> The trick is to create an algorithm to test the two sets of values to see if
> ten minutes or less has passed. This has to take into account leap years, 30
> and 31 day months, change of century (e.g. 2000 to 2001), etc.
You didn't say... is it possible to go more than 24 hours without wakeup?
If no, then if the dates don't match it must be the next day, you only
need to check old_time_until_midnight + current_time_since_midnight.
Otherwise, maybe 'Zeller Congruence' will yield a clue == Rich

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email KILLspamlistservKILLspamspammitvma.mit.edu with SET PICList DIGEST in the body


2001\01\10@035658 by o-8859-1?Q?K=FCbek_Tony?=

flavicon
face
Hi,
I don't have any personal expirience with the DS1305 however I took a
quick
glance at it. It seems that You could use one of the alarm register for
this.
As you can mask out hours/days and only use minutes to trigger the
alarm,
one of the int outputs ( whether used or not ) will be low even if only
powered by
battery. So at wake/startup you just need to check the status of this
pin and handle
accordingly, when data has been processed re-set the alarm to current
minutes +
10 minutes ( adjust to be less than 60 ofcource ) and away you go...
In each loop you just check if the pin is low/high.

Doesn't answer your original question though, but it seems like to way
to go there
is to convert to Julian date ( if you decide to go that route ). And
store
as a 24 or 32 bit variable depending if you want to be 'standarized' or
not :)


/Tony


Tony Kübek, Flintab AB            
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²
E-mail: RemoveMEtony.kubekTakeThisOuTspamflintab.com
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email spamBeGonelistservspamBeGonespammitvma.mit.edu with SET PICList DIGEST in the body


2001\01\10@050043 by o-8859-1?Q?K=FCbek_Tony?=

flavicon
face
Hi again, here's a implementation of calculation of julian date:
( shouldn't be to hard to implement in a pic :) )

long BIORHYTHM::JulianDate(UIW_DATE *dateField)
{
       int year;
       int month;
       int day;

       // Get the year, month, and day.
       UI_DATE date = *dateField->DataGet();
       date.Export(&year, &month, &day);

       // A Julian Date is defined as 'a day count starting at 1200 UT
on
       // 1 January -4713' by the U.S. Naval Observatory.  A date
entered is
       // assumed to be after 1200 UTC.  This algorhythm is valid for
any date
       // after Jan. 1, 1700 and accounts for all leap years (including
       // centessimal years and centessimal years divisible by 400).
This
       // algorithm (by Wayne Rust) is useful because it automatically
       // takes the number of days in each month into account and does
not use
       // floating point arithmetic.
       //
       // The day of week can also be computed from this by finding
       // JulianDate % 7 + 1. (1 = Sun, 2 = Mon, etc.)
       year -= 1700 + (month < 3);
       return (365L * (year + (month < 3)) + year / 4 - year / 100 +
               (year + 100) / 400 + (305 * (month - 1) - (month > 2) *
20 +
               (month > 7) * 5 + 5) / 10 + day + 2341972L);
}


Then you 'just' need to add the hour and minutes to achive you goal.

/Tony


Tony Kübek, Flintab AB            
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²
E-mail: TakeThisOuTtony.kubekEraseMEspamspam_OUTflintab.com
²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²²

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email RemoveMElistservspamTakeThisOuTmitvma.mit.edu with SET PICList DIGEST in the body


2001\01\11@131703 by Sam Linder

flavicon
face
First off, let me thank all of you who responded with suggestions. The
members of this list continue to be extremely helpful when I have posed
problems. I waited to respond until I had successfully resolved, then
tested, the solution. Some of the suggestions, while helpful, would not
resolve my problem. The Julian Date suggestions, however, were the key. The
following may be a bit long-winded, but I feel it is necessary to explain my
reasoning. Please bear with me.

The basic problem was that I have a device that writes a date/time out to a
plug-in chip containing an EEPROM. There are many of these chips, only one
of which can be plugged in at a time. When the plug-in chip is removed, the
device shuts down. When a plug-in chip is inserted, the device wakes up and
checks to see if the date/time in the plug-in chip's EEPROM is within 10
minutes of the device's current date/time. Bear in mind that I have to deal
with date/time's that are as disparate as the two test cases shown below and
I'm already running low on code space.

I'm using the Hi-Tech C compiler and a PIC16F877. Using the original
algorithm for Modified Julian Date (found on the web) caused me problems.
For some unknown reason, working with 24-bit doubles or floats did not give
me the correct answer. I finally decided to "integer-ize" the algorithm as
it wasn't necessary to achieve dead-on accuracy. Ten minutes plus or minus a
few seconds is sufficient for the project. Thus, the variation on a Modified
Julian Date function that you see below.

As currently designed, the function consumes 408 bytes of code space.
Interestingly enough, when I tried to consolidate the nine code lines down
to five (as can be seen in the commented out section near the bottom of the
function), the compiler added 40 more bytes of code. Having spent more time
on this than I really wanted to, I decided not to expend the additional
effort to determine why.

In appreciation for all the help, I hereby place this function in the public
domain in the hopes that it may help others facing similar date/time
problems.

       Sam....


// empirical testing determined that any ten minute
// julian date period equated to 69 or 70 decimal counts.
#define TENJULIANMINUTES  70


   // test date = Dec 31, 2000 @23:59:00
   uc_testresult[0] = 12;
   uc_testresult[1] = 31;
   uc_testresult[2] = 00;
   uc_testresult[3] = 23;
   uc_testresult[4] = 59;
   uc_testresult[5] = 00;
   oldjuliandate = ModifiedJulianDate(uc_testresult);  // e.g. 519109992

   // test date = Jan 1, 2001 @00:09:00
   uc_testresult[0] = 1;
   uc_testresult[1] = 1;
   uc_testresult[2] = 1;
   uc_testresult[3] = 0;
   uc_testresult[4] = 9;
   uc_testresult[5] = 0;
   newjuliandate = ModifiedJulianDate(uc_testresult); // e.g. 519110062


   datediff = newjuliandate - oldjuliandate;
   if (datediff > TENJULIANMINUTES) {
       // do error handling here
   }


// **************************************************
// Function Name: ModifiedJulianDate
//
// Description: converts month/day/year/hour/min/sec
//              to modified julian date
//
// Inputs:
//      databuffer: pointer to binary month/day/year/hour/min/sec
//
//
// Outputs: modified julian date
//
unsigned long
ModifiedJulianDate(bank3 unsigned char *databuffer)
{
   unsigned long xrjd;
   unsigned int xrjd2;
   unsigned char month, day, hour, min, sec;
   unsigned int year;


   month = databuffer[0];          // test month = 12
     day = databuffer[1];          // test day   = 31
    year = databuffer[2] + 2000;   // test year  = 00
    hour = databuffer[3];          // test hour  = 23
     min = databuffer[4];          // test min   = 59
     sec = databuffer[5];          // test sec   = 00

   // comments assume a test date of 12/31/2000 @23:59:00

   xrjd = (367*(unsigned long)year);
   //     xrjd =  734000

   xrjd -= (7*((unsigned long)year+((month+9)/12))/4);
   //     xrjd -= 3501 = (7 * (2000 + ((12+9) / 12)) / 4)
   //     xrjd =  730499 = (734000 - 3501)

   xrjd += (275*(unsigned long)month/9);
   //     xrjd += 366 = (275 * 12 / 9)
   //     xrjd = 730865 = (730499 + 366)

   xrjd += day;
   //     xrjd += 31
   //     xrjd = 730896

   xrjd2 = (unsigned int)(((hour + (min + sec/60.0)/60.0)/24.0) * 10000);
   //     xrjd2 = ((23 + (59 + (00/60.0)) / 60.0) / 24.0) * 10000
   //     xrjd2 = 9993

   xrjd += (1721013 + 1);
   //     xrjd = 2451910 = (730896 + 1721013 + 1)

   xrjd -= 2400000;
   //     xrjd = 51910

   xrjd *= 10000;
   //     xrjd = 519100000

   xrjd += xrjd2;
   //     xrjd = 519109993

/*
   xrjd = (367*(unsigned long)year) - (7*((unsigned
long)year+((month+9)/12))/4) +
          (275*(unsigned long)month/9) + day + 1721014;

   xrjd -= 2400000;

   xrjd *= 10000;

   xrjd2 = (unsigned int)(((hour + (min + sec/60.0)/60.0)/24.0) * 10000);

   xrjd += xrjd2;
*/

  return(xrjd);

} // end of ModifiedJulianDate()

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestEraseMEspam.....mitvma.mit.edu


More... (looser matching)
- Last day of these posts
- In 2001 , 2002 only
- Today
- New search...