/***************************************************************************
	ain_caldiff_ch.c
	Z-World Inc 2003

	This sample program is intended for RabbitNet RN1100 Digital I/O boards

	Description
	===========
	This program demonstrates how to recalibrate one differential ADC channel
	using two known voltages to generate constants for that channel and
	rewritten into RN1100 board flash.  It will also continuously
	display the voltages being monitored.

	Connections
	===========
	You will need two power supplies. For this demonstration,

	1. Connect -0.25 to +0.25 volt power supply 1 to AIN03+ on the controller
		and power supply 2 to AIN03-.
	2.	Connect power supply grounds to GND on the controller.


	Instructions
	============
	1. Adjust power supply (1) on AIN03+ to approximately +0.20 volts.
	2. Adjust power supply (2) on AIN03- to approximately -0.20 volts.
	3. Compile and run this program.
	6. When prompted, switch power supply 1 connection to AIN03- and
		connect power supply 2 to AIN03+.

***************************************************************************/
#class auto					/* Change local var storage default to "auto" */


const char vstr[][16] = {
	"0 - 10V\0",
	"0 - 10V\0",
	"0 -  1V\0",
	"+/- 0.25V\0"};

const float vmin[4] = {1.0, 1.0, 0.1, -0.20};
const float vmax[4] = {9.0, 9.0, 0.9, 0.20};


void main ()
{
	auto int channel, value1, value2, handle, status;
	auto float voltage, volts1, volts2, v1, v2;
	auto unsigned int rawdata, gaincode;
	auto char buffer[64];
   auto rn_AinData aindata;

	brdInit();                 //initialize controller
   rn_init(RN_PORTS, 1);      //initialize controller RN ports
   if ((handle = rn_device(0000)) == -1)  //device on port 0
   {
   	printf("\n no device found\n");
      exit(0);
   }

   // enable channel for conversions
	channel = 3;
	status = rn_anaInConfig(handle, channel, ENABLE, 0, 0);

	while (1)
	{
		printf("\nCalibration for AIN channel 3 .... ");

		printf("\nEnter actual voltage for AIN03+ =  ");
		gets(buffer);
		v1 = atof(buffer);
		printf("\nEnter actual voltage for AIN03- =  ");
		gets(buffer);
		v2 = atof(buffer);
		volts2=v1-v2;
		status = rn_anaIn(handle, channel, &value2, 10);
		if (value2 == -1)
			printf("channel=%d overflow\n", channel);
		else
			printf("channel=%d raw=%d difference=%.3fV\n", channel, value2, volts2);

		printf("\nSwitch power supply connections and hit return key\n");
		gets(buffer);

		volts1=-volts2;
		status = rn_anaIn(handle, channel, &value1, 10);
		if (value1 == -1)
			printf("channel=%d overflow\n", channel);
		else
			printf("channel=%d raw=%d difference=%.3fV\n", channel, value1, volts1);

		rn_anaInCalib(channel, SINGLE, 0, value1, volts1, value2, volts2, &aindata);
   	printf("gain=%8.5f offset=%d\n", aindata.gain, aindata.offset);

		printf("\nstore constants to flash\n");
		status = rn_anaInWrCalib(handle, channel, SINGLE, 0, aindata, 0);

		printf("\nread back constants\n");
		status = rn_anaInRdCalib(handle, channel, SINGLE, 0, &aindata, 0);
   	printf("read back gain=%8.5f offset=%d\n", aindata.gain, aindata.offset);

		printf("\nVary voltage %s .... \n", vstr[channel]);

		while (strcmp(buffer,"q") && strcmp(buffer,"Q"))
		{
			status = rn_anaInDiff(handle, channel, &voltage, 1);
			if (voltage != ADOVERFLOW)
				printf("Ch %2d Volt=%.5f \n", channel, voltage);

			printf("press enter key to read value again or 'Q' to calibrate another\n\n");
			gets(buffer);
		}
	}

}
///////////////////////////////////////////////////////////////////////////

