/*
	MG555PIO.C

	BL14xx/MicroG

	Use the 555 to generate periods for square waves output from PIODA
	bits 0, 1 and 2.

	Bit 0 (H3 pin2) has the base period.
	Bit 1 (H3 pin3) has double the period of bit 0.
	Bit 2 (H3 pin4) has double the period of bit 1.

	The period of the square wave is set by a resistor placed across pin 1 of
	H3 (+5 VDC) and pin 22 of H3 (EXTRES).
	
	The 555 chip reaches its reset point from trigger for t = 1.1 RC seconds.

	For example, for a 10K resistor, the period of the square
	wave out of bit 0 is 2 x ( 1.1*0.47uf*10K) = 10.34 ms.

	If the MicroG Prototyping Board is connected with a thermistor resistance
	of ~3000 omhs at 25C, the period of the square wave out of bit 0 is
	2 x (1.1*0.47uf*3000) = 3.10 ms.

	The 555 is not available with the MicroG2.
*/

char pioadata;

main()
{
	outport(PIOCA, 0xcf);      // mode 3 for PIOA
	outport(PIOCA, 0x00);      // all bits as outputs
	outport(PIODA, 0x00);      // send all zeros out

	resPIOCB(0x01);            // set PIODB bit 0 as output
	setPIODB(0x01);            // set bit 0 to high

	DI();
	outport(ITC, inport(ITC) | '\B00000010');    // INT1 is set on
	EI();
	
	pioadata = 0;

	resPIODB(0x01);            // trigger 555 first time
	setPIODB(0x01);

	while(1)
	{
		hitwd();
		runwatch();
	}
}


#INT_VEC INT1_VEC square_wave

interrupt square_wave()
{

	pioadata++;
	outport(PIODA, pioadata);

	resPIODB(0x01);      // trigger the 555 again
	setPIODB(0x01);
}
