Documente Academic
Documente Profesional
Documente Cultură
What is an Interrupt?
Microcontroller Development
Currently we are coding our Microcontrollers
with sequential code.
– Eg, writing and executing code, line by line.
– Creating Delay loops by repeating same code,
line by line.
Interrupts – Finishing executing the code, we return to the
start of the loop, line by line.
With sequential code, we more or less expect
what is going to happen next.
5 6
1
31/03/2019
7 8
1. When designing a system consider the following 2. Decide on the type of interrupt you need.
questions.
Is instant response needed? Edge Triggered: Can be either falling or rising
Is the micro busy with other processes which may lead to edge.
delays?
Would a delay be significant enough to effect signals or Level Triggered: Used in the past for interrupt
timings of the micro? sharing, (multiple devices connected to the
Are there any external interrupt pins available? same interrupt pin). A device wishing to signal
If the answer is yes to most of these questions, then you will
an interrupt drives the line to its active level, and
need to design a micro using interrupts. then holds it at that level until serviced, (the PCI
If the answer is no, then you are probably safe using a the
polling technique. standard uses this).
9 10
2
31/03/2019
13 14
15 16
3
31/03/2019
//PORTB = 0x08; //use with the debugging routine (see end of code)
while(1)
{
if(PinChange) //Check if pin change detected
{
PORTA |= 0x80;
_delay_ms(1000);
PORTA &= ~0x80;
PinChange = 0; // Reset pin change detection
}
19 }
20
}
//interrupt-service routine
ISR(PCINT0_vect) All the registers presented above, are already defined in the
{
header file iomxx0_1.h (this header file is included
PinChange = 1;
PCIFR = PCIFR | (1 << PCIF0); //re-arm the interrupt
automatically by the Arduino, based on your chosen
} microcontroller platform)
To use PCINT0, you need to configure any pin on PORTB as
//for debugging purposes only : to make sure that you actually get into the ISR
//make sure that you also comment out the PORTB outputs in the while loop
an input, and enable the pull up resistor
/*ISR(PCINT0_vect) – DDRB = 0xFE;
{ //NOTE: configure PB0 (PCINT0) as input and activate only its pull up
//counter++; resistor
PORTB |= 0x80; – PORTB = 0x01;
PCIFR = PCIFR | (1 << PCIF1); //re-arm the interrupt
}*/
One can not pass parameters to the interrupt function. This
means that the only way to be able to modify the value of a
variable from the interrupt is by using volatile global variables.
A volatile global variable is declared outside main() and can
be accessed from any function of the program.
21 22
(1 << PCIE0); is based on the definition of PCIE0 Demonstration where this file is located.
constant in iomxx0_1.h, which is as follows:
– #define PCIE2 2
C:\<install location>\Arduino\hardware\tools\avr\avr\include\avr
– #define PCIE1 1
– #define PCIE0 0
(1 << PCIE0); means that 1 is shifted left 0
positions (PCIE0 is 0), hence 0000 0001 becomes
0000 0001 and when OR’ing with PCICR, it sets bit
0 to 1, hence enabling the PCINT1 interrupt.
Interrupts are serviced when moving from the state
from a high ‘1’ to low ‘0’.
23 24
4
31/03/2019
27 28