Chapter 4 - Interrupts Revised
Chapter 4 - Interrupts Revised
Chapter 4
AVR Interrupts
www.vut.ac.za
1
CONTENTS
2
M o d u l e 3 – AV R I n t e r r u p t s
▪ AVR devices provide several different interrupt sources including internal and external interrupts.
▪ Interrupts can stop the main program from executing to perform a separate interrupt service routine (ISR).
▪ When the ISR is completed, program control is returned to the main program at the instruction that was interrupted.
▪ With AVR Micro-controllers, you can configure interrupts on various sources such as:
▪ All interrupts are assigned individual enable bits which must be written in one logic together with the Global Interrupt Enable bit in the
Status Register to enable the interrupt.
▪ The lowest addresses in the program memory space are by default defined as the Reset and Interrupt Vectors.
▪ They have determined priority levels, the lower the address the higher the priority level. RESET has the highest priority, and next is the
External Interrupt Request 0 (INT0).
3
M o d u l e 3 – AV R I n t e r r u p t s
▪ External Interrupts
▪ External interrupts are triggered by the INT pin or any of the PCINT pins.
▪ If enabled, the interrupts trigger even if the INT or PCINT pins are configured as outputs.
▪ This feature provides a way of generating a software interrupt.
▪ The external interrupts can be triggered by a falling or rising edge or a low level.
▪ This is set up by the External Interrupt Control Register A EICRA.
▪ When the external interrupts are enabled and are configured as level-triggered, the interrupts trigger if the pin is held low.
▪ Timer Interrupts
▪ Status flags in the TIMSK register show if an event has occurred or the timer is at overflow state.
▪ A timer overflow means that the counter has counted to its maximum value and is reset to zero in the next timer clock cycle.
▪ The resolution of the timer determines the maximum value of that timer. There are two timers with 8-bit resolution and third timer with 16-bit
resolution on the ATmega32.
▪ Depending on the Timer selected, TIMISK register can be used to enable the interrupt for the selected Timer while TIFR can be used to monitor
the status of all Timer enabled interrupts.
▪ For Timer interrupts to function, I-Global bit of SREG must be configured to high state.
▪ SPI
▪ The SPI interrupt gets triggered when the serial transfer is complete, the SPIF Flag in the SPSR register is set.
▪ An interrupt is generated if SPIE bit in SPCR is set, and global interrupts are enabled. 4
▪ SPIF is cleared by hardware when executing the corresponding interrupt handling vector.
M o d u l e 3 – AV R I n t e r r u p t s
▪ USART
▪ USART interrupts can be used to transmit and receive data. This increases the efficiency of the code and keeps the processor free for other tasks.
▪ The UCSRB register has RXCIE (Reception Complete Interrupt Enable) bit, which is used to enable the serial reception interrupt.
▪ The I-bit of SREG register must be set to enable global interrupt of ATmega32.
▪ Every time one byte of the data is received serially, a serial receive interrupt is generated and the control transfers to the corresponding ISR.
▪ The RXC flag will go high to indicate the serial receive interrupt.
▪ WDT
▪ In Interrupt mode, the WDT forces an interrupt when the timer expires.
▪ This interrupt can be used to wake the device from any of the sleep-modes, and as a general system timer.
▪ One example is to limit the maximum time allowed for certain operations, forcing an interrupt when the operation has run longer than expected.
▪ This is enabled by setting the Interrupt mode bit (WDIE) in the Watchdog Timer Control Register (WDTCSR).
▪ Interrupt and System Reset mode combines the other two modes by first forcing an interrupt and then switching to the System Reset mode.
▪ This mode will offer a safe shutdown by allowing time to save critical parameters before a system reset. This is enabled when both the WDTIE
and WDTE are set.
▪ The Watchdog System Reset Flag (WDRF) bit in the MCU Status Register (MCUSR) is set if a Watchdog System Reset occurs.
▪ The WDRF bit is cleared by a Power-on Reset or by writing '0' to it. To identify a reset condition, the user should read and then reset the WDRF
as early as possible in the program.
▪ If the register is cleared before another reset occurs, the source of the reset can be found by examining the Reset Flags.
5
M o d u l e 3 – AV R I n t e r r u p t s
▪ ADC
▪ in-built ADC of AVR consists of ADIE (ADC interrupt Enable) bit in ADCSRA register.
▪ In addition, the I-bit of SREG is also activated to activate interrupts
▪ ADC system consists of ADIE bit in ADCSRA register.
▪ ADIE bit is enabled to use ADC interrupts
▪ In A/D conversion ADSC bit remains high till the conversion is not completed. As soon as the conversion gets completed, ADSC automatically
gets cleared by hardware.
▪ Before starting the next conversion, ADSC must be set high again. Alternatively, auto triggering can be used to enable the ADSC bit after each
conversion.
▪ The ADATE (ADC Auto Triggering Enable) bit in ADCSRA register is used to activate auto-triggering mode.
▪ Analog Comparators
▪ The comparator’s output can be set to trigger the Timer/Counter1 Input Capture function.
▪ In addition, the comparator can trigger a separate interrupt, exclusive to the Analog Comparator. The user can select Interrupt triggering on
comparator output rise, fall or toggle.
▪ Analog Comparator interrupt can be enabled by ACIE bit in the Analog Comparator and Status Register (ACSR) and configuring I-Global bit of
SREG bit to high.
▪ Once the Interrupt has triggered, the ACI bit in the ACSR register will be set and this bit can be cleared by software.
6
M o d u l e 3 – AV R I n t e r r u p t s
▪ TWI/I2C
▪ I2C (Inter-Integrated Circuit) is a serial bus interface connection protocol. It is also called TWI (two-wire
interface) since it uses only two wires for communication, that two wires called SDA (serial data) and SCL
(serial clock).
▪ This bit gets set whenever TWI completes its current event (like start, stop, transmit, receive, etc).
▪ While I-bit in SREG and TWIE bit in TWCR is enabled then TWI interrupt vector called whenever TWI
interrupt occurs.
▪ TWI interrupt flag must be cleared by software by writing a logical one to it. This bit is not automatically cleared
by hardware.
7
M o d u l e 3 – AV R I n t e r r u p t s
▪ The method of handling interrupts differs in different languages. For an Interrupt to fire ISR three
conditions must be true. When all three conditions are met, the AVR will fire our ISR each time the
interrupt event occurs.
1. The AVR’s global Interrupts Enable bit must be set to one in the microcontroller control register SREG.
▪ This allows the AVR’s core to process interrupts via ISRs when set and prevents them from running when
set to zero.
▪ It is like a global ON/OFF switch for the interrupts. By default, this bit is zero.
2. The individual interrupt source’s enable bit must be set.
▪ Each interrupt source has a separate interrupt enable bit in the related peripherals control registers, which
turns on the ISR for that interrupt.
▪ This must also be set, so that when the interrupt event occurs the processor runs the associated
ISR.
3. The condition of the interrupt must be meet
▪ for example, when the ADC conversion is complete then only it will fire ADC conversion interrupt.
8
M o d u l e 3 – AV R I n t e r r u p t s
1. When an interrupt occurs, the Global Interrupt Enable I-bit is cleared and all interrupts are disabled.
2. The interrupt vector directs program control to the proper ISR or execution.
▪ That ISR can write logic one to the I-bit to enable nested interrupts.
3. All enabled interrupts can then interrupt the current interrupt routine.
4. When the ISR is completed and the return (RETI) command is executed from the ISR, the Global I-bit
is automatically set to 'ON' and the program execution returns to the main program at the instruction
that was interrupted.
9
M o d u l e 3 – AV R I n t e r r u p t s
▪ The interrupt execution response for all the enabled AVR interrupts is four clock cycles minimum.
▪ After four clock cycles, the program vector address for the actual interrupt handling routine is executed.
▪ During this four-clock cycle period, the Program Counter is pushed onto the stack.
▪ The vector is normally a jump to the interrupt routine, and this jump takes three clock cycles.
▪ If an interrupt occurs during execution of a multi-cycle instruction, this instruction is completed before the interrupt
is served.
▪ If an interrupt occurs when the MCU is in sleep mode, the interrupt execution response time is increased by four
clock cycles.
▪ This increase comes in addition to the start-up time from the selected sleep mode.
▪ A return from an interrupt handling routine takes four clock cycles. During these four clock cycles, the Program
Counter (two bytes) is popped back from the stack, the stack pointer is incremented by two, and the I-bit in SREG is
set.
10
M o d u l e 3 – AV R I n t e r r u p t s
▪ These interrupts each have a separate program vector in the program memory space.
▪ The vector name is the identifier that should be used at the start of the interrupt service routine (ISR).
▪ The names of the vectors are not always the same for the same interrupt in different processors
▪ In the ATmega32 processor, an Interrupt Vector is the address used to access the ISR when an enabled interrupt is triggered.
▪ A instruction that causes the processor to jump to the actual ISR code used to handle the interrupt must be placed at the vector address.
▪ Vectors with lower numbers have higher priority. In ATmega32 processors, priority determines which of several simultaneously
occurring interrupts will be handled and executed first.
▪ The section of memory (addresses 0x0000 to 0x0032) that holds the vectors (ISR jump instructions) can be referred to as a “vector
table”
▪ The Interrupt Vectors can be moved to the start of the Boot Flash section by setting the IVSEL bit in the MCU Control Register
MCUCR.
▪ The Reset Vector can also be moved to the start of the Boot Flash section by programming the BOOTRST Fuse.
11
M o d u l e 3 – AV R I n t e r r u p t s
AVR Interrupts
- Interrupt
Vector Table
12
M o d u l e 3 – AV R I n t e r r u p t s
13
M o d u l e 3 – AV R I n t e r r u p t s
14
M o d u l e 3 – AV R I n t e r r u p t s
7 6 5 4 3 2 1 0
INT1 INT0 INT2 - - - IVSEL IVCE
15
M o d u l e 3 – AV R I n t e r r u p t s
16
M o d u l e 3 – AV R I n t e r r u p t s
17
M o d u l e 3 – AV R I n t e r r u p t s
18
M o d u l e 3 – AV R I n t e r r u p t s
24
THANK YOU
Andries Po tgieter Blvd. Vanderbij lpark, 1900, So uth Africa | T 098 008 8900 | E [email protected]. za www.vut.ac.za
25