Interrupts Naturel

Interrupts

Interrupts met Arduino

  • Alleen maar External Interrupts
  • Max 2 pinnen
    • D2 & D3 voor de Nano
  • Verschillende triggers mogelijk
  • Andere interrupts zijn niet exposed

      interrupts()
      noInterrupts()
      attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)
      detachInterrupt(digitalPinToInterrupt(pin));
    

Interrupts op de AVR

  • Externe Interrupts
  • Pin Change Interrupts
  • Software Interrupts

Interrupt Vectors

  • Tabel met adres van ISR
    • Interrupt Service Routine
  • 26 ISR beschikbaar
  • Gerankschikt volgens prioriteit
  • Vectors staan vanvoor aan het geheugen

Interrupt Vectors

  • Tabel met adres van ISR
    • Interrupt Service Routine
  • 26 ISR beschikbaar
  • Gerankschikt volgens prioriteit
  • Vectors staan vanvoor aan het geheugen

#define INT0_vect_num     1
#define INT0_vect         _VECTOR(1)   /* External Interrupt Request 0 */

#define INT1_vect_num     2
#define INT1_vect         _VECTOR(2)   /* External Interrupt Request 1 */

#define PCINT0_vect_num   3
#define PCINT0_vect       _VECTOR(3)   /* Pin Change Interrupt Request 0 */

#define PCINT1_vect_num   4
#define PCINT1_vect       _VECTOR(4)   /* Pin Change Interrupt Request 0 */

#define PCINT2_vect_num   5
#define PCINT2_vect       _VECTOR(5)   /* Pin Change Interrupt Request 1 */

#define WDT_vect_num      6
#define WDT_vect          _VECTOR(6)   /* Watchdog Time-out Interrupt */

#define TIMER2_COMPA_vect_num 7
#define TIMER2_COMPA_vect _VECTOR(7)   /* Timer/Counter2 Compare Match A */

#define TIMER2_COMPB_vect_num 8
#define TIMER2_COMPB_vect _VECTOR(8)   /* Timer/Counter2 Compare Match A */

#define TIMER2_OVF_vect_num   9
#define TIMER2_OVF_vect   _VECTOR(9)   /* Timer/Counter2 Overflow */

#define TIMER1_CAPT_vect_num  10
#define TIMER1_CAPT_vect  _VECTOR(10)  /* Timer/Counter1 Capture Event */

#define TIMER1_COMPA_vect_num 11
#define TIMER1_COMPA_vect _VECTOR(11)  /* Timer/Counter1 Compare Match A */

#define TIMER1_COMPB_vect_num 12
#define TIMER1_COMPB_vect _VECTOR(12)  /* Timer/Counter1 Compare Match B */ 

#define TIMER1_OVF_vect_num   13
#define TIMER1_OVF_vect   _VECTOR(13)  /* Timer/Counter1 Overflow */

#define TIMER0_COMPA_vect_num 14
#define TIMER0_COMPA_vect _VECTOR(14)  /* TimerCounter0 Compare Match A */

#define TIMER0_COMPB_vect_num 15
#define TIMER0_COMPB_vect _VECTOR(15)  /* TimerCounter0 Compare Match B */

#define TIMER0_OVF_vect_num  16
#define TIMER0_OVF_vect   _VECTOR(16)  /* Timer/Couner0 Overflow */

#define SPI_STC_vect_num  17
#define SPI_STC_vect      _VECTOR(17)  /* SPI Serial Transfer Complete */

#define USART_RX_vect_num 18
#define USART_RX_vect     _VECTOR(18)  /* USART Rx Complete */

#define USART_UDRE_vect_num   19
#define USART_UDRE_vect   _VECTOR(19)  /* USART, Data Register Empty */

#define USART_TX_vect_num 20
#define USART_TX_vect     _VECTOR(20)  /* USART Tx Complete */

#define ADC_vect_num      21
#define ADC_vect          _VECTOR(21)  /* ADC Conversion Complete */

#define EE_READY_vect_num 22
#define EE_READY_vect     _VECTOR(22)  /* EEPROM Ready */

#define ANALOG_COMP_vect_num  23
#define ANALOG_COMP_vect  _VECTOR(23)  /* Analog Comparator */

#define TWI_vect_num      24
#define TWI_vect          _VECTOR(24)  /* Two-wire Serial Interface */

#define SPM_READY_vect_num    25
#define SPM_READY_vect    _VECTOR(25)  /* Store Program Memory Read */

#define _VECTORS_SIZE (26 * 4)



/* Constants */
#define SPM_PAGESIZE 128
#define RAMSTART     (0x100)
#define RAMEND       0x8FF     /* Last On-Chip SRAM Location */
#define XRAMSIZE     0
#define XRAMEND      RAMEND
#define E2END        0x3FF
    

External Interrupt Control Register A

  • Bepaal de sense control voor:
    • INT0
    • INT1

External Interrupt Mask Registers

  • Enable de interrupts:
    • INT0
    • INT1
  • States:
    • Enabled = 1
    • Disabled = 0

External Interrupt Flag Register

  • Checkt voor de status van:
    • INT0
    • INT1
  • States:
    • Geen interrupt = 0
    • Interrupt Triggerd = 1

Pin Change Interrupt Control Registers

  • Enable de interrupts:
    • PCINT0
    • PCINT1
    • PCINT2
  • States:
    • Enabled = 1
    • Disabled = 0

Pin Change Interrupt Flag Register

  • Checkt voor de status van:
    • PCINT0
    • PCINT1
    • PCINT2
  • States:
    • Geen interrupt = 0
    • Interrupt Triggerd = 1

Pin Change Mask Register O/1/2

  • Enable de pinnen van de poort
    • PCINT0
    • ...
    • PCINT23
  • States:
    • Pin Disabled = 0
    • Pin Enabled = 1

Pin Change Mask Register O/1/2

  • Enable de pinnen van de poort
    • PCINT0
    • ...
    • PCINT23
  • States:
    • Pin Disabled = 0
    • Pin Enabled = 1

ISR Enabelen

  • Correct configuren pinnen
  • Correct configuren interrups
  • ISR schrijven

ISR({VECTOR}_vect)
{
  //Perform Task 
}    
    

Nested Interrupts

  • Kan problematisch zijn
    • Recursief optreden bij communicatie
  • Staat standaard af
  • 2 manieren om nested interrupts in te stellen
    • sei() call in het begin van de nested interrupt
    • Non blocking interrupt met ISR(XXX_vect, ISR_NOBLOCK)

Voorbeeld


void setup()
{
  DDRD &= ~(1 << DDD2);
  EICRA |= ((1 << ISC00) | (1 << ISC01))  ;
  EIMSK |= (1 << INT0);
  Serial.begin(9600);
  sei();
}

void loop()
{
  //do nothing
}

ISR(INT0_vect)
{
  Serial.println("Interrupt fired");
};

  

Oefening #1

  • Laad de voorbeeldcode op circuits.io
  • Probeer de interrupt te triggeren.

Oefening #2

  • Maak een External Interrupt met volgende voorwaarde:
    • Interrupt op pin 3
    • Trigger op elke edge

Oefening #3

  • Maak een pin change interrupt met volgende voorwaarden
    • Detect change op pin D8