/*
	MGTEMP.C

	BL14xx/MicroG

	Use the 555 to read back the resistance of the thermistor on the
	MicroG Prototyping Board.

	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.
	
	For thermistor,
						R = A e^(B/T)
	The thermistor on the MicroG ProtoTyping Board has A = -4.49%
	and R=3000 ohms at 25C. Therefore:
						B = - (T*T)*A = 3991
						T = B/(ln R - ln A)  , T in Kelvin
						T = 3991/( ln(R) + 5.38)

	The 555 is not available with the MicroG2.
*/

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

	printf("Sample Program to measure temperature using the \n");
	printf("thermistor on the MicroG Prototyping Board.\n");
	printf("Hit PC key to get next measurement.(Press F4 first for DOS)\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;    
			temp_c = ( 3991.0 / ( log(ohms) + 5.38)) - 273.15;
			temp_f = temp_c * 1.8 + 32;
			printf("ohms >> %8.2f, temp >> %6.2fC , %6.2fF\n", ohms, temp_c, temp_f);
							
		}
  
		while(!kbhit())
		{
			hitwd();
			runwatch();       // wait for next keyboard hit
		}
		getchar();
	}
}