Last active
March 12, 2016 22:05
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Investigates interrupts from two switches | |
* In this case, both switches connect to the same port | |
* There is a switch at PC4 and PC5 | |
* 12/6/2015 | |
*/ | |
/*----------------------------Global Variables-----------------------------*/ | |
volatile boolean foundInterrupt; | |
volatile byte data; | |
byte datamask = 0b00110000; // Switches on PC4 and PC5 | |
/*-----------------------------Interrupt Service Routine----------------*/ | |
ISR (PCINT1_vect){ //Exactly like this for interrupts on PC0 - PC5 | |
data = PINC & datamask; | |
PCMSK1 = 0; //Disables the interrupt to avoid further interrupts | |
//Avoids the interrupt when swich is closed again | |
foundInterrupt = true; //Lets other functions know that interrupt occured | |
} | |
/*___________________________________SetUp__________________________________*/ | |
void setup() { | |
Serial.begin(9600); | |
DDRC = 0; //PC0 - PC5 are inputs | |
PORTC = 0b00110000; // Enable Pull-up resistors for switchs at PC4 and PC5 | |
//Setup Interrupt | |
PCICR = 0b00000010; //Enables interrupts for PC0 - PC5 | |
PCMSK1 = 0b00110000; //further refines interrupt selection to PC4 and PC5 | |
foundInterrupt = false; | |
while(1){ | |
delay(2000); | |
Serial.println("Checking For Interrupt"); | |
if (foundInterrupt){ //If either switch is pressed ISR will set this true | |
if (data == 0b00010000){ | |
Serial.println("\tRed Switch Pressed"); | |
} | |
else if (data == 0b00100000){ | |
Serial.println("\tGreen Switch Pressed"); | |
} | |
} | |
foundInterrupt = false; //This is made true in ISR when interrupts occur | |
data = PINC & datamask; | |
if (data == 0b00110000){ | |
PCMSK1 = 0b00110000; //Enables interrupts again. Disabled in ISR | |
} | |
} | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment