/***************************************************************************
	sstarad1.c
	
   Z-World, 2001
	This sample program is used with Smart Star products.

	This program demonstrates how to recalibrate an ADC channel using
	two known voltages and defines the two coefficients, gain and offset,
	which will be rewritten into the ADC's EEPROM. 
	Computed equivalent voltage will be displayed.

	Instructions:
	Connect a power supply of 0-10 volts (or +/-10 volts depending on your
	board) to the input channel.
	Follow the prompted directions of this program during execution.
	Compile and run this program.
	Vary the voltage from 0-10 volts (or +/-10 volts depending on your
	board) to the input channel.
	
	This setup defaults for voltages of 0-10 volts.
	Change the macros below to match your board.
***************************************************************************/

#define LOVOLT 1.0		//voltage for first reading, for +/- 10V enter -9.0
#define HIVOLT 9.0		//voltage for second reading


main ()
{
	auto int rawdata, data1, data2,i;
	auto float voltequ, volt1, volt2;
	auto int inputnum, slotnum;

	brdInit();

	printf("Please enter ADC board slot position, 0 thru 6....");
	do {
		slotnum = getchar();
		} while ((slotnum < '0') || (slotnum > '6'));
	printf("Slot %d chosen.\n", slotnum-=0x30);

	printf("Please enter an input channel, 0 thru A (10)....");
	do {
		inputnum = getchar();
		} while (!((inputnum >= '0') && (inputnum <= '9')) && (inputnum != 'a') && (inputnum != 'A'));
	if ((inputnum == 'a') || (inputnum == 'A'))
	{
		printf("channel 10 chosen.\n");
		inputnum=10;
	}
	else
		printf("channel %d chosen.\n", inputnum-=0x30);

	/////get raw data from two known voltages
	printf("Adjust for voltage to %.3fV for first reading and press spacebar to continue\n", LOVOLT);
	getchar();
	data1 = anaIn(ChanAddr(slotnum, inputnum));
	printf("Adjust for voltage to %.3fV for second reading and press spacebar to continue\n", HIVOLT);
	getchar();
	data2 = anaIn(ChanAddr(slotnum, inputnum));
	
	/////calculate gains and offsets
	if	(anaInCalib(ChanAddr(slotnum, inputnum), data1, LOVOLT, data2, HIVOLT))
		printf("Cannot make coefficients\n");
	else {
		/////store coefficients into eeprom
		while (anaInEEWr(ChanAddr(slotnum, inputnum)));
		printf("Wrote coefficients to eeprom\n");
		
		/////read back coefficients from eeprom
		while (anaInEERd(ChanAddr(slotnum, inputnum)));
		printf("Read coefficients from eeprom\n");
		
		printf("Vary voltage on channel\n");
		while (1)
		{
			voltequ = anaInVolts(ChanAddr(slotnum, inputnum));
			printf("Voltage at CH%d is %.5f V\n", inputnum, voltequ);
		}
	}
}	//end main


