2
\$\begingroup\$

I am using two timer interrupts:

  • TIMER0_OVF triggered every 12 ms, with its ISR only polling a pin, conditionally setting a flag and incrementing a value, so it should complete pretty quick

  • TIMER2_OVF triggered at a rate of 4 kHz, with its ISR only toggling an output pin to make a piezo speaker beep

Since the TIMER2_OVF interrupts are blocked for a short moment every 12 ms by the TIMER0_OVF ISR, the beep doesn't sound clean, it is overlayed by a "rrr...".

Adding ISR_NOBLOCK to the TIMER0_OVF ISR solves this and the beep sounds clean, and the rest still works fine.

However I am unsure if this is a valid use of ISR_NOBLOCK and if there is a more elegant way to generate a "clean" beep?

Reading about ISR_NOBLOCK in <avr/interrupt.h>: Interrupts leaves the impression to me that it is intended for rare cases where nested interrupts are needed and should be used with care.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The AVR has some dedicated pins (OCR?) that can be toggled from timer events by hardware (without the need to perform the IO operation in the ISR / in code). \$\endgroup\$
    – Rev
    Commented Apr 23, 2015 at 14:22
  • 1
    \$\begingroup\$ This works nice and makes the ISR and ISR_NOBLOCK unnecessary. Thanks! There is still a click caused by ADC_SLEEP_MODE as far as I can tell, but I prefer that over the beep causing noise to the AD conversions. \$\endgroup\$ Commented Apr 23, 2015 at 16:50

1 Answer 1

2
\$\begingroup\$

This is a reasonable use of ISR_NOBLOCK as long as you understand the limitations. If you add more interrupts you may encounter problems.

However, for the beep case, you may be able to get the timer to generate the signal with no CPU intervention. See http://www.wrightflyer.co.uk/Using%20AVR%20Counter.pdf

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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