All pastes #2061120 Raw Edit

Exercise Mega1280 IRQs

public cpp v1 · immutable
#2061120 ·published 2011-05-16 09:21 UTC
rendered paste body
/* Exercise IRQ triggersThis routine exercises each interrupt & its assigned pin on a Mega1280. It sets output pins Low then High to trigger an interrupt. Only one interruptshould happen per loop (Ei, c. line 69), as only one Input Pin/IRQ is set for input and attached per iteration. That input pin isconnected to a single output pin that changes state only one time to trigger the interrupt. What is observed here, however, is that IRQs areflagged from triggers being sent to pins not directly wired to the IRQ input pin, shown in the serial monitor report in the lines with  "|| Trigger pin"To use this routine, jumper Mega1280 digital IO pins {46,47,48,49,50,51} to IRQ Pins {2,3,18,19,20,21} (e.g.: jumper pin 46 to Pin 2, etc),then upload and run this routine.  The serial monitor will show a table of trigger pins and flagged IRQs (example below).Each line should have only one "Trigger Pin" result, e.g: "| Trigger pin 46 triggered IRQ 0 on pin 2| Ei:Ej=0:0|", and never a "||" resultwhich indicates that two (+) different trigger pins each flagged the interrupt.Example Output (same for three different Mega1280s):====================================Mega1280 Pins & IRQs-> Pin 2:IRQ0, Pin 3:IRQ1, Pin 18:IRQ5, Pin 19:IRQ4, Pin 20:IRQ3, Pin 21:IRQ2.| Trigger pin 46 while wired to pin 2 triggered IRQ 0 on pin 2| Ei:Ej=0:0|| Trigger pin 47 while wired to pin 3 triggered IRQ 1 on pin 3| Ei:Ej=1:1|| Trigger pin 46 while wired to pin 2 triggered IRQ 5 on pin 18| Ei:Ej=2:0|| Trigger pin 48 while wired to pin 18 triggered IRQ 5 on pin 18| Ei:Ej=2:2|| Trigger pin 46 while wired to pin 2 triggered IRQ 4 on pin 19| Ei:Ej=3:0|| Trigger pin 49 while wired to pin 19 triggered IRQ 4 on pin 19| Ei:Ej=3:3|| Trigger pin 46 while wired to pin 2 triggered IRQ 3 on pin 20| Ei:Ej=4:0|| Trigger pin 50 while wired to pin 20 triggered IRQ 3 on pin 20| Ei:Ej=4:4|| Trigger pin 46 while wired to pin 2 triggered IRQ 2 on pin 21| Ei:Ej=5:0|| Trigger pin 51 while wired to pin 21 triggered IRQ 2 on pin 21| Ei:Ej=5:5|====================================*/volatile int IntFlagged = false;		// ISR set flags- volatile vars set by the ISR to flag the polling check to service the encoder.int IRQ_[6]  = {0,1,5,4,3,2};			// interrupt assigned to IRQ_Pin[]int IRQ_Pin[6]  = {2,3,18,19,20,21};		// see arduino schematic for IRQ:PinA assignmentsint TriggerPin[6] = {46,47,48,49,50,51};  	// digital output pins to trigger IRQ//int TriggerPin[6] = {22,23,24,25,26,27};  	// Use only Port A pins for digital outputs to trigger IRQ, must rejumper trigger pins to use, seems to have no effect//int TriggerPin[6] = {41,53,37,36,49,22};	// Use different ports to trigger, must rejumper trigger pins to use, seems to have no effectint TriggerState = LOW;				// initial state on trigger pinsint Ei,Ej,Ek;					// array indexesvoid SetIntFlag() {				// ISR called, set flag    IntFlagged = true;}void Check4IntFlag(void) {			// Check for ISR/IRQ flags, report if flag set.  Should only be one trigger/IRQ reported per line.    if (IntFlagged == true) {        Serial.print("| Trigger pin ");        Serial.print(TriggerPin[Ej],DEC);        Serial.print(" while wired to pin ");        Serial.print(IRQ_Pin[Ej],DEC);        Serial.print(" triggered IRQ ");        Serial.print(IRQ_[Ei],DEC);        Serial.print(" on pin ");        Serial.print(IRQ_Pin[Ei],DEC);        Serial.print("| Ei:Ej=");        Serial.print(Ei,DEC);        Serial.print(":");        Serial.print(Ej,DEC);        Serial.print("|");        IntFlagged = false;    }}void setup(void) {				// make one pass thorugh IRQ test logic    Serial.begin(38400);    delay(500);    interrupts();    Serial.println("Mega1280 Pins & IRQs-> Pin 2:IRQ0, Pin 3:IRQ1, Pin 18:IRQ5, Pin 19:IRQ4, Pin 20:IRQ3, Pin 21:IRQ2.");    for (Ei=0;Ei<6;Ei++) {        detachInterrupt(IRQ_[Ei]); 	  	// first detach all interrupts - clear strays        pinMode(TriggerPin[Ei], OUTPUT);	// set trigger pins {46,47,48,49,50,51} to output        pinMode(IRQ_Pin[Ei], INPUT);      	// set IRQ pins to input        digitalWrite(IRQ_Pin[Ei], HIGH);    	// turn ON pullup resistors, out 5V @ 20K Ohm on pin//        digitalWrite(IRQ_Pin[Ei], LOW);       // turn OFF pullup resistors,out 0v : to use, uncomment and comment out line above    }    for (Ei=0;Ei<6;Ei++) { 			// for all 6 IRQs and IRQ pins, set a current IRQ and Pin to use in Ej loop        Serial.println();			// print a new line on serial monitor        for (Ej=0;Ej<6;Ej++) {			// for all 6 TriggerPins in sequence, attach the interrupt and change state from LOW to HIGH            TriggerState = LOW;			// set the trigger pin to low, then attach the interrupt service to the pin.            digitalWrite(TriggerPin[Ej], TriggerState);            IntFlagged = false;			// clear flag so Check4IntFlag will fail and nothing reported, then attach interrupt.            attachInterrupt(IRQ_[Ei], SetIntFlag, RISING);            TriggerState = !TriggerState;	// now HIGH, will interrupt on rising edge            digitalWrite(TriggerPin[Ej], TriggerState);// trigger irq service, should set IntFlagged to true and be reported by Check4IntFlag().            detachInterrupt(IRQ_[Ei]);		// free the IRQ pin and detach from interrupt service            Check4IntFlag();			// report if IRQ flagged        }    }    Serial.println();    Serial.println("====================================");}void loop(void) {    Check4IntFlag();}