/* kilroy.c tests int-pin, if LED on when button is pressed - Kilroy was there */ // Arduino UNO R3 // AVR_ATmega328P #define F_CPU 16000000UL #include #include #include /* Press button to toggle LED - proves that you are entering the interrupt routine */ int main() { DDRD &= ~( 1 << PD2 ); // pin 4 PD2 direction is input, Butt (INT0) PORTD |= ( 1 << PD2 ); // Enable pullup resistor for INT0 DDRB |= ( 1 << PB5 ); // pin 13 PB5 direction is output, (LED) PORTB &= ~( 1 << PB5 ); // Make sure LED is off at start setup_extint(); while(1) { _NOP(); // will not be removed by compiler optimization } return 0; } /* *********************************** */ /* INTERRUPTS */ /* *********************************** */ // Interrupt Service Routine attached to INT0 vector ISR( INT0_vect ) { PORTB ^= ( 1 << PB5 ); // Toggle LED on pin 13 (PB5) _delay_ms(200); // wait for contact bounces to settle // Bits in EIFR register: "- - - - - - INTF1 INTF0" EIFR |= ( 1 << INTF0 ); // Clear INTF0 flag probably set by bounces during wait } void setup_extint(void) { // Bits in EIMSK register: "- - - - - - INT1 INT0" EIMSK |= ( 1 << INT0 ); // Local Enable external interrupt INT0 // Bits in EICRA register: "- - - - ISC11 ISC10 ISC01 ISC00" EICRA &= ~( 1 << ISC00 ); // INT0 on falling edge EICRA |= ( 1 << ISC01 ); // INT0 on falling edge sei(); // Global Enable interrupts } /* *********************************** */ /* HARDWARE */ /* *********************************** */ /* Chip ATMega328 Arduino Uno R3 stackable header _______ Digital: _____/ \__ Analog: ______________ ______________ txd ->-|D00 >RXD A5|- | \/ | rxd -<-|D01 -|D02 A3|- rxd -<-(D00)-|02 PD0/RXD SDA/PC4 27|-(A4)- -|D03~ A2|- txd ->-(D01)-|03 PD1/TXD PC3 26|-(A3)- -|D04 A1|- Butt ->-(D02)-|04 PD2/INT0 PC2 25|-(A2)- -|D05~ A0|- -(D03)-|05 PD3/INT1/PWM PC1 24|-(A1)- -|D06~ | Power: -(D04)-|06 PD4 PC0 23|-(A0)- -|D07 Vin|- +5V ---|07 VCC GND 22|--- Gnd | GND|--- GND Gnd ---|08 GND AREF 21|--- Vin -|D08 GND|- Xtal |X|--|09 PB6/OSC1 AVCC 20|--- +5V -|D09~ +5V|--- +5V 16MHz |X|--|10 PB7/OSC2 SCK/PB5 19|-(D13)- -|D10~ +3.3V|- -(D05)-|11 PD5/PWM MISO/PB4 18|-(D12)- -|D11~ Res|- -(D06)-|12 PD6/PWM PWM/MOSI/PB3 17|-(D11)- -|D12 IOREF|- -(D07)-|13 PD7 PWM/SS'/PB2 16|-(D10)- -|D13 LED --- | -(D08)-|14 PB0 PWM/PB1 15|-(D09)- -|GND | |______________________________| -|AREF | -|SCL | -|SDA | |________________| */