View previous topic :: View next topic |
Author |
Message |
1953cub
Joined: 28 Jan 2014 Posts: 7
|
Long timer times |
Posted: Fri Nov 07, 2014 10:10 am |
|
|
Hello,
I'm trying to figure out the best way to have timing intervals in the order of 5 to 10 minutes. I have a project that monitors equipment, does whatever based on inputs, updates an LCD display and also sends txt messages with issues. Now that this is working I would like to be able to setup a function that will check to see if say at least 10 minutes has passed and if so send a general sms status message. It's fine if it's 10 minutes 30 seconds (or whatever) as it's just a status. Checking the function would be in the main loop so depending on whats going on, the time it gets called could vary 1-20 seconds as it's a very low priority.
So can I setup a timerX with very long interval (like 5 minutes) and check to see if it over flowed whenever the loop got around to it. Without using interrupts. Then clear it and send message.
Or should I setup the timer with a tick of maybe 1 second, and use an ISR to increment a 16bit counter and check it to see if it's over XX(seconds) and if so clear it and send the status message ?
I think the latter is probably the best way, but I'm a little worried about to many interrupts happening and using the timers is pretty new to me. And interrupts still scare me
How long is it reasonable to set a timer/counter to ?
Any guidance or thoughts would be very welcome.
I'm using an 18F46K22 chip with a 16mhz crystal
compiler version 4.141
Thanks
Tim |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Fri Nov 07, 2014 10:35 am |
|
|
There's a nice software RTC program in the 'code library' here that would be a GREAT start for you!! Shows how to set/test 'flags' for hrs,min,day,etc. so 99% of the hard part is done for you.
hth
jay |
|
|
VernonAMiller
Joined: 11 Sep 2014 Posts: 25 Location: Contoocook, NH
|
Re: Long timer times |
Posted: Fri Nov 07, 2014 11:48 am |
|
|
1953cub wrote: |
Or should I setup the timer with a tick of maybe 1 second, and use an ISR to increment a 16bit counter and check it to see if it's over XX(seconds) and if so clear it and send the status message ?
I think the latter is probably the best way, but I'm a little worried about to many interrupts happening and using the timers is pretty new to me. And interrupts still scare me
|
To address your concern, as long as you do as little as possible in the interrupt service routine (ISR), you will have no problem at all with that scheme.
Here is another simple example from some code I wrote. It uses Timer0 to keep track of system uptime and also to trigger the tasks I want to do once per minute.
Code: |
#INT_TIMER0
void TIMER0_isr(void) {
static UINT16 ticksMinute = TICKS_PER_MINUTE;
// Update upTime.
upTime++;
// Check for once-per-minute.
if (--ticksMinute == 0) {
ticksMinute = TICKS_PER_MINUTE;
doOncePerMinute = TRUE;
}
} // End of "TIMER0_isr(void)"
|
In this case, "upTime" is a global unsigned int32 variable that gets incremented with every interrupt (in this case every 100 mS). I have an external flag "doOncePerMinute" that gets set if 1 minute has passed. I then check it in the main loop; if set, I call the housekeeping tasks I need to do once per minute, and then clear the flag.
The whole routine is very short and quick - simple compares and increments and the like. The key is to just check what you need to check in the ISR, and set flags so that the time-consuming tasks can get done in the main loop where time is usually not as much of a factor.
VAM |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Fri Nov 07, 2014 12:34 pm |
|
|
As a general 'comment', depending on how fast the 'tick' is, and how fast the main loop is, I prefer to use 'countdowns'.
So:
Code: |
int16 sec_count;
#define TICKS_PER_SEC xxx //depending on your interrupt rate
#INT_TIMERx //whatever timer you are using
void tick(void)
{
static int8 per_sec = TICKS_PER_SEC-1;
if (per_sec)
--per_sec;
else
{
per_sec = TICKS_PER_SEC-1;
if (count)
--count;
}
}
|
If (for instance) you want to wait for 5 minutes, set 'count = (5*60)-1', and then wait till it is zero. Since the counter stops when it reaches zero, you never 'miss' the zero. |
|
|
VernonAMiller
Joined: 11 Sep 2014 Posts: 25 Location: Contoocook, NH
|
|
Posted: Fri Nov 07, 2014 12:37 pm |
|
|
Good point, thanks!
VAM |
|
|
1953cub
Joined: 28 Jan 2014 Posts: 7
|
|
Posted: Fri Nov 07, 2014 1:00 pm |
|
|
Thanks Jay, for the tip on the RTC and the count down point. I think I may add the RTC later as I can use it display date time on LCD without constantly spending time talking to modem and I should be able to read the time from the SMS modem to set the RTC.
And thanks JAM for the help on keeping the Timer ISR short & sweet and checking flags later. Great plan.
Best,
Tim |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri Nov 07, 2014 5:22 pm |
|
|
I agree with Mr T., use count-downs.
You can use several in the one ISR, keeps things simple and compact.
Mike |
|
|
|