Using External Interrupt in AVR

July 25, 2009 by admin · 3 Comments
Filed under: Uncategorized 

External Interrupts in AVR ATmega16/32/8

ATmega16/32 has Three pins which can be used to trigger external hardware interrupt to the controller. While ATmega8 has two external interrupt in it.

As ATmega16 & ATmega32 are pinwise compatible it has facility for external hardware interrupt at these pins.

INT0 – External Interrupt 0 – PIN 16/PORTD2

INT1 – External Interrupt  1 – PIN 17/PORTD3

INT2 – External Interrupt 2 – PIN 3/PORTB2

Suppose we have a task to blink a led whwnever a high to low edge is detected on the corresponding External Interrupt Pin. So when i write the code for that purpose it is said that i am writing an external interrupt service routine or interrupt handeller.

Here is the circuit diagrm for the task…

int0

Below is a description of all the registers related to the interrupts…

1. We must set I (Interrupt) Flag high in Status register taht is SREG to use any kind of interrupts. This flag can be set by software using instruction like SEI or CLI in asm but in ‘C’ we can directly call functions like “sei()” to set I flag and “cli()” to reset I flag.

2. 6th bit of GICR taht is General Interrupt Control Register must keep high to use external interrupt 0 in your application. INT0 bit can be set to one by executing following statment.

GICR=(1<<INT0); // similarly for INT1 or INT2

3. Another very important register is MCUCR (MCU control register). here is a short description of how it affects external interrupt operation.

atmel_mcucr

ISC01                   ISC00                           Description

0                              0                                 Low level of INT0 generates an interrupt

0                              1                                  Any logical change on INT0 generate

1                              0                                 Falling edge of INT0 generates interrupt

1                              1                                  Rising edge of INT0 generates interrupt]

Similar kind of bits are there for INT1 and INT2 in corresponding registers.

Here is the code in WinAVR to handle a external interrupt request.

#include<avr/io.h>
#include<avr/interrupt.h>  // header that defines addresses for INT vect
#include<util/delay.h>
#include<compat/deprecated.h>
int main(void)
{
DDRD=0x00;
DDRA=0xFF;
PORTD=0xFF;   // PIN INT0 initially pulled high
PORTA=0x00;   // LED initially Off
sei();                 // Interrupt flag set
GICR=1<<INT0;   // INT0 enabled
sbi(MCUCR,1);       // falling edge at INT0 generates interrupt
cbi(MCUCR,0);
while(1==1);
return 0 ;
}

SIGNAL(    SIG_INTERRUPT0 ) // SIGNAL is a macro for interrupts
{
PORTA=0xA0;
_delay_ms(200);
PORTA=0x50;
_delay_ms(200);
PORTA=0x00;
}

Similar kind of code can be written for INT1 or INT2. Whatever you want to do on interrupt call just write it inside SIGNAL(int vector){} and that’s it.

Contact-

devesh samaiya

devesh@electroons.compre

Duck Hunt Shooting Game on CRO

July 25, 2009 by admin · 3 Comments
Filed under: Microcontrollers, electronics 

Object Shooting Game on C.R.O. using ATmega32

deveshamaiya and ketan kothari

What Is It ???

This is a very interesting project. You all must have played the game “Duck Hunt” in video game . Here is a circuit that let you play the same shooting game but now the display device is not your television while it is a C.R.O. that is Cathode Ray Oscilloscope.

How to Do It Yourself ???

Now come to the concept part of the design. The basic building blocks of the design are ATmega32 (Atmel AVR Family), an Digital to Analog convertor, and a CRO.

R-2R

The circuit above is a simple R-2R ladder type of Digital to Analog Converter. It has total 8 inputs as digital word input and an analog output from Op-Amp LM358.

The basic control of signal is provided by the controller i.e. ATmega32. mega32 generates digital output from two of its ports which in turn converted into analog voltage using a pair of DAC’s.

circuit1-300x207

In our case we have used buffers to couple microcontroller’s output to the DAC input, this is because while doing experiments we were facing some problems because of unbalance in the R-2R ratio of DAC circuit. Now buffer properly isolates the microcontroller output to the DAC input.For buffer we used 74HCT541 IC you can go for some other also.

To couple the analog output from DAC to the CRO input we used MIC 50ohm cable, and ofcourse CRO needs BNC connectors for input. The most important thing is we are using CRO display in X-Y mode of operation. Each of two DAC generates analog signals corresponding to X and Y input respectively.

Source Code

Download Complete Source Code Compiled using WinAVR here. This source code includes functions to generate object by raster scanning on CRO screen. If you can use this code Do it…but if proved to be a good helper to you do reply me first. Thanks !!!


How it Works ???

Now the most important part, How to make a gun for Shooting and how the system actually works. The behind is very simple, Our gun has a sensor for green light at its tip. Sensor senses the green light and send the feedback to microcontroller. When i press the trigger of gun infront of any moving object on CRO screen, sensor sends a feed back to the microcontroller and microcontroller sounds the buzzer. This is how we detected the duck on the screen. Comparator is used to generate a tuned feed back to the microcontroller.

« Previous PageNext Page »