/*
	MG555.C

	BL14xx/MicroG

	Shows usage of timer0, INT1 and DMA0 to read back the elapsed time from
	when the 555 is triggered.

	Connect resistance to be measured across 5V (pin 1, H3) and EXTRES
	(pin 22, H3).

	One Timer count is 20/6144000 = 3.255 microsecond.

	For the NE555S0, the one shot done time is t = 1.1 RC; where C is 0.47 uf
	and R is to be the measured quantity.

	In terms of the number of timer count tcount :
			R = (tcount x 3.255 us/cnt)/ (1.1 * 0.47 uf)
			R = 6.29634 * tcount.
	NOT RECOMMENDED for Small Resistance.

	Thermistor resistance can easily be measured.
	For thermistor,
						R = A e^(B/T)
	For a thermistor with A = -4.78% and R=100 KOhms at 25C.
						B = - (T*T)*A = 4247
						T = B/(ln R - ln A)  , T in Kelvin
						
	The 555 is not available with the MicroG2.
*/

main()
{
int myload;
float ohms;
unsigned int elapsed;
int icheck; 

	printf("Sample Program to read elapsed time from timer0\n");
	printf("to measure the 1.1RC time of the 555 chip. \n");
	printf("Hit PC key to get next measurement.(Press F4 first if DOS)\n");
	printf("While in the watch window, change myload to other values to get \n");
	printf("timer0 to time out.\n");
	
	myload = 0xffff;              // load to maximum count for timer0
							
	while(1)
	{
		Set555(myload);            // set accessory function and trigger the 555
	
		while( !(icheck=Read555(&elapsed)))    // wait for done or timeout flag
		{
			hitwd();             // watchdog is always enabled
			runwatch();          // open access via the watch window
		}

		if( icheck == -1)
		{
			printf("timeout \n");
		}
		else
		{
			ohms = elapsed * 6.29634;    
			printf("count >> %d,  ohms >> %8.2f\n", elapsed, ohms);
		}
  
		while(!kbhit())
		{
			hitwd();
			runwatch();       // wait for next keyboard hit
		}
		getchar();
	}
}