Interrupt or Timer?

Guys,

I want to fire an event every second. Which has the least impact on the Panda I? A timer firing every second or an interrupt that is fed by a 1hz signal coming from a DS1307 RTC.

Greetings

They are both really interrupts… Personally I would use the external 1hz from the RTC as timers are a precious commodity in the world of embedded systems.

Just my opinion.

Cheers Ian

Would probably have to test both to be sure if you need to eke out last bit. I would tend to go with what is easier to reason about and test for correctness in code. MF timer does have one side effect that it is “blocking” per timer task. That is to say, only one timer task can run at same time. So you can back yourself into potential live locks. I would tend to favor a simple thread with a sleep. I ~think this stays in managed world, so should not have a native to managed transistion cost. The single worker thread here is easy to reason about control, debug, errors, and avoids any potential timing or overlap issues.

Example:


static bool stopTast = false;
public static void ScheduledTask()
{
    new Thread(() =>
        {
            while (!stopTast)
            {
                // Do Stuff
                Thread.Sleep(1000); // Wait a sec.
            }
        }).Start();
}

if the difference between a tmer and an external interrupt once a second makes a difference to your applications performance you have bigger problems!

The other issue you might face purely using sleep(1000) is that the effort you consume to “do work” means you might drift from 1 second to 1.2 sec at some point, and then you can get out of whack with the actual time - especially bad if that’s a timer to drive a time-based function. So the answer still seems like “it depends” :slight_smile: perhaps its worth elaborating on what your usage scenario is

@ Brett. Most true. However if you just need a relative 1sec for polling, then does not really matter. The opposite issue can happen using timer. If your timer task takes .5 sec, then your getting another event in .5 seconds which may not be obvious and cause other issues. If your timer task takes >=1 sec, then you can get overlapped events (not in netmf). As you say, it depends.

Well, I’m building a Energy logger (electricity, water, gas) with a 3.6" Display attached, and I would like to 1) update the display every x seconds (pref 1 sec) and 2) write the counter values to an xml file on a USB stick, 3) poll or push the counter values and calculated values (like instant consumption) over serial at regular interval (every 5 secs).

What I want to avoid is missing pulses coming in from the meters. The fastest pulse is the electricity one which at minimum will have a pulse distance of about 420ms.

If you’re worried about missing pulses, you can always add a capacitor in there to extend the pulse. I know thats not relevant to your original question, but might be of use anyways :slight_smile: