/*

XP89_1:

This program demonstrates, step by step, how to calibrate the DAC channels.
To use this program properly, you can edit the statements that initializes
channel, margin, f1 and f2. You can also compile the program as is with F3,
then use watch expressions to override the assignment statements in this
program (be sure to execute the watch expression AFTER the assignment
statement is executed.

*/

#use eziocmmn.lib
//#use eziobl17.lib		//	this line is required only by the BL17xx
#use ezioplc.lib			//	this line is required only by the:
								//	BL1200, BL1600, PK2100, PK2200
//#use ezioplc2.lib			//	this line is required only by the BL1700
//#use eziomgpl.lib			//	this line is required only by the BL1400 & BL1500
//#use eziolgpl.lib			//	this line is required only by the BL1100
//#use eziotgpl.lib			//	this line is required only by the BL1000

main() {
	auto int i;
	auto struct _eioAdcCalib c;
	auto int channel;
	auto float f1, f2, fout;
	auto long l;
	auto int margin;

	channel = 0;		//	execute watch expression to override
	margin = 0x40;		//	execute watch expression to override
	plcBusReset();
	if (plcXP89Init(channel / 8)) {
		printf("DAC8 board not found\n");
	} else {
		plcXP89Sw(channel / 8, 1);								//	enable voltage reference
		plcXP89Out(channel,margin);			//	use meter to record level
		f1 = 10;										//	use watch expr to override
		plcXP89Out(channel,0xfff-margin);	//	use meter to record level
		f2 = -10;									//	use watch expr to override
		
		eioAdcMakeCoeff(&c,margin,0xfff-margin,f1,f2);
		if (plcXP89WrCalib(channel,&c)) {
			printf("Can't write calibration constant\n");
		}
		memset((char *)&c,0,sizeof(struct _eioAdcCalib));
		if (plcXP89RdCalib(channel,&c)) {
			printf("Can't read calibration constant\n");
		}
		fout = 2.345;							//	use watch expr to override
		l = eioAdcDigitize(fout, &c);
		plcXP89Out(channel,(unsigned)l);	//	use meter to check voltage now
	}
}
