AVR External Interrupt C Programming

AVR C Programming of External Interrupt


This AVR tutorial looks at how to use the external interrupt of an ATMega8515 microcontroller by completing the task that is describe below. Before continuing with this tutorial it is highly recommended that you go through the tutorial on the AVR 8-bits Microcontroller External Interrupts.


The figure below shows a circuit diagram in which eight (8) LEDs are connected to port C of an ATMega8515 AVR microcontroller. Also there is a push button switch connected between ground and the External Interrupt 0 (INT0)pin (pin 31) of the ATMega8515.


The Task
The task is to write a C program, utilizing the external interrupt of the ATMega8515, that when download to the ATMega8515 in the figure below perform one (1) of two (2) operations. On start-up OPERATION 1 is is executed and continue indefinitely. If the push-button switch is pressed OPERATION 1 is paused and OPERATION 2 is carried out after which OPERATION 1 is resumed.


OPERATION 1 - A roll action is perform using the LEDs. The first LED is lit and roll down to the last LED then back to the first LED. This operation is done continuous.


OPERATION 2 - ALL the LEDs blink five (5) times.


Important: The internal oscillator for the microcontroller must be enabled for the circuit to operate as described. Otherwise the circuit must be modified to include an external oscillator/crystal to clock the microcontroller.
AVR ATMega8515 Microcontroller Interrupt 0 Circuit Example
Code Solution
Below is the AVR C program for this tutorial. Please note that this code was written in AVR Studio 5.
Things to note:
1 - The "avr/interrupt.h" files must be included to use interrupts.
2 - The ISR() function is a general interrupt service routine for all interrupts. This means that you can have several ISR() function in an AVR C program. The argument "INT0_vect" indicate that this ISR is for the External Interrupt 0.
/*
* Written in AVR Studio 5 / AVR Studio 6
* Compiler: AVR GNU C Compiler (GCC)
*
* Author: AVR
* Website: www.projectbandya.com
*/
 
#include <avr/io.h>
#include <avr/interrupt.h>
 
#define F_CPU 4000000UL
#include <util/delay.h>
 
#define DataPort PORTC // Using PortC as our Dataport
#define DataDDR DDRC
 
//Interrupt Service Routine for INT0
ISR(INT0_vect)
{
unsigned char i, temp;
 
_delay_ms(500); // Software debouncing control
 
temp = DataPort; // Save current value on DataPort
 
/* This for loop blink LEDs on Dataport 5 times*/
for(i = 0; i<5; i++)
{
DataPort = 0x00;
_delay_ms(500); // Wait 5 seconds
DataPort = 0xFF;
_delay_ms(500); // Wait 5 seconds
}
 
DataPort = temp; //Restore old value to DataPort
}
 
int main(void)
{
DDRD = 1<<PD2; // Set PD2 as input (Using for interupt INT0)
PORTD = 1<<PD2; // Enable PD2 pull-up resistor
 
DataDDR = 0xFF; // Configure Dataport as output
DataPort = 0x01; // Initialise Dataport to 1
 
GICR = 1<<INT0; // Enable INT0
MCUCR = 1<<ISC01 | 1<<ISC00; // Trigger INT0 on rising edge
 
sei(); //Enable Global Interrupt
 
while(1)
{
if(DataPort >= 0x80)
DataPort = 1;
else
DataPort = DataPort << 1; // Shift to the left
 
_delay_ms(500); // Wait 5 seconds
}
}

projectbandya

No comments:

Post a Comment