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

executing a task every x minute

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



Joined: 18 Mar 2009
Posts: 38

View user's profile Send private message

executing a task every x minute
PostPosted: Tue Sep 18, 2012 2:46 pm     Reply with quote

Hello every body,

try to find a simple piece of code for executing a logging task every x minute with x which could be anything between 1 to 20.
For this purpose I have already a variable containing the minute of the hour. Of course this variable change every minute Exclamation Exclamation

At first I was thinking to calculate the rest of the divisision of the minute by the step, but that uses float calculations and I try to avoid that.
Any other good idea ?

thanks for the answer.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Sep 18, 2012 3:16 pm     Reply with quote

have a variable called "interval" ... for example..
and another one called "alarm"

Code:
While (1)

when "minutes" = "alarm"
"alarm"+="interval"
if "alarm">60 "alarm-=60
execute task()


its really not that dificult...

floating point math is totally unnesesary.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue Sep 18, 2012 3:21 pm     Reply with quote

sorry im at the office and forgot i had a local copy of my Alarm code....

this is basically what i suggested

you can ignore the hex to bcd conversions....
this is from my DS1305 driver ... it works with BCD numbers..

Concept is the same.

Code:
//****************************************************************************************
// this function needs to be called within the minute the alarm is triggered.
// it sets the next alarm time based on the time of the previous one plus an offset.
// takes minute reading, converts it from bcd to hex, takes interval or offset set by user
// converts it to hex, adds past time with offset to get new minutes of next alarm time.
// Alarm intervals are limited 1 to 60 minutes max.
// requires BCD_to_HEX() and HEX_to_BCD() included in this file for support
// ***************************************************************************************
int Next_Alarm()
{

   N_Alarm=(BCD_to_HEX(minutes) + BCD_to_HEX(interval)); // Convert minutes and interval to hex and add them

   if(N_Alarm<0X3C)                    // If Next alarm less than 59, convert to BCD and return
      {
         return(HEX_to_BCD(N_Alarm));
      }
   if(N_Alarm==0X3C)                   // If Next alarm = 60, return cero (minute 60 = 00)
      {
         return(0X00);
      }
   if(N_Alarm>0X3C)                    // If Next alarm 60+, substract 60, convert and return
      {
         N_Alarm-=0X3C;
         return(HEX_to_BCD(N_Alarm));
      }
}

_________________
CCS PCM 5.078 & CCS PCH 5.093
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