[go: up one dir, main page]

0% found this document useful (0 votes)
112 views5 pages

Microcontroller Development What Is An Interrupt?: Interrupts

An interrupt is an event that stops or interrupts sequential code execution to run interrupt service routine (ISR) code. When the ISR finishes, execution returns to where it left off. Interrupts provide immediate response and allow a microcontroller to perform multiple tasks. External interrupts are triggered by hardware signals to INT pins, while internal interrupts are triggered by timers, ADC, serial communication. The microcontroller saves registers/program counter on the stack during ISR execution then restores them after. Interrupt design considers response needs and available pins/registers to enable interrupts.

Uploaded by

karan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views5 pages

Microcontroller Development What Is An Interrupt?: Interrupts

An interrupt is an event that stops or interrupts sequential code execution to run interrupt service routine (ISR) code. When the ISR finishes, execution returns to where it left off. Interrupts provide immediate response and allow a microcontroller to perform multiple tasks. External interrupts are triggered by hardware signals to INT pins, while internal interrupts are triggered by timers, ADC, serial communication. The microcontroller saves registers/program counter on the stack during ISR execution then restores them after. Interrupt design considers response needs and available pins/registers to enable interrupts.

Uploaded by

karan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

31/03/2019

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.

What is an Interrupt? Interfacing Techniques

 An Interrupt is another event which stops or  Polling


‘interrupts’ your sequential code. – An interfacing technique based on repeated
 When an interrupt occurs, it will stop the checking of a condition to determine if action is
needed. It is implemented by using repetition.
current code, will exit that code and then
– When to use:
commit to executing the interrupt code.
 When there is no need for an immediate response.
 When the interrupt code is complete, the  When simple implementation is needed.
program then returns from where it left off – Note: Polling is using sequential code.
from the original executed code.
3 4

Interfacing Techniques Interrupt Sources

 Interrupt Driven  External Interrupts


– An interfacing technique based on ‘trigger’ events – External events from the microcontroller which triggers an
which lead to: Interrupt to occur.
 Interruption of the flow of the processor program. – Triggered by hardware signals connected to the INTn pins
 Execution of a special function call the ‘interrupt service of the microcontroller.
routine’.  Internal Interrupt
– When to use: – Interrupts that are invoked from within the microcontroller
 When an immediate response is needed. itself. (This to be covered in the next lecture).
 When there are lots of activities that the micro need to – Triggered by the Microcontrollers internal timers, ADC
perform, meaning that it can’t afford to waste time. system, serial communications etc.

5 6

1
31/03/2019

Interrupt Execution Interrupt Execution

 If the interrupt is enabled on the micro, the  Note:


processor:
– Stops the execution of the code. – Microcontorllers can have more than one
– Saves registers on the stack. interrupt. This means an interrupt might be
– Saves the program counter of the stack. interrupted by other interrupts.
– Branches execution to an Interrupt Service Routine, (ISR).
– The ISR deals with the interrupt as required. – The ISR is an asynchronous mechanism. It can
 Eg temperature threshold monitoring system where it may turn on a happen at any moment.
fan once a temperature sensor reaches a certain point.
– When the ISR is exited, all the registers are popped back
onto the stack, including the program counter. This ensures
that the execution of the original code resumes.

7 8

Interrupt Design Interrupt Design

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

Interrupt Design ATMega2560 External Interrupts

3. Decide what you need to happen during the


Interrupt source Port pin
execution of the ISR.
INT0 – INT3 PD0 – PD3
4. Check the datasheet to find out which pins INT4 – INT7 PE4 – PE7
are used as external interrupts. PCINT0
PCINT0:7 PB0:7
5. Check the datasheet to find out which PCINT1
register to use in order to enable and setup PCINT8 PE0
PCINT9:15 PJ0:6
the interrupt.
PCINT2
PCINT16:23 PK0:7
For more, refer to Atmega2560 data sheet
11 12

2
31/03/2019

AVR Registers and External Interrupts Example – The DDRB Register

 To use an interrupt, you need to set them


up.
 An interrupt must be enabled via a setup
register in order for it to be used.
– Eg, like we setup up PORTA and C to be either
input / output via the DDRx register.

13 14

Global Interrupt Enable Selecting Specific Interrupt

 Bit 2, 1, 0 – PCINT2, PCINT1, PCINT0: External Interrupt Request Enable


– when PCINTn = 0, the corresponding interrupt is masked (ie. not
enabled)
– to enable it, PCINTn must be set
(for example, to enable PCINT2: PCICR = PCICR | 0b00000100)
– to be used in conjunction with SREG (see above) and PCMSKn (see
below)

15 16

Select the Hardware Trigger Interrupt Flag

 Bit 2, 1, 0 – PCIF2, PCIF1, PCIF0: Pin Change Interrupt Flag


 Bit 7:0 – PCINT7:0: Pin Change Enable Mask 7:0 2,1,0
– Each PCINT7:0 bit selects whether pin change interrupt is – When a logic change on any corresponding PCINT pin triggers
enabled on the corresponding I/O pin an interrupt request, PCIFn becomes set (one).
– If PCINT7:0 is set and the PCIE0 bit in PCICR is set, pin change – If the I-bit in SREG and the PCIEn bit in PCICR are set (one), the
interrupt is enabled on the corresponding I/O pin. MCU will jump to the corresponding Interrupt Vector.
– If PCINT7:0 is cleared, pin change interrupt on the corresponding – The flag is cleared when the interrupt routine is executed.
I/O pin is disabled. INT1 and INT0 require the presence of an I/O Alternatively, the flag can be cleared by writing a logical one to it.
clock
17 18

3
31/03/2019

/*================ intEx1.c ======================================


PURPOSE - code to test external interrupts
- every time PCINT1 is triggered the onboard LED is turned on for 1 second */
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>

Datasheet #include <util/delay.h>


//user-defined function for the initialisation of the external interrupt PCINT0
void pcint0_init(void);
//============== Executive Loop ============================================
volatile unsigned char PinChange = 0; //global variable
int main(void)
 Refer to ATMega 2506 Datasheet. {//--- User initializations.
DDRB = 0xFE; //NOTE: configure only PB0 (PCINT0) as input and
– Page 112 PORTB = 0x01; //activate its pull up resistor, and turn off LED
DDRA = 0xFF; //Port A set as output

int1_init(); //initialise registers to enable PCINT0

//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
}

//user-defined function for the initialisation of the external interrupt PCINT0


void pcint0_init(void)
{
cli(); //SREG = 0
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT0); //Port B, pin 0
PCIFR |= (1 << PCIF0);
sei(); //SREG = 1 Code Notes
}

//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

Code Notes iomxx0_1.h file

 (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

Code Notes Hex Keypad

 cli(); and sei(); are predefined and take care


of the clearing and setting of the global
interrupt enable pin.  For Practical 4, the Hex Keypad is to be connected
to the Mega2560 Basic Shield.
 At the end of an ISR, you need to re-arm
 The keypad will be used to type alphanumeric
the interrupt by setting the corresponding
values into the microcontroller.
interrupt enable bit. For PCINT0:
 These values are then to be displayed on the LCD.
– PCIFR = PCIFR | (1 << PCIF0);
 The LCD must be set in 4 BIT MODE.
 Note how short the ISR is.  Communication via the keypad to the
microcontroller shall use the ISR.
25 26

Hex Keypad Next Lecture: Timers

 For the next lecture, we will be using the


microcontrollers internal interrupts to create
programmable timers.

27 28

You might also like