0% found this document useful (0 votes)
32 views17 pages

Programable Elements I Interrupts

Here are some code examples to implement the activities using interrupts on an ATTINY microcontroller: 1. Alternating LED cycles using external interrupt on HIGH signal: #define LED_PIN 1 ISR(INT0_vect){ static uint8_t led_state = 0; led_state = !led_state; if(led_state) PORTB |= (1<<LED_PIN); else PORTB &= ~(1<<LED_PIN); } int main(){ DDRB |= (1<<LED_PIN); //LED pin as output EICRA |= (1<<ISC01); //interrupt on rising edge EIMSK |=
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)
32 views17 pages

Programable Elements I Interrupts

Here are some code examples to implement the activities using interrupts on an ATTINY microcontroller: 1. Alternating LED cycles using external interrupt on HIGH signal: #define LED_PIN 1 ISR(INT0_vect){ static uint8_t led_state = 0; led_state = !led_state; if(led_state) PORTB |= (1<<LED_PIN); else PORTB &= ~(1<<LED_PIN); } int main(){ DDRB |= (1<<LED_PIN); //LED pin as output EICRA |= (1<<ISC01); //interrupt on rising edge EIMSK |=
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/ 17

Programable elements I

Interrupts
Interrupt Definition
• Interrupt request, signal sent to the processor that temporarily
stops a running program and allows a special program,
an interrupt handler, to run instead.

ISR(TIMER1_OVF_vect)//Interrupt Service Routine


{
Do Sthg
}
Sources
• Timers
• ADC
• Port Pins : INT0, INT1
and INT2
• Analog Comparator
• EEPROM
• UART
• SPI
• TWI or I2C
Priority
HIGHEST

LOWEST
Interrupt Vectors
Vector
Interrupt definition Vector name Vector
Number Interrupt definition Vector name
2 External Interrupt Request 0 INT0_vect Number
3 External Interrupt Request 1 INT1_vect 1 External Interrupt Request 0 INT0_vect
4 Pin Change Interrupt Request 0 PCINT0_vect 2 Pin Change Interrupt Request 0 PCINT0_vect
5 Pin Change Interrupt Request 1 PCINT1_vect
6 Pin Change Interrupt Request 2 PCINT2_vect 3 Timer/Counter1 Compare Match A TIMER1_COMPA_vect
7 Watchdog Time-out Interrupt WDT_vect
8 Timer/Counter2 Compare Match A TIMER2_COMPA_vect
4 Timer/Counter1 Overflow TIMER1_OVF_vect
9 Timer/Counter2 Compare Match B TIMER2_COMPB_vect
10 Timer/Counter2 Overflow TIMER2_OVF_vect 5 Timer/Counter0 Overflow TIMER0_OVF_vect
11 Timer/Counter1 Capture Event TIMER1_CAPT_vect
12 Timer/Counter1 Compare Match A TIMER1_COMPA_vect 6 EEPROM Ready EE_RDY_vect
13 Timer/Counter1 Compare Match B TIMER1_COMPB_vect 7 Analog Comparator ANA_COMP_vect
14 Timer/Counter1 Overflow TIMER1_OVF_vect
8 ADC Conversion Complete ADC_vect
15 Timer/Counter0 Compare Match A TIMER0_COMPA_vect
16 Timer/Counter0 Compare Match B TIMER0_COMPB_vect 9 Timer/Counter1 Compare Match B TIMER1_COMPB_vect
17 Timer/Counter0 Overflow TIMER0_OVF_vect
18 SPI Serial Transfer Complete SPI_STC_vect 10 Timer/Counter0 Compare Match A TIMER0_COMPA_vect
19 USART Rx Complete USART_RX_vect
20 USART Data Register Empty USART_UDRE_vect
11 Timer/Counter0 Compare Match B TIMER0_COMPB_vect
21 USART Tx Complete USART_TX_vect
22 ADC Conversion Complete ADC_vect 12 Watchdog Time-out WDT_vect
23 EEPROM Ready EE_READY_vect
24 Analog Comparator ANALOG_COMP_vect 13 USI Start Condition USI_START_vect
25 Two-wire Serial Interface TWI_vect 14 USI Overflow USI_OVF_vect
26 Store Program Memory Read SPM_READY_vect
External Interrupts
True Int vs Pin Change Interrupts
PCINTx, Shares ISR
INTx, individual ISR
Steps to configure the Interrupts:
External Interrupts
1.Set INT1 and INT0 bits in the respective Control Register
Example for ATMEGA328p
On ATTINY
External Interrupts
2. Set the Enable bit in it’s correct register

Bit 1 – INT1: External Interrupt Request 1 Enable

Bit 0 – INT0: External Interrupt Request 0 Enable

3. Write the program to follow on interrupt


ISR(Intx_vect )
{
Interrupt Program
}
On ATTINY

Bit 6 – INT0: External Interrupt Request 0 Enable

Bit 5 – PCIE: Pin Change Interrupt Enable


Example
#include <avr/io.h>
#include <avr/interrupt.h>
void int_init()
{
//BIT 7 6 5 4 3 2 1 0
EICRA|= (1 << ISC10)|(1 << ISC11)|(1 << ISC01);// EICRA: 0 0 0 0 1 1 0 1
//Int0 on fallin and Int1 on rising
EIMSK|= (1 << INT1)|(1 << INT0);//Enable interrupt 1 and 0
}
ISR(INT0_vect)
{
PORTB^=0xFF; //PINS on PORTB ON
}
ISR(INT1_vect)
{
PORTC^=0xFF; //PINS on PORTB OFF
}
int main(void)
{
int_init();
DDRB=0xFF;
DDRC=0xFF;
DDRC=0x00; //INT pins as INPUTS
while (1)
{}
}
PIN Change Interrupts
1. Set the Enable the control register
Example for ATMEGA328p

Bit 2 - PCIE2: Pin Change Interrupt Enable 2


PCINT23..16
Bit 1 – PCIE1: Pin Change Interrupt Enable 1
PCINT14..8
Bit 0 – PCIE0: Pin Change Interrupt Enable 0
PCINT7..0
PIN Change Interrupts 2. Set which pins will act as interruptions
On ATTINY
Activity
• Create a code for 2 infinite led cycles which alternate on the use of an
interruption on a HIGH signal.

• Create a code which has a led blink at 1 sec intervals, using 4 interrupts when
a LOW signal is received make it blink twice at the following intervals:
• 1. 100 ms
• 2. 500 ms 3.
• 1.5 secs
• 4. 2secs

• Create a code to measure time, when an interrupt is activated save the time
passed and light a led the same amount of time passed, with a maximum
time of 6 seconds.

You might also like