/*
	MGPIOM3A.C
	
	BL14xx/MicroG or BL15xx/MicroG2

	Write to PIO port B and Read PIO port A.
	
	Set up PIO port A as input and monitor the bit changes.
	Set up PIO port B bit 6 and 7 as output.
	Connect H3, pins 7-13 and 8-14.

	Change values of PIOB bit 6 with resPIODB(0x40) and setPIODB(0x40).
	Change values of PIOB bit 7 with resPIODB(0x80) and setPIODB(0x80).

	Since the PIOB is also used to drive other devices, the functions 
	resPIODB() and setPIODB() preserves the state of the other bits when
	not activated.
*/

char last_state;
char next_state;
int  icheck, idata;

main()
{
	printf("Reads back the current state of the PIODA 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");

	outport(PIOCA, 0xcf);         // mode 3 for PIOA
	outport(PIOCA,'\B11111111');  // all bits are inputs

	resPIOCB('\B11000000');       // set bits 6 and 7 to output
	
	last_state = 0;
	while (1)
	{
		hitwd();
		runwatch();
		next_state = inport(PIODA);
		if(last_state != next_state)
		{
			printf("PIODA inputs >> %x\n", next_state);
			last_state = next_state;
		}           
		icheck++;
		if(icheck >= 1000)
		{
			icheck = 0;
			if(idata)
			{
				resPIODB(0xc0);      // reset bits 6 & 7 
				idata = 0;
			}
			else
			{
				setPIODB(0xc0);      // set bits 6 & 7
				idata = 1;
			}
		}
	}
}





