Interrupts Code Examples
This page lists two code examples for interrupts.
The wiki is structured this way (central compilation instead of code examples on each page) because it is:
- Easier to maintain code examples and catch mistakes this way instead of asking experts to divide attention across scattered pages
- Easier to link and maintain code examples that involve multiple registers to each register this way
- Informative for the beginning reader, who would benefit from such detailed examples, to see the different options
- Each individual page is kept short and succinct, as an expert user might want it
On each individual page, [[Template: Interrupts Code Examples Link{{PAGENAME}}]] is used to link to this compilation page.
This page consists solely of two code examples: Example Code One and Example Code Two.
Contents |
[edit] External Interrupts Example Code One
[edit] PCMSK0
This example code increments two counters each time two photodiodes change pin states (from high to low, or low to high) respectively. The photodiodes are connected to hardware interrupt pins PCINT0 and PCINT1 respectively. Case 1B and Case 1C are given are theoretical examples for robots with sensors connected to (pins 8,9,10) and pin 16 respectively and are used for the PCMSK1 and PCMSK2 documentation.
[edit] Physical Setup
A 40 kHz IR receiver is connect to pin IR_IN, aka PCINT0. The code below does not include the 40 kHz IR emitter code that triggers the IR receiver pin changes. See DFRobot for a full example code file.
More concretely, this code is for the DFrobot MiniQ 2wd robot.
- Toolchain: Arduino IDE
- Board: Nano w/ Atmega328
- Microcontroller: Atmega328p
[edit] Example Code 1A: PCMSK0
int count; void setup() { Serial.begin(9600); pcint0_init(); sei(); // global interrupt enable } void pcint0_init(void) { PCICR = 0x01; //Allow interrupts for pins PCINT7 through PCINT0, disallow for all other pins PCMSK0 = 0x01; //Enable interrupts specifically for pin PCINT0 (set rightmost bit to 1), disable the rest } ISR(PCINT0_vect) { count++; } void loop() { Serial.println(count); delay(100); }
[edit] Example Code 1B: PCMSK1
int count; void setup() { Serial.begin(9600); pcint1_init(); sei(); // global interrupt enable } void pcint1_init(void) { PCICR = 0x02 // binary 10, aka 0b00000010. Allow interrupts for pins PCINT14 through PCINT8, disallow for all other pins PCMSK1 = 0b00000111 // binary 111, aka hexadecimal 0x07. Enable interrupts specifically for pins PCINT8, 9, and 10 (any change will trigger PCINT1_vect). } ISR(PCINT1_vect) { count++; } void loop() { Serial.println(count); delay(100); }
[edit] Example Code 1C: PCMSK2
Similarly, now for PCINT2_vect and pins PCINT23 to PCINT16,
void pcint2_init(void) { PCICR = 0x04 // binary 0b00000100. Allow interrupts for pins PCINT23 through PCINT16, disallow for all other pins PCMSK2 = 0x01 // binary 0b00000001. Enable interrupts specifically for pins PCINT16 (any change will trigger PCINT2_vect), disable other pins }
[[Top]]
[edit] PCMSK1
This example code increments two counters each time two photodiodes change pin states (from high to low, or low to high) respectively. The photodiodes are connected to hardware interrupt pins PCINT0 and PCINT1 respectively. Case 1B and Case 1C are given are theoretical examples for robots with sensors connected to (pins 8,9,10) and pin 16 respectively and are used for the PCMSK1 and PCMSK2 documentation.
[edit] Physical Setup
A 40 kHz IR receiver is connect to pin IR_IN, aka PCINT0. The code below does not include the 40 kHz IR emitter code that triggers the IR receiver pin changes. See DFRobot for a full example code file.
More concretely, this code is for the DFrobot MiniQ 2wd robot.
- Toolchain: Arduino IDE
- Board: Nano w/ Atmega328
- Microcontroller: Atmega328p
[edit] Example Code 1A: PCMSK0
int count; void setup() { Serial.begin(9600); pcint0_init(); sei(); // global interrupt enable } void pcint0_init(void) { PCICR = 0x01; //Allow interrupts for pins PCINT7 through PCINT0, disallow for all other pins PCMSK0 = 0x01; //Enable interrupts specifically for pin PCINT0 (set rightmost bit to 1), disable the rest } ISR(PCINT0_vect) { count++; } void loop() { Serial.println(count); delay(100); }
[edit] Example Code 1B: PCMSK1
int count; void setup() { Serial.begin(9600); pcint1_init(); sei(); // global interrupt enable } void pcint1_init(void) { PCICR = 0x02 // binary 10, aka 0b00000010. Allow interrupts for pins PCINT14 through PCINT8, disallow for all other pins PCMSK1 = 0b00000111 // binary 111, aka hexadecimal 0x07. Enable interrupts specifically for pins PCINT8, 9, and 10 (any change will trigger PCINT1_vect). } ISR(PCINT1_vect) { count++; } void loop() { Serial.println(count); delay(100); }
[edit] Example Code 1C: PCMSK2
Similarly, now for PCINT2_vect and pins PCINT23 to PCINT16,
void pcint2_init(void) { PCICR = 0x04 // binary 0b00000100. Allow interrupts for pins PCINT23 through PCINT16, disallow for all other pins PCMSK2 = 0x01 // binary 0b00000001. Enable interrupts specifically for pins PCINT16 (any change will trigger PCINT2_vect), disable other pins }
[[Top]]
[edit] PCMSK2
This example code increments two counters each time two photodiodes change pin states (from high to low, or low to high) respectively. The photodiodes are connected to hardware interrupt pins PCINT0 and PCINT1 respectively. Case 1B and Case 1C are given are theoretical examples for robots with sensors connected to (pins 8,9,10) and pin 16 respectively and are used for the PCMSK1 and PCMSK2 documentation.
[edit] Physical Setup
A 40 kHz IR receiver is connect to pin IR_IN, aka PCINT0. The code below does not include the 40 kHz IR emitter code that triggers the IR receiver pin changes. See DFRobot for a full example code file.
More concretely, this code is for the DFrobot MiniQ 2wd robot.
- Toolchain: Arduino IDE
- Board: Nano w/ Atmega328
- Microcontroller: Atmega328p
[edit] Example Code 1A: PCMSK0
int count; void setup() { Serial.begin(9600); pcint0_init(); sei(); // global interrupt enable } void pcint0_init(void) { PCICR = 0x01; //Allow interrupts for pins PCINT7 through PCINT0, disallow for all other pins PCMSK0 = 0x01; //Enable interrupts specifically for pin PCINT0 (set rightmost bit to 1), disable the rest } ISR(PCINT0_vect) { count++; } void loop() { Serial.println(count); delay(100); }
[edit] Example Code 1B: PCMSK1
int count; void setup() { Serial.begin(9600); pcint1_init(); sei(); // global interrupt enable } void pcint1_init(void) { PCICR = 0x02 // binary 10, aka 0b00000010. Allow interrupts for pins PCINT14 through PCINT8, disallow for all other pins PCMSK1 = 0b00000111 // binary 111, aka hexadecimal 0x07. Enable interrupts specifically for pins PCINT8, 9, and 10 (any change will trigger PCINT1_vect). } ISR(PCINT1_vect) { count++; } void loop() { Serial.println(count); delay(100); }
[edit] Example Code 1C: PCMSK2
Similarly, now for PCINT2_vect and pins PCINT23 to PCINT16,
void pcint2_init(void) { PCICR = 0x04 // binary 0b00000100. Allow interrupts for pins PCINT23 through PCINT16, disallow for all other pins PCMSK2 = 0x01 // binary 0b00000001. Enable interrupts specifically for pins PCINT16 (any change will trigger PCINT2_vect), disable other pins }
[[Top]]
[edit] PCICR
This example code increments two counters each time two photodiodes change pin states (from high to low, or low to high) respectively. The photodiodes are connected to hardware interrupt pins PCINT0 and PCINT1 respectively. Case 1B and Case 1C are given are theoretical examples for robots with sensors connected to (pins 8,9,10) and pin 16 respectively and are used for the PCMSK1 and PCMSK2 documentation.
[edit] Physical Setup
A 40 kHz IR receiver is connect to pin IR_IN, aka PCINT0. The code below does not include the 40 kHz IR emitter code that triggers the IR receiver pin changes. See DFRobot for a full example code file.
More concretely, this code is for the DFrobot MiniQ 2wd robot.
- Toolchain: Arduino IDE
- Board: Nano w/ Atmega328
- Microcontroller: Atmega328p
[edit] Example Code 1A: PCMSK0
int count; void setup() { Serial.begin(9600); pcint0_init(); sei(); // global interrupt enable } void pcint0_init(void) { PCICR = 0x01; //Allow interrupts for pins PCINT7 through PCINT0, disallow for all other pins PCMSK0 = 0x01; //Enable interrupts specifically for pin PCINT0 (set rightmost bit to 1), disable the rest } ISR(PCINT0_vect) { count++; } void loop() { Serial.println(count); delay(100); }
[edit] Example Code 1B: PCMSK1
int count; void setup() { Serial.begin(9600); pcint1_init(); sei(); // global interrupt enable } void pcint1_init(void) { PCICR = 0x02 // binary 10, aka 0b00000010. Allow interrupts for pins PCINT14 through PCINT8, disallow for all other pins PCMSK1 = 0b00000111 // binary 111, aka hexadecimal 0x07. Enable interrupts specifically for pins PCINT8, 9, and 10 (any change will trigger PCINT1_vect). } ISR(PCINT1_vect) { count++; } void loop() { Serial.println(count); delay(100); }
[edit] Example Code 1C: PCMSK2
Similarly, now for PCINT2_vect and pins PCINT23 to PCINT16,
void pcint2_init(void) { PCICR = 0x04 // binary 0b00000100. Allow interrupts for pins PCINT23 through PCINT16, disallow for all other pins PCMSK2 = 0x01 // binary 0b00000001. Enable interrupts specifically for pins PCINT16 (any change will trigger PCINT2_vect), disable other pins }
[[Top]]
[edit] PCINT0_vect
This example code increments two counters each time two photodiodes change pin states (from high to low, or low to high) respectively. The photodiodes are connected to hardware interrupt pins PCINT0 and PCINT1 respectively. Case 1B and Case 1C are given are theoretical examples for robots with sensors connected to (pins 8,9,10) and pin 16 respectively and are used for the PCMSK1 and PCMSK2 documentation.
[edit] Physical Setup
A 40 kHz IR receiver is connect to pin IR_IN, aka PCINT0. The code below does not include the 40 kHz IR emitter code that triggers the IR receiver pin changes. See DFRobot for a full example code file.
More concretely, this code is for the DFrobot MiniQ 2wd robot.
- Toolchain: Arduino IDE
- Board: Nano w/ Atmega328
- Microcontroller: Atmega328p
[edit] Example Code 1A: PCMSK0
int count; void setup() { Serial.begin(9600); pcint0_init(); sei(); // global interrupt enable } void pcint0_init(void) { PCICR = 0x01; //Allow interrupts for pins PCINT7 through PCINT0, disallow for all other pins PCMSK0 = 0x01; //Enable interrupts specifically for pin PCINT0 (set rightmost bit to 1), disable the rest } ISR(PCINT0_vect) { count++; } void loop() { Serial.println(count); delay(100); }
[edit] Example Code 1B: PCMSK1
int count; void setup() { Serial.begin(9600); pcint1_init(); sei(); // global interrupt enable } void pcint1_init(void) { PCICR = 0x02 // binary 10, aka 0b00000010. Allow interrupts for pins PCINT14 through PCINT8, disallow for all other pins PCMSK1 = 0b00000111 // binary 111, aka hexadecimal 0x07. Enable interrupts specifically for pins PCINT8, 9, and 10 (any change will trigger PCINT1_vect). } ISR(PCINT1_vect) { count++; } void loop() { Serial.println(count); delay(100); }
[edit] Example Code 1C: PCMSK2
Similarly, now for PCINT2_vect and pins PCINT23 to PCINT16,
void pcint2_init(void) { PCICR = 0x04 // binary 0b00000100. Allow interrupts for pins PCINT23 through PCINT16, disallow for all other pins PCMSK2 = 0x01 // binary 0b00000001. Enable interrupts specifically for pins PCINT16 (any change will trigger PCINT2_vect), disable other pins }
[[Top]]
[edit] PCINT1_vect
This example code increments two counters each time two photodiodes change pin states (from high to low, or low to high) respectively. The photodiodes are connected to hardware interrupt pins PCINT0 and PCINT1 respectively. Case 1B and Case 1C are given are theoretical examples for robots with sensors connected to (pins 8,9,10) and pin 16 respectively and are used for the PCMSK1 and PCMSK2 documentation.
[edit] Physical Setup
A 40 kHz IR receiver is connect to pin IR_IN, aka PCINT0. The code below does not include the 40 kHz IR emitter code that triggers the IR receiver pin changes. See DFRobot for a full example code file.
More concretely, this code is for the DFrobot MiniQ 2wd robot.
- Toolchain: Arduino IDE
- Board: Nano w/ Atmega328
- Microcontroller: Atmega328p
[edit] Example Code 1A: PCMSK0
int count; void setup() { Serial.begin(9600); pcint0_init(); sei(); // global interrupt enable } void pcint0_init(void) { PCICR = 0x01; //Allow interrupts for pins PCINT7 through PCINT0, disallow for all other pins PCMSK0 = 0x01; //Enable interrupts specifically for pin PCINT0 (set rightmost bit to 1), disable the rest } ISR(PCINT0_vect) { count++; } void loop() { Serial.println(count); delay(100); }
[edit] Example Code 1B: PCMSK1
int count; void setup() { Serial.begin(9600); pcint1_init(); sei(); // global interrupt enable } void pcint1_init(void) { PCICR = 0x02 // binary 10, aka 0b00000010. Allow interrupts for pins PCINT14 through PCINT8, disallow for all other pins PCMSK1 = 0b00000111 // binary 111, aka hexadecimal 0x07. Enable interrupts specifically for pins PCINT8, 9, and 10 (any change will trigger PCINT1_vect). } ISR(PCINT1_vect) { count++; } void loop() { Serial.println(count); delay(100); }
[edit] Example Code 1C: PCMSK2
Similarly, now for PCINT2_vect and pins PCINT23 to PCINT16,
void pcint2_init(void) { PCICR = 0x04 // binary 0b00000100. Allow interrupts for pins PCINT23 through PCINT16, disallow for all other pins PCMSK2 = 0x01 // binary 0b00000001. Enable interrupts specifically for pins PCINT16 (any change will trigger PCINT2_vect), disable other pins }
[[Top]]
[edit] PCINT2_vect
This example code increments two counters each time two photodiodes change pin states (from high to low, or low to high) respectively. The photodiodes are connected to hardware interrupt pins PCINT0 and PCINT1 respectively. Case 1B and Case 1C are given are theoretical examples for robots with sensors connected to (pins 8,9,10) and pin 16 respectively and are used for the PCMSK1 and PCMSK2 documentation.
[edit] Physical Setup
A 40 kHz IR receiver is connect to pin IR_IN, aka PCINT0. The code below does not include the 40 kHz IR emitter code that triggers the IR receiver pin changes. See DFRobot for a full example code file.
More concretely, this code is for the DFrobot MiniQ 2wd robot.
- Toolchain: Arduino IDE
- Board: Nano w/ Atmega328
- Microcontroller: Atmega328p
[edit] Example Code 1A: PCMSK0
int count; void setup() { Serial.begin(9600); pcint0_init(); sei(); // global interrupt enable } void pcint0_init(void) { PCICR = 0x01; //Allow interrupts for pins PCINT7 through PCINT0, disallow for all other pins PCMSK0 = 0x01; //Enable interrupts specifically for pin PCINT0 (set rightmost bit to 1), disable the rest } ISR(PCINT0_vect) { count++; } void loop() { Serial.println(count); delay(100); }
[edit] Example Code 1B: PCMSK1
int count; void setup() { Serial.begin(9600); pcint1_init(); sei(); // global interrupt enable } void pcint1_init(void) { PCICR = 0x02 // binary 10, aka 0b00000010. Allow interrupts for pins PCINT14 through PCINT8, disallow for all other pins PCMSK1 = 0b00000111 // binary 111, aka hexadecimal 0x07. Enable interrupts specifically for pins PCINT8, 9, and 10 (any change will trigger PCINT1_vect). } ISR(PCINT1_vect) { count++; } void loop() { Serial.println(count); delay(100); }
[edit] Example Code 1C: PCMSK2
Similarly, now for PCINT2_vect and pins PCINT23 to PCINT16,
void pcint2_init(void) { PCICR = 0x04 // binary 0b00000100. Allow interrupts for pins PCINT23 through PCINT16, disallow for all other pins PCMSK2 = 0x01 // binary 0b00000001. Enable interrupts specifically for pins PCINT16 (any change will trigger PCINT2_vect), disable other pins }
[[Top]]
[edit] External Interrupts Example Code Two
[edit] EICRA
This example code increments two counters each time two photodiodes change pin states (from high to low, or low to high) respectively. The photodiodes are connected to hardware interrupt pins INT0 and INT1 respectively.
[edit] Physical Setup
Two photodiodes are connected to pins INT0 and INT1. More concretely, this code is for the DFrobot MiniQ 2wd robot.
- Toolchain: Arduino IDE
- Board: Nano w/ Atmega328
- Microcontroller: Atmega328p
[edit] Example Code Two
int count_r=0, count_l=0; void interrupt01_init(void) { EICRA = 0X0F; // in binary, 1111. That is, the rising edge of INT1 and INT0 generate an interrupt request. EIMSK = 0X03; // in binary, 0011. Enables INT0 and INT1 interrupts. } ISR(INT0_vect) { count_r++; } ISR(INT1_vect) { count_l++; } void setup(){ Serial.begin(9600); interrupt01_init(); } void loop(){ Serial.print(count_l); Serial.print(", "); Serial.println(count_r); delay(100); }
[edit] EIMSK
This example code increments two counters each time two photodiodes change pin states (from high to low, or low to high) respectively. The photodiodes are connected to hardware interrupt pins INT0 and INT1 respectively.
[edit] Physical Setup
Two photodiodes are connected to pins INT0 and INT1. More concretely, this code is for the DFrobot MiniQ 2wd robot.
- Toolchain: Arduino IDE
- Board: Nano w/ Atmega328
- Microcontroller: Atmega328p
[edit] Example Code Two
int count_r=0, count_l=0; void interrupt01_init(void) { EICRA = 0X0F; // in binary, 1111. That is, the rising edge of INT1 and INT0 generate an interrupt request. EIMSK = 0X03; // in binary, 0011. Enables INT0 and INT1 interrupts. } ISR(INT0_vect) { count_r++; } ISR(INT1_vect) { count_l++; } void setup(){ Serial.begin(9600); interrupt01_init(); } void loop(){ Serial.print(count_l); Serial.print(", "); Serial.println(count_r); delay(100); }
[edit] INT0_vect
This example code increments two counters each time two photodiodes change pin states (from high to low, or low to high) respectively. The photodiodes are connected to hardware interrupt pins INT0 and INT1 respectively.
[edit] Physical Setup
Two photodiodes are connected to pins INT0 and INT1. More concretely, this code is for the DFrobot MiniQ 2wd robot.
- Toolchain: Arduino IDE
- Board: Nano w/ Atmega328
- Microcontroller: Atmega328p
[edit] Example Code Two
int count_r=0, count_l=0; void interrupt01_init(void) { EICRA = 0X0F; // in binary, 1111. That is, the rising edge of INT1 and INT0 generate an interrupt request. EIMSK = 0X03; // in binary, 0011. Enables INT0 and INT1 interrupts. } ISR(INT0_vect) { count_r++; } ISR(INT1_vect) { count_l++; } void setup(){ Serial.begin(9600); interrupt01_init(); } void loop(){ Serial.print(count_l); Serial.print(", "); Serial.println(count_r); delay(100); }
[edit] INT1_vect
This example code increments two counters each time two photodiodes change pin states (from high to low, or low to high) respectively. The photodiodes are connected to hardware interrupt pins INT0 and INT1 respectively.
[edit] Physical Setup
Two photodiodes are connected to pins INT0 and INT1. More concretely, this code is for the DFrobot MiniQ 2wd robot.
- Toolchain: Arduino IDE
- Board: Nano w/ Atmega328
- Microcontroller: Atmega328p
[edit] Example Code Two
int count_r=0, count_l=0; void interrupt01_init(void) { EICRA = 0X0F; // in binary, 1111. That is, the rising edge of INT1 and INT0 generate an interrupt request. EIMSK = 0X03; // in binary, 0011. Enables INT0 and INT1 interrupts. } ISR(INT0_vect) { count_r++; } ISR(INT1_vect) { count_l++; } void setup(){ Serial.begin(9600); interrupt01_init(); } void loop(){ Serial.print(count_l); Serial.print(", "); Serial.println(count_r); delay(100); }