/*****************************************************

	Samples\Intrupts\EXTINT_300O.C
	Z-World, 2002

	This program demonstrates how to use the external
	interrupt lines to trigger an interrupt
	service routine (ISR) in your code for the
	Rabbit 3000 CPU.  Unlike the Rabbit 2000 CPU, both
	external interrupts are available for use on the
	Rabbit 3000.

	Two input lines should be attached to parallel port
	E pins 0 and 1 for this example.  The program will
	separately count pulses on both input line until
	20 pulses are counted on one of them.

   The "interrupt" keyword is used to create an ISR in C code.

******************************************************/
#class auto

// Set equal to 1 to use fast interrupt in I&D space
#define FAST_INTERRUPT 0

char count0, count1;

void my_isr0();
void my_isr1();

void main()
{
	count0 = 0;
	count1 = 0;

	WrPortI(PEDDR, &PEDDRShadow, 0x00);	// set port E as all inputs

#if __SEPARATE_INST_DATA__ && FAST_INTERRUPT
	interrupt_vector ext0_intvec my_isr0;
	interrupt_vector ext1_intvec my_isr1;
#else
	SetVectExtern3000(0, my_isr0);
	SetVectExtern3000(1, my_isr1);
   // re-setup ISR's to show example of retrieving ISR address using GetVectExtern3000
	SetVectExtern3000(0, GetVectExtern3000(0));
	SetVectExtern3000(1, GetVectExtern3000(1));
#endif

	WrPortI(I0CR, &I0CRShadow, 0x09);		// enable external INT0 on PE0, rising edge, priority 1
	WrPortI(I1CR, &I1CRShadow, 0x09);		// enable external INT1 on PE1, rising edge, priority 1


	while ((count0 < 20) && (count1 < 20)) {

		// output the interrupt count every second
		costate {
			printf("counts = %3d  %3d\n", count0, count1);
			waitfor(DelaySec(1));
		}
	}

	WrPortI(I0CR, &I0CRShadow, 0x00);				// disable external interrupt 0
	WrPortI(I1CR, &I1CRShadow, 0x00);				// disable external interrupt 1

	// final counts
	printf("counts = %3d  %3d\n", count0, count1);
}


/****** interrupt routines  ******/

nodebug root interrupt void my_isr0()
{
	count0++;
}

nodebug root interrupt void my_isr1()
{
	count1++;
}