1
\$\begingroup\$

I'm using LPC1778 uC for a project and want to know what is the difference between generating pulses via timer match output and via toggling a pin in a timer ISR. What are the pros and cons of both the approaches? Which one would generate more accurate pulses? Or is there no difference between them?

The only downside I can see to generating pulses via match pins is that uC's generally have only few match pins and hence only a few pulses can be generated via a timer match, while it's possible to generate pulses at any GPIO from a timer ISR.

\$\endgroup\$
1
\$\begingroup\$

Timer match is by far more accurate and is the best solution if you need high precision and the most deterministic behavior.

Timer match has a precision of the CPU bus frequency (to 60 MHz on that part). Interrupts have uncertainty of a few clocks (e.g. if the CPU is in the middle of a long instruction when the interrupt happens). In addition, some CPUs have small caches and the fetch time affects the interrupt latency.

Interrupts can be more flexible if you need some additional decision making on the pin action -- toggle or not, perform calculations etc.

\$\endgroup\$
  • \$\begingroup\$ Since Match pulses are a completely independent unit, any fault in the code will still cause the pulse to be accurately generated. Im using pulses,generated by the uC,that must be validated by a supervisor. Strange behaviour of pulses will indicate the supervisor that somethings wrong. Change in behaviour can easily be detected when generating from a ISR. ISR allows ur program to take full control of every aspect of ur code. \$\endgroup\$ – AkshayImmanuelD May 9 '16 at 5:53
1
\$\begingroup\$

The issue with an ISR-based approach is that it's an interrupt. It interrupts things.

Using the built-in timer compare match features to toggle a match pin is done completely in hardware. You can have code running on the processor and it has no idea that the timer is toggling the pin.

When you use an ISR to toggle a pin, you have more flexibility (you can do more than just toggle a pin, and you can toggle whichever pin you want), but you now have lots of interrupts, interrupting your code. This can upset timing-specific routines, or, at high frequencies, incur unnecessary overhead (your interrupt can become a large portion of the overall instruction count, eating away at cycles that could be used by the main program).

Which one would generate more accurate pulses?

The timer match feature will likely generate more accurate output, unless you are very careful when programming your interrupts. In fact, to get accurate PWM/pulses with ISRs, you'll probably need to write your ISRs in assembly so that you have know how many cycles they take, and then account for this in your code that sets the timings. Basically, the overhead of entering the ISR is going to affect things, timing-wise.

In general, if it's possible to do what you want to do with the timer match pins, do it that way so you don't have the extra interrupt overhead. If it's not possible to accomplish what you need with the timers alone or you've run out of timer pins, then use an ISR.

\$\endgroup\$
  • \$\begingroup\$ I used Keil uVision to simulate a pulse generated from ISR and was able to achieve 499.97Hz out of actually intended 500 Hz. Il try the same for Match and let you know how much I achieved. It all comes down to the last para u said i guess. totally application based \$\endgroup\$ – AkshayImmanuelD May 9 '16 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.