Anda di halaman 1dari 5

Tutorial No.

: 6
Tutorial Name: External-Interrupt



Content: page
1. Bill of Materials --------------------------------------------------- 3
2. Circuit connection------------------------------------------------- 4
2.1 Proteus components------------------------------------------4
3. Code----------------------------------------------------------------- 5

1-Bill of Materials:
# Name Quantity
1 ATmega16A-pu 40pin 1
2 Crystal Oscillator 8MHz 1
3 Resistance 10K 1
4 Resistance 330 3
5 LED (any color) 3
6 22pF Capacitor 2
7 Push Button 1

External Interrupt:
ATmega16A is equipped to handle a 21 interrupt sources. Three of the interrupts originate from
external interrupt sources INT0, INT1, and INT2, if enabled, the interrupts will trigger even if
the INT0:2 pins are configured as outputs.
Note: check Reset and Interrupt Vectors table in Datasheet to see the priority of the ext. interrupt
The external interrupts can be triggered by a falling or rising edge or a low level (INT2 is only an
edge triggered interrupt). When the external interrupt is enabled and is configured as level
triggered (onlyINT0/INT1), the interrupt will trigger as long as the pin is held low.
We will use in this tutorial two registers. The MCU Control Register MCUCR to set the
interrupt working condition, and the General Interrupt Control Register GICR to Enable or
Disable the interrupt set or clear the last three bits in this register.








2- Circuit Connection:













2.1 Proteus components:
1. ATmega16.
2. Resistance.
3. Capacitor.
4. Crystal.
5. LED.
6. Button.


3- Code:


#ifndef F_CPU
#define F_CPU 8000000UL // "set the clock to 8MHz"
#endif


#include <avr/io.h> //io.h is AVR ports Header file
#include <util/delay.h> //delay.h is Delay Function Header file
#include <avr/iom16.h> //iom16.h is atmega16 ports Header file
#include <avr/interrupt.h> //interrupt.h is interrupt Header file

ISR(INT0_vect){ // Interrupt routine ()

unsigned char i,temp; // declare two variables 'i' used in for loop
// and temp to to save the PORTC state before the interrupt
temp=PORTC; //save current state of PORTC in temp variable
PORTC=0x0f; //set PORTC to 0b00001111 to start blinking sequence
_delay_ms(100); // delay 1st blink

for (i=0;i<=3;i++) // loop to continue the sequence
{ PORTC^=0xff; // xor mask PORTC with ones to switch the led state and
make the blink
_delay_ms(100); // delay to notice the blink effect
}

PORTC=temp; // Retrieve the state of PORTC before the interrupt
}

int main(void)
{
DDRC=0xff; // SET ALL PORTC pins as Output
PORTC=0b00000100; // Initialize PC2 to one to start shift from led1
DDRD&=0xFB; // Set PD2 as input
PORTD=0x04; // Enable pull-up resistor
GICR=1<<INT0; // Enable INT0 in General interrupt control reg. 1
MCUCR=0x00; // trigger the interrupt when switch is set to Low level ```|_____0

// GIFR=1<<INTF0; //General interrupt flag reg.(used when pull-up resistor is disabled)

sei(); //set global interrupt enable


while(1)
{
if (PORTC==0x04) //check if PORTC has one on pin PD2
PORTC=0x01; //set one on PD0

else //otherwise
PORTC=PORTC<<1; // shift left one through the PORTC to make leds blink one by one
_delay_ms(100); // delay for 100ms


}
}

Anda mungkin juga menyukai