/***************************************************************************
	ssdac1.c
	
   Z-World, 2001
	This sample program is used with Smart Star products, specifically
	for 0-10V and +/-10V DAC boards.

	!!!Caution this will overwrite the calibration constants set at the
	   factory.
	   
	This program demonstrates how to recalibrate an DAC channel using
	two known voltages and defines the two coefficients, gain and offset,
	which will be rewritten into the DAC's EEPROM. 

	Instructions:
	Connect a voltage meter to an output channel.
	Compile and run this program.
	Follow the prompted directions of this program during execution.
	
***************************************************************************/

#define LOCOUNT 400			//gives highest voltage
#define HICOUNT 3695			//gives lowest voltage

main()
{
	auto int slotnum, outputnum;
	static float voltout, volt1, volt2;
	auto char tmpbuf[24];
	
	brdInit();
	
	printf("Please enter DAC board slot position, 0 thru 6....");
	do
	{
		slotnum = getchar();
	} while ((slotnum < '0') || (slotnum > '6'));
	printf("Slot %d chosen.\n", slotnum-=0x30);

	///// configure all outputs to zero volts and enable output
	for (outputnum=0; outputnum<=7; outputnum++)
	{
		while (anaOutEERd(ChanAddr(slotnum, outputnum)));
		anaOutVolts(ChanAddr(slotnum, outputnum), 0.0);
	}
	anaOutEnable();
	
	printf("Please enter an output channel, 0 thru 7....");
	do
	{
		outputnum = getchar();
	} while (!((outputnum >= '0') && (outputnum <= '7')));
	printf("channel %d chosen.\n", outputnum-=0x30);

	/////get voltages from two known raw data
	anaOut(ChanAddr(slotnum, outputnum), HICOUNT);
	printf("Type first voltage reading (in Volts) from meter and press Enter   ");
	volt1 = atof(gets(tmpbuf));
	
	anaOut(ChanAddr(slotnum, outputnum), LOCOUNT);
	printf("Type second voltage reading (in Volts) from meter and press Enter  ");
	volt2 = atof(gets(tmpbuf));
	
	if (anaOutCalib(ChanAddr(slotnum, outputnum), HICOUNT, volt1, LOCOUNT, volt2))
		printf("Cannot make coefficients\n");
	else
	{
		/////store coefficients into eeprom
		while (anaOutEEWr(ChanAddr(slotnum, outputnum)));
		printf("Wrote coefficients to eeprom\n");
		
		/////read back coefficients from eeprom
		while (anaOutEERd(ChanAddr(slotnum, outputnum)));
		printf("Read coefficients from eeprom\n");
		
		while (1)
		{
			printf("\nType a desired voltage (in Volts)  ");
			voltout = atof(gets(tmpbuf));
			printf("Observe voltage on meter.....\n");
			anaOutVolts(ChanAddr(slotnum, outputnum), voltout);
		}
	}

}