/*
	MG2PIO3B.C

	BL15xx/MicroG2

	Set up bits 0 and 1 of PIOA2 to generate interrupts when a high to
	low transition is seen on either bits. PIOA2-3 and PIOA2-4 are configured
	as outputs to the inputs PIOA2-1 and PIOA2-2.

	Jumper H1, pins 2-4 and 3-5 to connect output to input. Outputs will
	alternately toggle.
*/

char jjj, kkk, state;

main()
{
int toggle;
	
	jjj = kkk = toggle = 0;
	
	printf("Jumper pins H1, 2-4 and 3-5 to\n");
	printf("generate interrupt on the PIOA2 port.\n\n"); 
	outport(ITC, inport(ITC) | 0x01);          // enable INT0

	outport(PIOCA2, 0xcf);           // mode 3 for PIOA2
	outport(PIOCA2,'\B11110011');    // all bits are inputs, except bits 2 & 3
	outport(PIOCA2, PIOA2_VEC);      // set vector to PIOA2
 
	outport(PIOCA2,0x97);            // interrupts on, d7 = 1, (0) off
												// or data        d6 = 0, (1) and
												// active low     d5 = 0, (1) high
												// mask follows   d4 = 1, (0) not

	outport(PIOCA2, '\B11111100');   // bit is masked out if 1
												// active bits should be zero
												
	outport(PIODA2,0x0f);				// initialize inputs high
		
	while (1)
	{
		
		hitwd();
		runwatch();

		toggle = toggle?0:1;		
		outport(PIODA2, toggle?0x07:0x0b);		//toggle PIOA2-2 and PIOA2-3
	
		if (jjj != kkk)         					// test for interrupt  
		{
			printf("Interrupt generated by %s !!! >> %x\n",
					toggle?"PIOA2-3":"PIOA2-2", inport(PIODA2));
			kkk = jjj;
		}
		outport(PIODA2,0x0f);						// reset inputs
	}
}

#INT_VEC PIOA2_VEC myint

interrupt reti myint()
{
	jjj++;                              // change jjj 
}





