/*
	MG2PIO3A.C

	BL15xx/MicroG2

	Set up PIO2 port A as input and monitor the bit changes.
	Set up PIO2 port B bits 4, 5, 6 and 7 as output.

	Connect H1, pins 5-11, 7-13, 6-14, 8-12.

	Toggle the state of the PIO2 B outputs with resPIODB2(0xf0) and
	setPIODB2(0xf0).
*/

char last_state;
char next_state;
int  icheck, idata;

main()
{
	printf("Reads back the current state of the PIODA2 port.\n");
	printf("Print the state if it changes from last read.\n");
	printf("Since bits 2 to 7 are not pulled high, watchout for\n");
	printf("crosstalk across bits.\n");
	
	last_state = 0;
	while (1)
	{
		hitwd();
		runwatch();
		next_state = inport(PIODA2);
		resPIOCB2(0xf0);              // set 4 upper nibber to output
		if(last_state != next_state)
		{
			printf("PIODA2 inputs >> %x\n", next_state);
			last_state = next_state;
		}           
		icheck++;
		if(icheck >= 1000)
		{
			icheck = 0;
			if(idata)
			{
				resPIODB2(0xf0);      // reset bits 4, 5, 6 & 7 
				idata = 0;
			}
			else
			{
				setPIODB2(0xf0);      // set bits 4, 5, 6 & 7
				idata = 1;
			}
		}
	}
}





