/***************************************************************************
	ad_caldiff_ch.c
	Z-World, 2002

	This sample program is for the RCM3400 series controllers with
	prototyping 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 user block data area.  It will also continuously
	display the voltages being monitored.

	This program must be compiled to Flash.

	
	Power Supply to Proto-Board Connections
	=======================================
		+V			Gnd		
		-----		-----		
		AIN0 		AIN1 	
		AIN2 		AIN3 	
		AIN4 		AIN5
		
	Make sure pins 1-2 are connected on JP1, JP2, and JP9.  JP5 should
	not have any pins connected.
		
	Instructions
	============
	1. Connect 0-20 volt power supply positive terminal to AIN1 on the
		proto-board.
	2.	Connect power supply ground to AIN0 on the proto-board. 
	3. Adjust power supply on to approximately 19.0 volts. The controller
		will see -19.0 volts across the two terminals.
	4. Compile and run this program.
	5. Enter 0 when prompted for gain code.
	6. Enter 0 when prompted for the input channel code.
	7. When prompted, switch power supply Gnd to AIN1 and positive terminal
		AIN0.

***************************************************************************/
#class auto

const char vstr[][10] = {
	"20.0V\0",
	"10.0V\0",
	"5.0V\0",
	"3V\0",
	"2.25V\0",
	"2.0V\0",
	"1.25V\0",
	"1.0V\0"};
	
void printrange()
{
	printf("\ngain_code\tVoltage range\n");
	printf("---------\t-------------\n");
	printf("\t0\t-20 to +20\n");
	printf("\t1\t-10 to +10\n");
	printf("\t2\t-5 to +5\n");
	printf("\t3\t-3 to +3\n");
	printf("\t4\t-2.25 to +2.25\n");
	printf("\t5\t-2 to +2\n");
	printf("\t6\t-1.25 to +1.25\n");
	printf("\t7\t-1 to +1\n");
	
	printf("\nchannel_code\tInputs\n");
	printf("------------\t-------------\n");
	printf("\t0\t+AIN0 -AIN1\n");
	printf("\t2\t+AIN2 -AIN3\n");
	printf("\t4\t+AIN4 -AIN5\n");
}

nodebug
void msDelay(unsigned int delay)
{
	auto unsigned long done_time;
	
	done_time = MS_TIMER + delay;
   while( (long) (MS_TIMER - done_time) < 0 );
}


main ()
{
	auto unsigned int channel, value1, value2;
	auto float voltage, volts1, volts2, v1, v2;
	auto unsigned int rawdata;
	auto char buffer[64];
	auto unsigned int gaincode;
	
	brdInit();
	
	while (1)
	{
		printrange();
		printf("\nChoose the AD channel 0, 2, 4  .... ");
		gets(buffer);
		channel = atoi(buffer);	

		printf("\nChoose gain code .... ");
		gets(buffer);
		gaincode = atoi(buffer);

		printf("\nAdjust voltage to just above -%s and enter actual =  ", vstr[gaincode]);
		gets(buffer);
		volts1 = atof(buffer);
		value1 = anaIn(channel, DIFF, gaincode);
		if (value1 == ADOVERFLOW)
			printf("lo:  channel=%d overflow\n", channel);
		else		
			printf("lo:  channel=%d raw=%d\n", channel, value1);
	
		printf("\nSwitch the connections and enter voltage =  ");
		gets(buffer);
		volts2 = atof(buffer);
		value2 = anaIn(channel, DIFF, gaincode);
		if (value2 == ADOVERFLOW)
			printf("hi:  channel=%d overflow\n", channel);
		else		
			printf("hi:  channel=%d raw=%d\n", channel, value2);
	
		anaInCalib(channel, DIFF, gaincode, value1, volts1, value2, volts2);
	
		printf("\nstore constants to flash\n");
		anaInEEWr(channel, DIFF, gaincode);		
	
		printf("\nread calibration constants\n");
		anaInEERd(channel, DIFF, gaincode);		
		
		printf("\nVary voltage from -%s to +%s  .... \n", vstr[gaincode], vstr[gaincode]);
		
		while (strcmp(buffer,"q") && strcmp(buffer,"Q"))
		{
			voltage = anaInDiff(channel, gaincode);
			printf("Ch %2d Volt=%.5f \n", channel, voltage);
			printf("press enter key to read values again or 'Q' to calibrate another channel\n\n");
			gets(buffer);
		}
	}              
}
///////////////////////////////////////////////////////////////////////////

