/*
	BL17AIN.C

	Use with BL1700 boards.
		
	This program demonstrates how to calibrate Analog Input 0 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:
	Connect power supply of 0-9 volts to AIN0 terminal.
	Follow the prompted directions of this program during execution.
	Compile and run this program.
	Vary the voltage from 0-9.
*/

#use vdriver.lib
#use eziobl17.lib
#define AINPUT 0				//adc input 0
#define LOVOLT 0.0			//voltage for first reading
#define HIVOLT 9.0			//voltage for second reading
#define SETRAW 16				//offset to get raw data

void AnyKeyPress (void);

main ()
{
	auto unsigned rawdata;
	auto unsigned data1, data2;
	float volt_equ, volt1, volt2;
	auto unsigned inputnum;

	VdInit();
	eioBrdInit(0);
	inputnum = AINPUT;
	volt1 = LOVOLT;
	volt2 = HIVOLT;

	//get raw data from two known voltages
	printf("Adjust for voltage to %.2fV for first reading and press spacebar to continue\n", LOVOLT);
	AnyKeyPress();
	data1 = (unsigned)eioBrdAI(inputnum+SETRAW);

	printf("Adjust for voltage to %.2fV for second reading and press spacebar to continue\n", HIVOLT);
	AnyKeyPress();
	data2 = (unsigned)eioBrdAI(inputnum+SETRAW);

	//define gain and offset and store in memory
	eioBrdACalib(inputnum, data1, data2, volt1, volt2);

	//use the stored values to compute equivalent voltage
	while (1)
	{
		rawdata = (unsigned)eioBrdAI(inputnum+SETRAW);
		volt_equ = eioBrdAI(inputnum);
		printf("Voltage at AIN%d is %.2f from raw data %d\n", inputnum, volt_equ, rawdata);
	}
}

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