AVR: How to use Interrupt in ATMEGA8.

Filed in AVR , MCU 0 comments

Until now we were using polling to get data from the environment. Polling is one of the techniques used by microcontroller. This technique runs the whole code in a sequence, when the sequence complete it goes back to point zero and the cycle continues.

The second is Interrupt driven. This means that the microcontroller only processes the required code when it is interrupt by some event. This event can be external, e.g. ADC, UART, comparator, SPI, pin change, or internal, e.g. timer.

The best thing about using Interrupt is that it is independent of other coding being processed. Interrupts are on high priority and whenever they occur, the function being processed is paused and the ISR (Interrupt Service Routine) is processed. And afterward the previous function is resumed. Interrupts help to code functions which are required in urgent states such as triggering an alarm or stopping an actuator.

The best example of interrupt is “Ctrl+Alt+Del” function in Computers. It is at the highest priority and pause everything being carried before its execution.
We will use interrupt to read the temperature and then control some LEDs. There are many sensors available in the market through which we can easily read the temperature. Two of them are LM35 and LM36. LM35 measure the temperature in Celsius while LM36 in Fahrenheit. Both of them output voltage which we can read and perform the desired function.

The circuit for this application is given below:
avr1

The above circuit shows a LM35 temp sensor attached to ADC0. When the temp is less than 24°C , D1 lights up, if temp is greater than 24°C and less than 32°C D2 lights up and if temp is greater than 32°C, D3 lights up. The capacitor is added for filtration at AVcc.

The code is given below:

#include<avr\io.h>

#include<avr\interrupt.h>

ISR(SIG_ADC)

{

if(ADCH<12)

{

PORTB&=~((1<<PB1)|(1<<PB2));

PORTB|=(1<<PB0);

}

else if(ADCH>12 && ADCH<17)

{

PORTB&=~((1<<PB0)|(1<<PB2));

PORTB|=(1<<PB1);

}

else if(ADCH>17)

{

PORTB&=~((1<<PB0)|(1<<PB1));

PORTB|=(1<<PB2);

}

ADCSRA|=(1<<ADSC); //start conversion

}

int main()

{

DDRB=(1<<DDB0)|(1<<DDB1)|(1<<DDB2);

ADMUX=(1<<REFS0)|(1<<ADLAR); //Make AVcc the reference and select ADC0 channel.

ADCSRA=(1<<ADEN)|(1<<ADFR)|(1<<ADIE)|(1<<ADPS2); //Enable Analog convertor,

//set prescaler to 16

ADCSRA|=(1<<ADSC); //start conversion

sei();  //start interrupt

while(1);

return 1;

}

The above code contains three new things. First, we included interrupt.h, secondly, the sei() command and lastly ISR.

sei means ‘Set Interrupt’. Whenever we want to use an interrupt we have to call this command. ISR stands for Interrupt service routine. Whenever an interrupt is executed the program automatically jumps to its specific interrupt.  In this program we have executed the ADC interrupt and performed some operation.

The ADIE bit in ADCSRA register enables the ADC interrupt. After this we have to start the ADC for the first time to make the first conversion. An infinite loop is initiated so the program never exits.

The ADC condition values are calculated by the following formula:

avr2

Don’t be scared by the length of the formula! If you checkout the LM35 datasheet you will see that voltage change is 10mV/C. the maximum temperature is 150C and minimum is -55C. Therefore, the highest voltage which can be measured is 150*10mV=1.5V. Similarly, as we are only using ADCH for taking data so the highest value than can be measure is 256(dec) or 0xFF(hex). If you combine these to facts on a paper you will get the above equation. The table below might help you:

Temperature Decimal Value
10 C 5
20 C 10
40 C 20

You might be thinking, “what the use of an interrupt routine anyways?”Practically speaking in this case we don’t need an interrupt. But consider a case, where we have to simultaneous receive and send data over the PC. We would be discussing this in the next article.

Posted by hamzaazeem   @   11 October 2009 0 comments
Tags : , , ,

Share This Post

RSS Digg Twitter StumbleUpon Delicious Technorati

0 Comments

blog comments powered by Disqus
Previous Post
«
Next Post
»
EXULT designed by ZENVERSE  |  In conjunction with Reseller Hosting from the #1 Web Hosting Provider - HostNexus.