/***************************************************************************
	XP85_1.C

	This program demonstrates how to calibrate an XP8500 ADC CH0 using
	two known voltages and defines the two coefficients, gain and offset.
	It	will then read back these coefficients to compute the equivalent
	voltage.

	Instructions:
	This program will look for an XP8500 board.
	Connect power supply of 0-10 volts to CH0 terminal.
	Make sure J3 is jumpered on pins 2-3 or program will end.
	Follow the prompted directions of this program during execution.
	Compile and run this program.
	Vary the voltage from 0-10 volts.
***************************************************************************/

#use vdriver.lib
#use eziocmmn.lib
#use eziopbdv.lib
#use eziobl17.lib			//	this line is required only by the BL1700
#use ezioplc2.lib			//	this line is required only by the BL1700
//#use ezioplc.lib		//	this line is required only by the:
								//	BL1200, BL1600, PK2100, PK2200
//#use eziomgpl.lib		//	this line is required only by the BL1400 & BL1500
//#use eziolgpl.lib		//	this line is required only by the BL1100
//#use eziotgpl.lib		//	this line is required only by the BL1000


#define AINPUT 0				//adc input 0-10
#define LOVOLT 1.0			//voltage for first reading
#define HIVOLT 9.0			//voltage for second reading

void AnyKeyPress (void);

main ()
{
auto int rawdata;
auto int data1, data2;
float volt_equ, volt1, volt2;
char inputnum;
char boardnum, testbrd;
auto struct _eioAdcCalib c;

VdInit();
plcBusReset();
eioPlcRstWait();

testbrd = 0xff;
for (boardnum=0; boardnum<=3; boardnum++)			
	{
	if (plcXP85Init(boardnum)==0)
		{
		printf("Located XP8500 board %d\n", boardnum);
		testbrd = boardnum;
		break;
		}
	}

if (testbrd!=boardnum)
	printf("Cannot find XP8500 board %d\n");
else
	{	
	inputnum = AINPUT;
	volt1 = LOVOLT;
	volt2 = HIVOLT;

	//get raw data from two known voltages
	printf("Make sure J3 is jumpered on pins 2-3.\n");
	printf("Adjust for voltage to %.2fV for first reading and press spacebar to continue\n", LOVOLT);
	AnyKeyPress();
	data1 = plcXP85In(testbrd*16+inputnum);		//raw data

	printf("Adjust for voltage to %.2fV for second reading and press spacebar to continue\n", HIVOLT);
	AnyKeyPress();
	data2 = plcXP85In(testbrd*16+inputnum);		//raw data

	//define gain and offset and store in memory
	if	(eioAdcMakeCoeff(&c,data1,data2,volt1,volt2))
		printf("Cannot make coefficients\n");
		
	if (plcXP85WrCalib(testbrd*16+inputnum, &c))
		printf("Cannot calibrate\n");
	else
		{	
		//use the stored values to compute equivalent voltage
		memset((char *)&c,0,sizeof(struct _eioAdcCalib));
		if (plcXP85RdCalib(testbrd*16+inputnum,&c))
				printf("Can't read calibration constant\n");
		else
			{
			printf("Vary voltage on channel\n");
			while (1)
				{
				rawdata = plcXP85In(testbrd*16+inputnum);		//raw data
				volt_equ = plcXP85InC(testbrd*16+inputnum);	//converted raw data
				printf("Voltage at CH%d is %.2f from raw data %d\n", inputnum, volt_equ, rawdata);
				}
			}
		}
	}
}	//end main


void AnyKeyPress (void)
{
	while (!kbhit())
		hitwd();
	getchar();
}