AVR Programming: Interrupts September 17, 2010. What are interrupts? An asynchronous signal...

9
AVR Programming: Interrupts September 17, 2010

Transcript of AVR Programming: Interrupts September 17, 2010. What are interrupts? An asynchronous signal...

Page 1: AVR Programming: Interrupts September 17, 2010. What are interrupts? An asynchronous signal indicating the need for an event to be handled A synchronous.

AVR Programming:

Interrupts

September 17, 2010

Page 2: AVR Programming: Interrupts September 17, 2010. What are interrupts? An asynchronous signal indicating the need for an event to be handled A synchronous.

What are interrupts?

An asynchronous signal indicating the need for an event to be handled

A synchronous event in software indicating the need for a change in execution

Temporarily halts program execution to enter an interrupt service routine (similar to a function but with a few key differences)

2

Page 3: AVR Programming: Interrupts September 17, 2010. What are interrupts? An asynchronous signal indicating the need for an event to be handled A synchronous.

Why interrupts?

3

Allows your program to wait for an event without blocking

Allows you to respond to special events in a time sensitive manner

Cleans up your behavior code from constant polling steps

Page 4: AVR Programming: Interrupts September 17, 2010. What are interrupts? An asynchronous signal indicating the need for an event to be handled A synchronous.

What happens on an interrupt?

Program state (register data, status flags, program counter etc.) is pushed to the stack

The program counter jumps to a predefined memory address (interrupt vector) that contains one instruction : jmp $XXXX

XXXX is the hex value of the address of the first line of the interrupt handler

On return all state data (including program counter) is popped off the stack into registers again fully restoring the original program 4

Page 5: AVR Programming: Interrupts September 17, 2010. What are interrupts? An asynchronous signal indicating the need for an event to be handled A synchronous.

What different interrupts exist?

ATmega128 defines 35 vectors

Various things from timers to serial to analog conversion

Right now we’ll use external interrupts

There are 8 of them Shown as INTn

5

Page 6: AVR Programming: Interrupts September 17, 2010. What are interrupts? An asynchronous signal indicating the need for an event to be handled A synchronous.

How to enable these interrupts

6

Use SEI to enable all interrupt and CLI to disable all interrupts

Page 7: AVR Programming: Interrupts September 17, 2010. What are interrupts? An asynchronous signal indicating the need for an event to be handled A synchronous.

7

How does this look in code?

#include <avr/interrupt.h>#include <avr/io.h>

volatile char externalValue; //a value that can be updated externally to the behavior code

int main(void){int x;externalValue = 0;DDRC |= _BV(PC1) | _BV(PC5) | _BV(PC0) | _BV(PC4);DDRE &= ~_BV(PE7);EICRB |= _BV(ISC70); //any logical change triggers interruptEIMSK |= _BV(INT7); //enable external interrupt 7sei(); //global interrupt enablewhile(1){

for(x = 0; x<3; x++){if(externalValue){

PORTC &= ~(_BV(PC1) | _BV(PC5)); //turn lights green}else{

PORTC &= ~(_BV(PC0) | _BV(PC4)); //turn lights red}

}PORTC |= _BV(PC1) | _BV(PC5) | _BV(PC0) | _BV(PC4); //turn lights off

}}

ISR(INT7_vect){externalValue = (PINE>>7) & 0x01; //read pin E

}

Page 8: AVR Programming: Interrupts September 17, 2010. What are interrupts? An asynchronous signal indicating the need for an event to be handled A synchronous.

8

Stuff to try out

Look at other interrupts if you want External interrupts can be triggered in software

– Output values on the interrupt’s digital I/O port Figure out how to connect a push button to

one of the interrupt pins Combine with what you’ve learned about DIO You should not need the dragonfly library

Page 9: AVR Programming: Interrupts September 17, 2010. What are interrupts? An asynchronous signal indicating the need for an event to be handled A synchronous.

9

Help/More Info

AVR Interrupts Reference:http://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html

Datasheet:http://www.atmel.com/dyn/resources/prod_documents/doc2467.pdf