interrupts (1)

Upload: samih-jaber

Post on 22-Feb-2018

231 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/24/2019 Interrupts (1)

    1/13

  • 7/24/2019 Interrupts (1)

    2/13

  • 7/24/2019 Interrupts (1)

    3/13

    INTERRUPTSDEFINITION

    USAGE

    SYNTAX

    SIDE EFFECTS

  • 7/24/2019 Interrupts (1)

    4/13

    DEFINITION An interrupt (in our context) is a perturbat

    caused to a running program, and handled

    automatically.

    This perturbation causes the program to st

    handle the interrupt, and then resume.

    Handling the interrupt is done through a fu

    called Interrupt Service Routine (ISR).

  • 7/24/2019 Interrupts (1)

    5/13

    USAGE To avoid always checking the input of

    sensors at every iteration, you can use

    interrupt triggered whenever the sensor

    active.

    The interrupt can start on:

    rising edge, falling edge, change, low

    high (not available in UNO).

  • 7/24/2019 Interrupts (1)

    6/13

    USAGE You SHOULD write a specific function

    be called when the interrupt is triggere

    This function SHOULD be VOID with

    NO ARGUMENTS. For our programs, we will trigger the

    interrupt on the rising edge.

  • 7/24/2019 Interrupts (1)

    7/13

    USAGE The interrupt input should be defined i

    program.

    By default, pins 2 & 3 are interrupt pin

    Arduino UNO, but use the functiondigitalPinToInterrupt(pin) with pin = 2

  • 7/24/2019 Interrupts (1)

    8/13

    SYNTAX When c

    goBackprogram

    anywhe

    main.

    We may

    declare

    input.

  • 7/24/2019 Interrupts (1)

    9/13

    SIDE EFFECTS The function delay wont work in ISR

    The function millis wont work in ISR

    The function micros works correctly fo

    1-2ms and then misbehaves. The function delayMicroseconds work

    fin in ISR.

  • 7/24/2019 Interrupts (1)

    10/13

    SIDE EFFECTS Only one ISR runs at a time

    If many interrupts happen, ISRs will exec

    depending on hardware priority.

    If too many interrupts happen at once, som

    may be lost.

    Interrupts dont interrupt ISRs.

    ISRs should be short.

  • 7/24/2019 Interrupts (1)

    11/13

    SIDE EFFECTS If you change a variable inside an ISR,

    and you want to keep it changed, you

    should declare it as volatile.

    If you need to execute some statements

    without interruption, use noInterrupts

    function, and remember to re-enable th

    using interrupts() function.

  • 7/24/2019 Interrupts (1)

    12/13

    SIDE EFFECTS If a

    volati

    chang

    will d

    after t

    ends.

  • 7/24/2019 Interrupts (1)

    13/13