Sunday, July 6, 2008

Testing external interrupt INT0 on tiny13

Tiny 13 have an external interrupt "INT0" on PB1, We have created a test project on avr-gcc to have a quick testing on the INT0.



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

unsigned char led=0;

ISR(INT0_vect) {
if (led){
b0_high;
led = 0;
} else {
b0_low;
led = 1;
}
}

int main (void)
{
b1_input; //switch
b0_output; //led output

MCUCR = 1<<isc01; //set INT0 as falling edge trigger

GIMSK = 1<<INT0; //enable INTO in global interrupt mask


sei(); //enable interrupt

while(1) {
;; //interrupt do the rest
}
}



B1 is connected to a switch, and B0 is connected to a LED.
When B1 is pressed, it would toggle the LED On or Off.

Download the whole project : http://labs.bitartist.org/test_int0.tar.gz

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

2 comments:

Anonymous said...

Thanks.

However note that "isc01" should be changed to "ISC01".

Unknown said...

Note that mechanical switches bounce. That is, switch opening or closure is rarely a clean digital state transition. So when you flip the switch, it may actually register open-closed-open-closed-open... multiple times. This would cause your LED to turn on and off multiple times for a single change of the switch state.

There are various debouncing schemes - but a simple addition to this program would be to add a delay in the ISR. Something like _delay_ms(100) should do the trick. If you turn the switch on and off in less than a 10th of a second, it may not register, but otherwise, you should be okay.