• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

RTC timer

Status
Not open for further replies.

TucsonDon

Member
I am building a controller with a rtc that incorporates a timer function to start and stop equipment. I am using time.h with the rtc to display the time, so having said that if I want the equipment to start @ “time a” and stop @ “time b” or to run for “time c” can that be calculated using time.h ie if I want to run for 90 min will it convert to 1:30?
 

Nigel Goodwin

Super Moderator
Most Helpful Member
As you don't specify what processor, what compiler, or anything else, it's not really possible to give you any accurate answer.

However, I would say NO - but you could obviously rewrite the time.c/time.h files to add it if you wished.
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
You can create several tm stuctures.. Then populate them as and when you need..

Remember! The time is in seconds since Jan 1980 so to evaluate just convert to longs...

The chip you are using has no RTC built in so you'll have to use timer1 and the function's within time.h..

There are a few good example on the web.
 

TucsonDon

Member
this is the time.h out of the xc8 library no time.c. To get the time from the rtc I am using code from MPLab MCC.
C:
#ifndef _TIME_H_
#define _TIME_H_

#include <__unsupported.h>
#include <__size_t.h>
#include <__null.h>

typedef long time_t;  /* for representing times in seconds */
struct tm {
 int tm_sec;
 int tm_min;
 int tm_hour;
 int tm_mday;
 int tm_mon;
 int tm_year;
 int tm_wday;
 int tm_yday;
 int tm_isdst;
};

#define CLOCKS_PER_SEC 1
#define clock() (-1L)
#define difftime(t1, t0) ((double)((time_t)(t1)-(time_t)(t0)))

extern int time_zone; /* minutes WESTWARD of Greenwich */
    /* this value defaults to 0 since with
       operating systems like MS-DOS there is
       no time zone information available */

extern time_t time(time_t *); /* seconds since 00:00:00 Jan 1 1970 */
extern int stime(time_t *); /* set time */

#ifdef _PIC12
#define __strftime_support __unsupported(strftime)
#define __asctime_support __unsupported(asctime)
#define __ctime_support  __unsupported(ctime)
#define __gmtime_support __unsupported(gmtime)
#define __localtime_support __unsupported(localtime)
#else
#define __strftime_support
#define __asctime_support
#define __ctime_support 
#define __gmtime_support
#define __localtime_support 
#endif

extern char * asctime(const struct tm *) __asctime_support; /* converts struct tm to ascii time */
extern char * ctime(const time_t *) __ctime_support; /* current local time in ascii form */
extern struct tm * gmtime(const time_t *) __gmtime_support; /* Universal time */
extern struct tm * localtime(const time_t *) __localtime_support; /* local time */
extern size_t  strftime(char *, size_t, const char *, const struct tm *) __strftime_support;
extern time_t  mktime(struct tm *);

#endif /* _TIME_H_ */
 

TucsonDon

Member
You can create several tm stuctures.. Then populate them as and when you need..

Remember! The time is in seconds since Jan 1980 so to evaluate just convert to longs...

The chip you are using has no RTC built in so you'll have to use timer1 and the function's within time.h..

There are a few good example on the web.
I am using an external rtc with a i2c interface
 

TucsonDon

Member
Well!!! That makes it easier... My time structure is an array... BUT!! I had to make a datalogger some time since and time was stored on a flash disk as a long.... I'll dig out what I did...
were you able to find anything?
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
were you able to find anything?
Yes and No!... I found some code but it's in basic...

Code:
Function translatetimelong() As Long
 Dim timeshift As Word
 translatetimelong = 0
 Gosub getdatetime
 timeshift = time(0) / 2
 translatetimelong.LW = timeshift
 timeshift = ShiftLeft(time(1), 5)
 translatetimelong.LW = translatetimelong.LW + timeshift
 timeshift = ShiftLeft(time(2), 11)
 translatetimelong.LW = translatetimelong.LW + timeshift
 timeshift = time(4)
 translatetimelong.HW = timeshift
 timeshift = ShiftLeft(time(5), 5)
 translatetimelong.HW = translatetimelong.HW + timeshift
 time(6) = time(6) + 20
 timeshift = ShiftLeft(time(6), 9)
 translatetimelong.HW = translatetimelong.HW + timeshift
End Function      
                                
Proc translatelongtime(number As Long)
 time(0) = number.LW And 0x1f
 time(0) = time(0) * 2
 time(1) = ShiftRight(number.LW, 5)
 time(1) = time(1) And 0x3f
 time(2) = ShiftRight(number.LW, 11)
 time(2) = time(2) And 0x3f
 time(4) = number.HW And 0x1f
 time(5) = ShiftRight(number.HW, 5)
 time(5) = time(5) And 0x1f
 time(6) = ShiftRight(number.HW, 9)
 time(6) = time(6) And 0x3f
 time(6) = time(6) - 20
 
End Proc
time(7) as byte 0,1,2 = seconds minutes and hours 4,5,6 are day month year..
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top