Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: The two forms of interrupt handlers

  1. #1

    The two forms of interrupt handlers

    There are two different forms of declaring interrupt handlers that I found by googling various tutorials, maybe I am getting terminology and/or concepts mixde up, be kind, I am an interrupts newbie

    When timers/counters are used, I only found the form

    Code:
    ISR(TIMER1_COMPA_vect)
    in tutorials but when reacting to interrupts from pins, tutorials always declare

    Code:
    attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
    I think it is weird to use the two forms combined in one program? Or am I overthinking it? Should I convert both interrupt handlers in my sketch to one form (which?) or leave it? Is

    Code:
    attachInterrupt(TIMER1_COMPA_vect, my_isr, mode)
    the direct equivalent? Arduino docs say this is not recommended?
    What is the ISR() syntax of attaching an interrupt to a pin?

    What do you do in your programs?

    Thanks in advance,
    Ralph

  2. #2
    Senior Member
    Join Date
    Mar 2015
    Location
    UK
    Posts
    301
    Have a look at this post https://forum.pjrc.com/threads/57386...ure-and-Output to see how you code for a timer interrupt.

    The "attach" statement declares the name of the routine that will become the interrupt for this timer (it changes the jump address in the interrupt table), and the "NVIC ENABLE" switches it on in the interrupt mechanism (not all interrupts are enabled at power up). When the interrupt fires, it is extremely important that the flag that caused the interrupt is cleared, or else the interrupt mechanism will not fire a second time - and this can cause a failure of operation on all the interrupts.

    Remember that there is also a "priority" value associated with interrupts. The lower the "numerical value", then the higher is the priority - "0" being the highest. This is important for the cpu to test if some other device can also "interrupt" the ISR that your timer might have already called - you could be running through one ISR, when it suddenly gets pre-empted by yet some other ISR. Each time an interrupt occurs, the registers in the cpu have to be saved (somewhere - often on the STACK) such that they can be recovered when the interrupt is over with, and processing returns to normal. If an ISR itself is interrupted, then you will get two lots of register sets being saved on STACK - or even more, depending if these secondary interrupts have priority higher than that subsequently being executed.

    There is also a very important "assembler" intruction called "CLI" (and its inverse "SLI"). CLI means "clear all interrupts" as a means of preventing other interrupts from interrupting your ISR. Its a MASTER type command that shuts down all further interrupts (including yours) until further notice. SLI will "set all interrupts" back again. If you ever use CLI, then you must stick an SLI at the end of your protected code, or else your program will never accept another interrupt... with the consequences this might have.

    A second interrupt can interrupt one currently being serviced if it is of equal or higher priority. But not if of lower priority - it will be "pending" until the current ISR has finished.

    It can be an involved topic

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,920
    What Teensy is in use?

    The first ref may be the AVR version. For ARM Teensy 3.x, LC and 4.0 There are differences.

    This page shows Teensy Timer _isr() usage - it is different from a pin interrupt as it has a wrapper to set up the timer: pjrc.com/teensy/td_libs_TimerOne.html

    This page shows using interrupts on pins :: arduino.cc/ … attachinterrupt/

    For an ARM Teensy the third listed as 'Not recommended' happens to work Properly:
    attachInterrupt(pin, ISR, mode) (Not recommended. Additionally, this syntax only works on Arduino SAMD Boards, Uno WiFi Rev2, Due, and 101.)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •