/****************************************************************************
adc4smp1.c
	Sample program to read data from an XP8500/ADC4 board on the PLCBUS. The
	first four channels of the XP8500/ADC4 board are read. The data are
	displayed showing the raw A/D readings and their equivalent voltages based
	on the calibration constants stored in the eeprom of the XP8500/ADC4 board.
	Calibration constants to convert A/D reading to voltages are stored in the 
	last 66 bytes (6 bytes/channel) of the eeprom of the XP8500/ADC4 board. 

	adc4smp3.c shows how to recalibrate a channel of an XP8500/ADC4 board and
	how to store the new calibration constants to the eeprom.
****************************************************************************/

#if (BOARD_TYPE == CPLC_BOARD) || (BOARD_TYPE==L_STAR)
#use cplc.lib
#endif

main()
{
	struct adc4coeff adc4conv0;
	struct adc4coeff adc4conv1;
	struct adc4coeff adc4conv2;
	struct adc4coeff adc4conv3;

	int data0, data1, data2, data3;
	unsigned int adc4_board;

	int i;      
#if (BOARD_TYPE == CPLC_BOARD) || (BOARD_TYPE==L_STAR)
	uplc_init();
#endif

	reset_pbus();              // reset the plcbus
	reset_pbus_wait();         

	if(sysclock() > 0x1e00)
		reset_pbus_wait();  // wait double if the clock is faster than 9 Mhz. 

	// find the first available XP8500/ADC4 board on the PLCBUS

	hitwd();
	for(i=0;i<4;i++)
	{
		if(adc4_init(0x0c0+i))
		{
			adc4_board = 0x0c0 + i;
			break;
		}
	}

	if( i >= 4)
	{
		printf("No XP8500/ADC4 Board is detected !!!\n");
		while(1) runwatch();
	}

	printf("XP8500/ADC4 board %x has been detected !!!\n\n", adc4_board);

	printf("Reading XP8500/ADC4 board calibration constants ...\n\n");

	adc4_readcoeff(adc4_board, 0, &adc4conv0);   // read calibration for chan0
	adc4_readcoeff(adc4_board, 1, &adc4conv1);   // read calibration for chan1
	adc4_readcoeff(adc4_board, 2, &adc4conv2);   // read calibration for chan2
	adc4_readcoeff(adc4_board, 3, &adc4conv3);   // read calibration for chan3

	hitwd();
	printf("Chan0 Calibration, zero_offset = %d, invgain = %f\n",
					adc4conv0.zero_offset, adc4conv0.invgain);
	printf("Chan1 Calibration, zero_offset = %d, invgain = %f\n",
					adc4conv1.zero_offset, adc4conv1.invgain);
	printf("Chan2 Calibration, zero_offset = %d, invgain = %f\n",
					adc4conv2.zero_offset, adc4conv2.invgain);
	printf("Chan3 Calibration, zero_offset = %d, invgain = %f\n",
					adc4conv3.zero_offset, adc4conv3.invgain);

	printf("\nToggle F4 (DOS only) to make keyboard input active as STDIO.\n");
	printf("\nHit any key to read A/D data from the XP8500/ADC4 board.\n");
	
	for(;;)
	{
		while(!kbhit()) {
			runwatch();         // wait for key from the PC
			hitwd();
		}
		
		getchar();                          // get the key 
	
		data0 = adc4_read(adc4_board, 0);   // read A/D channel 0
		data1 = adc4_read(adc4_board, 1);   // read A/D channel 1
		data2 = adc4_read(adc4_board, 2);   // read A/D channel 2
		data3 = adc4_read(adc4_board, 3);   // read A/D channel 3
		
		hitwd();

		printf("\nData for XP8500/ADC4 channels 0-3 !!!\n");
		printf("chan 0 >> %6d, %8.3f volts\n", data0, adc4_convert(data0, &adc4conv0));
		printf("chan 1 >> %6d, %8.3f volts\n", data1, adc4_convert(data1, &adc4conv1));
		
		hitwd();
		printf("chan 2 >> %6d, %8.3f volts\n", data2, adc4_convert(data2, &adc4conv2));
		printf("chan 3 >> %6d, %8.3f volts\n", data3, adc4_convert(data3, &adc4conv3));

		hitwd();
	}
}
