CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Simple RTC cron task for On or Off switching.

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

Simple RTC cron task for On or Off switching.
PostPosted: Sat Mar 05, 2016 4:08 am     Reply with quote

Need a simple "cron task". It must switch something on or off according to the RTC (i have the RCT). Simple, i think not. All must be done in software, because the hardware is in production, and this is only a update to the software...

It must be simple therefore my first idea was to map the time ex. 22:15 to minutes 22*60+15 and handle all the time as integer. But i am stocked to find the solution.

I have a polling for ex 5min or 300sec where i can call the main Cron-task. The accuracy is not as important, +- 5 or 10 minutes is ok.

I have the time ex. 12:30 and want to switch x on 06:30 and off 22:00 (24/7-365).
As human i can see it must be switch on now. But how to do it simple in coding?
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Sat Mar 05, 2016 5:07 am     Reply with quote

There is a standard set of functions that converts time (stored in minutes, seconds etc., in a 'struct_t' structure), into a 'time_t' value (int32 seconds since Jan 1st 1970 - or Jan 1 2010). These are in time.c, and time.h (which also has the main comments, explaining how these can be linked to an RTC). One of these 'difftime' allows you to calculate the number of seconds between two of these counts, so if you calculate this from 'now' to 'event', then your RTC, need only count this value 'down'.
You have to #define TIME_T_USES_2010 to switch to using the 2010 origin.

These function exactly like the same functions used in standard Unix.
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Mar 05, 2016 6:21 am     Reply with quote

If you can use time in 15 minute increments, it is very easy.
IE: times like On at 6:15, Off at 17:45
This allows you to divide a 24hr day into 96, 15 segments so an 'on' or 'off' time can be stored in a single byte. As well the 'control' command can be stored as the msbit of the time byte.
Once you get the function to convert control times into bytes the rest is easy.
Simply enter a 'clock' time....convert to 0-96, then enter on or off( 1 or 0) and store in high bit.
The main program simply reads the current time,converts to 0-96, looks at the stored 'command' times and either turns on or off the device based on the high bit.
I ran my remote energy control system with this method of over 30 years using this function. Hundreds of furnaces, HVAC, lighting, etc. Original code was 6800 asm, then 8052 Basic, then 16F877 PCM C.
The code doesn't need 'fancy' math or lots of storage for times (2 bytes/unit/action).

Jay
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Sun Mar 06, 2016 5:32 am     Reply with quote

Maybe my logic is out of sync.
How will you handle 23:59 as a point for on or off. If the actual time is 13:00 and you make have a statement as: “if actual > point”. It will never be true. My polling time is about 5 minutes.
Time is easy in C# but these low level things get me out of sync with it. If you have a small ex it will help a lot.
I already have converted the time stamp to int. I just use minutes @temtronic use 15 minutes, and @Ttelmah use unix time second. My real problem is to find a simple way to make the on or off cron.
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Mar 06, 2016 6:16 am     Reply with quote

Sorry but you can't program times like 23:59, just every 15 minutes.
so 1:00, 1:15,1:30,1:45,2:00 are all valid times.
This code was cut 30 years ago when RTC was software driven ISR and RAM was very,very expensive , and EPROMS were 1KB each.
System was used to remotely control furnaces(HVAC) and 'to the minute' on/off times isn't important.

If you need that, then either use the 'standard C time functions' already mentioned or do simple string comparisons. Both of these methods take up a lot more processing power,time and code space.

Another way would be to modify my method and encode/decode time into minutes (1440 minutes in a day), store as an int16. This does leave 5 extra bits available for 'control or status' functions. It'd also work to get 1/4 second resolution with 1 'control/status' bit.

There 3 options. All will work, which you choose depends upon memory space, processing power(speed) and ease to code. I like mine,version 2 for tightest code, minimal code space and well it's worked for 30 years...

Jay
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Sun Mar 06, 2016 7:52 am     Reply with quote

All ok, just after i posted, i found the solution.
Rounded down to closest 5 min. I changed the scheduler to call the Cron every 60 second. It work ok, and with real small code print.

Thanks for supporting:-)
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Tue Mar 08, 2016 6:17 am     Reply with quote

Working with time 12h/24h is a pain in simple c, i think, or?

12:10 to 12:15 is 5 minutes.
11:55 to 12:10 is 15 minutes.
02:00 to 14:05 is 24h + 5 min.

Is there any simple time calculation lib or some there have one, i think i give up to get this simple. All time too many complex if and else if and else...

Maybe i from the first place look the wrong way to the problem. I convert the string 12:30 to minutes for to only work in int16 maximum is 23:59 (23*60+59). All the cron stuff is running smooth, but now someone ask for diff time from one to another stamp...

Hits on this.
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Mar 08, 2016 6:38 am     Reply with quote

this...
02:00 to 14:05 is 24h + 5 min.

should be 12 hrs + 5 min.

If you 'math' function computes 24hrs... something is wrong.

I'll try to find my QB45 or 8052BASIC 'time machine' code, both are string based,both have worked 'forever'.
I never converted the to CCS C as the hardware was still working, why change?!

Maybe post 2 or 3 examples of times
like
on time off time
01:00 15:05
22:10 05:10
06:05 19:30
as well as your 'function' to decide 'on or off'.

Spanning the days( ex. #2) works for me.

Jay
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

PostPosted: Tue Mar 08, 2016 9:22 am     Reply with quote

Yes I know +12 a brain bug.

My code look like it is working for the cron side, no problem there. if it goes as usual I find the solution within a few days, I just do not think about it.

The main Cron look like:
Code:

//Do Cron ... called every 1 minutes...
void Cron_MainTask(){
 static int8 onoffold=0xFF;
 static int8 onoff=0xFF;

 if (!Cron.OnOff || Cron.Error) {return;}//Only is cron is active =1.
 
 if (Cron.Reset) {Cron.Reset=0; onoffold=0xFF; onoff=0xFF;}
 
 Cron_LoadData();
 
 if (Cron.TimeActual>=Cron.TimeOn  && Cron.TimeActual <Cron.TimeOn+2)  {onoff=1;}
 if (Cron.TimeActual>=Cron.TimeOff && Cron.TimeActual <Cron.TimeOff+2) {onoff=0;}
 
 debug_print_Cron(debug,"Cron_MainTask ActT:%Lu OnT:%Lu OffT:%Lu onoff:%u onoffold:%u\r\n",Cron.TimeActual,Cron.TimeOn,Cron.TimeOff,onoff,onoffold);
 
 
 if (onoff!=onoffold){
  onoffold=onoff;
 
  if (onoff==1) {Cron_DO_On();} else
  if (onoff==0) {Cron_DO_Off();}
 }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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