View previous topic :: View next topic |
Author |
Message |
dynamitron
Joined: 18 Mar 2009 Posts: 38
|
executing a task every x minute |
Posted: Tue Sep 18, 2012 2:46 pm |
|
|
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
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
|
|
Posted: Tue Sep 18, 2012 3:16 pm |
|
|
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
|
|
Posted: Tue Sep 18, 2012 3:21 pm |
|
|
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 |
|
|
|