Pin Interrupt (INT0 & INT1)

Pin Interrupt (INT0 & INT1)
Çeşme:
- https://github.com/MicrocontrollersAndMore/Atmel_Programming_Tutorial_5_External_Interrupts/blob/master/INT_Pin_Interrupt.c

-
https://www.arduino.cc/en/Reference/AttachInterrupt
https://www.youtube.com/watch?v=bmPKQzpnCeU
  1. Arduino UNO
#define LED_PIN_INTO 12 #define BUTTON_PIN_INT0 2 #define LED_PIN_INT1 11 #define BUTTON_PIN_INT1 3 void setup() { pinMode(LED_PIN_INTO, OUTPUT); pinMode(LED_PIN_INT1, OUTPUT); pinMode(LED_BUILTIN, OUTPUT); pinMode(BUTTON_PIN_INT0, INPUT_PULLUP); digitalWrite(BUTTON_PIN_INT0, HIGH); pinMode(BUTTON_PIN_INT1, INPUT_PULLUP); digitalWrite(BUTTON_PIN_INT1, HIGH); attachInterrupt(digitalPinToInterrupt(BUTTON_PIN_INT0), handle_int0, CHANGE); attachInterrupt(digitalPinToInterrupt(BUTTON_PIN_INT1), handle_int1, CHANGE); } void loop() { digitalWrite(LED_BUILTIN, HIGH); delay(500); digitalWrite(LED_BUILTIN, LOW); delay(500); } void handle_int0() { digitalWrite(LED_PIN_INTO, !digitalRead(BUTTON_PIN_INT0)); } void handle_int1() { digitalWrite(LED_PIN_INT1, !digitalRead(BUTTON_PIN_INT1)); }
#ifndef F_CPU #define F_CPU 1000000UL #endif #include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> #define BIT_IS_SET(byte, bit) (byte & (1 << bit)) #define BIT_IS_CLEAR(byte, bit) (!(byte & (1 << bit))) #define LED_PIN_INTO PB3 #define BUTTON_PIN_INT0 PD2 #define LED_PIN_INT1 PB4 #define BUTTON_PIN_INT1 PD3 #define LED_BLINK_PIN PB5 int main(void) { DDRD &= ~(1 << BUTTON_PIN_INT0); PORTD |= (1 << BUTTON_PIN_INT0); DDRD &= ~(1 << BUTTON_PIN_INT1); PORTD |= (1 << BUTTON_PIN_INT1); DDRB |= (1<<LED_BLINK_PIN)|(1<<PB4)|(1 << PB3); EIMSK = (1<<INT0)|(1<<INT1); EICRA = (1<<ISC00)|(1<<ISC10); sei(); while (1) { PORTB ^= (1 << LED_BLINK_PIN); _delay_ms(500); } return 0; } ISR(INT0_vect) { if(BIT_IS_CLEAR(PIND, BUTTON_PIN_INT0)) { PORTB |= (1 << LED_PIN_INTO); } else if(BIT_IS_SET(PIND, BUTTON_PIN_INT0)) { PORTB &= ~(1 << LED_PIN_INTO); } } ISR(INT1_vect) { if(BIT_IS_CLEAR(PIND, BUTTON_PIN_INT1)) { PORTB |= (1 << LED_PIN_INT1); } else if(BIT_IS_SET(PIND, BUTTON_PIN_INT1)) { PORTB &= ~(1 << LED_PIN_INT1); } }
.def temp = r16 .equ LED_PIN_INTO = 3 .equ BUTTON_PIN_INT0 = 2 .equ LED_PIN_INT1 = 4 .equ BUTTON_PIN_INT1 = 3 .equ LED_BLINK_PIN = 5 jmp start .org INT0addr ; INT0addr is the address of EXT_INT0 jmp handle_pb0 .org INT1addr ; INT1addr is the address of EXT_INT1 jmp handle_pb1 start: ;; enable int0 and int1 ldi temp, (1 << ISC00) // The low level of INT0 generates an interrupt request. ori temp, (1 << ISC10) // The low level of INT1 generates an interrupt request. sts EICRA, temp ;; enable int0 and int1 in temp, EIMSK ori temp, (1<<INT0) // INT0 External Interrupt Request 0 Enable ; Arduino Pin 2 ori temp, (1<<INT1) // INT1 External Interrupt Request 1 Enable ; Arduino Pin 3 out EIMSK, temp sei sbi ddrb, LED_PIN_INTO sbi ddrb, LED_PIN_INT1 sbi ddrb, 5 sbi portd, BUTTON_PIN_INT0 // setup pullup resistor sbi portd, BUTTON_PIN_INT1 // setup pullup resistor loop: sbi portb, LED_BLINK_PIN rcall delay_500ms cbi portb, LED_BLINK_PIN rcall delay_500ms rjmp loop handle_pb0: sbis pind, BUTTON_PIN_INT0 sbi portb, LED_PIN_INTO sbic pind, BUTTON_PIN_INT0 cbi portb, LED_PIN_INTO reti handle_pb1: sbis pind, BUTTON_PIN_INT1 sbi portb, LED_PIN_INT1 sbic pind, BUTTON_PIN_INT1 cbi portb, LED_PIN_INT1 reti delay_500ms: ; the subroutine: ldi r16, 31 ; load r16 with 31 OUTER_LOOP: ; outer loop label ldi r24, 0xfd ; load registers r24:r25 with 1021, our new ; init value ldi r25, 0x03 ; the loop label DELAY_LOOP: ; "add immediate to word": r24:r25 are ; incremented adiw r24, 1 ; if no overflow ("branch if not equal"), go ; back to "delay_loop" brne DELAY_LOOP dec r16 ; decrement r16 brne OUTER_LOOP ; and loop if outer loop not finished ret ; return from subroutine