/***************************************************************************
	ssdac3.c
	
   Z-World, 2001
	This sample program is used with Smart Star products, specifically
	for the 4-20mA DAC board.

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

	Instructions:
	Connect a current meter to an output channel.
	Compile and run this program.
	Follow the prompted directions of this program during execution.
	
***************************************************************************/
#define LOCOUNT 400				//gives highest current
#define HICOUNT 3695				//gives lowest current


main()
{
	auto int slotnum, outputnum, i;
	static float curout, cur1, cur2;
	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 4 mA and enable output
	for (outputnum=0; outputnum<=7; outputnum++)
	{
		while (anaOutEERd(ChanAddr(slotnum, outputnum)));
		anaOutmAmps(ChanAddr(slotnum, outputnum), 4.00);
	}
	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 currents from two known raw data
	anaOut(ChanAddr(slotnum, outputnum), HICOUNT);
	printf("Type first current reading (in milli-Amps) from meter and press Enter   ");
	cur1 = atof(gets(tmpbuf));
	
	anaOut(ChanAddr(slotnum, outputnum), LOCOUNT);
	printf("Type second current reading (in milli-Amps) from meter and press Enter  ");
	cur2 = atof(gets(tmpbuf));
	
	if (anaOutCalib(ChanAddr(slotnum, outputnum), HICOUNT, cur1, LOCOUNT, cur2))
		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 current (in milli-Amps) and press Enter  ");
			curout = atof(gets(tmpbuf));
			printf("Observe current on meter.....\n");
			anaOutmAmps(ChanAddr(slotnum, outputnum), curout);
		}
	}
}