Task 2: Nested External Interrupt Usage

As mentioned, once the CPU enters the ISR, the global interrupt enable bit (I-bit) in SREG will be cleared so that all other interrupts are disabled. In order to use nested interrupts, the I-bit is set by software when the CPU enters an ISR.

Task: Enable INT0 and INT1 interrupts. Within the ISR of INT0 set the I-bit so that INT1 interrupt will be sensed and executed (by jumping to ISR of INT1) while the CPU is inside ISR of INT0.

  1. 1.Configure PORTB0 and PORTC0 as outputs to turn ON LED0 and LED1 respectively. To use the switches in the Atmel STK600 as interrupt source, enable pull-up on PORTD (since PD0 & PD1 are INT0 & INT1 respectively).
  2. 2.Configure INT0 and INT1 to sense rising edge. Enable the interrupts INT0 and INT1 and set the global interrupt enable bit.
  3. 3.Inside the ISR of INT0, set the I-bit (using sei() instruction), turn on LED0 and then turn off LED0 after some delay. Similarly for INT1, turn on and off LED1 with some delay in between.
Hardware Setup:
  1. 1.Connect PB0 pin to LED0 and PC0 pin to LED1 using wires, on the STK600.
  2. 2.Connect two wires; one between the pins SW0 and PD0 (for INT0), and the other between SW1 and PD1 (for INT1).

Without using STK600 connect two LED circuits as shown in Figure 1; one at PB0 and another at PC0 and two switch circuits, one at PD0 and another at PD1.

While running the example code when switch SW0 is pressed, due to pull-up the interrupt INT0 will be triggered only when SW0 is released and LED0 blinks for some time. During the time while LED0 is glowing, a switch action on SW1 will trigger INT1 and hence the LED1 blinks for a moment and goes off. This is because, inside the ISR of INT0, the I-bit is set so that all other interrupts are activated. So even when the CPU is inside the INT0 routine it senses the interrupt INT1 and jumps to the ISR of INT1 and executes the routine and then jumps back to ISR of INT0.

Comment the line ‘sei();’ in the ISR of INT0 and observe the output.