/***************************************************************************
	ain_calse_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 single-ended ADC channel
	using two known voltages to generate constants for that channel and
	rewritten RN1100 board flash.  It will also continuously display the voltages
   being monitored.


	Connections
	===========
	Connect the power supply positive output to the AD channels and the
	negative output to GND on the controller.

	Connect a volt meter to monitor the voltage input.


	Instructions
	============
	1. Compile and run this program.
	2. Follow directions in STDIO window.

***************************************************************************/
#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;
	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 on all channels for conversions
	for(channel = 0; channel <= 2; channel++)
	{
		status = rn_anaInConfig(handle, channel, ENABLE, 0, 0);
   }

	while (1)
	{
		printf("\nChoose the AIN channel 0 - 2 .... ");
		gets(buffer);
		channel = atoi(buffer);

		printf("\nAdjust voltage to %5.2f and enter actual =  ", vmin[channel]);
		gets(buffer);
		volts1 = atof(buffer);
		status = rn_anaIn(handle, channel, &value1, 10);
		if (value1 == ADOVERFLOW)
			printf("lo:  channel=%d overflow\n", channel);
		else
			printf("lo:  channel=%d raw=%d\n", channel, value1);

		printf("\nAdjust voltage to %5.2f and enter actual =  ", vmax[channel]);
		gets(buffer);
		volts2 = atof(buffer);
		status = rn_anaIn(handle, channel, &value2, 10);
		if (value2 == ADOVERFLOW)
			printf("hi:  channel=%d overflow\n", channel);
		else
			printf("hi:  channel=%d raw=%d\n", channel, value2);

		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_anaInVolts(handle, channel, &voltage, 1);
			printf("Ch %2d Volt=%8.5f \n", channel, voltage);
			printf("press enter key to read values again or 'Q' to calibrate another channel\n\n");
			gets(buffer);
		}
	}
}
///////////////////////////////////////////////////////////////////////////

