/*
 XP81_1.C
 
 XP81xx  Digital Input/Output

 This sample program demonstrates higher level digital input and output
 function calls. It will detect XP81xx boards attached to the PLC-bus, scan
 all 32 channels, turn on all 32 output channels, and turn off all 32 output
 channels. 
 
 Hardware Requirements:

 	1.	Any controller supporting the PLC-bus natively, BL1400(Micro-G)
 		or BL1500(Micro-G2).
 	2.	XP81xx, schematic
 	3. Relays or LED's and Resistors - optional
 	4.	Digital Wiring Board - optional

 Software Requirements:

 	1.	lib.dir
 	2.	default.h
 	3.	libraries listed below

 Instructions:

 	Detecting the board -
 	This program scans all eight possible XP81xx board addresses.

	Visual Output -
	This program turns on all 32 output channels, then turns off all 32
	channels. Please refer to the manual for instructions to set up the
	output. If your XP81xx board is not configured for output (or part
	of it is not), you can still run this program.

	Input Display-
	This program scans all 32 input channels. If your XP81xx board is not
	configured for input (or part it is not), you can still run this
	program. The positions corresponding to non-input pins will be
	undetermined.

 Uses:

	vdriver.lib provides virtual driver.

	eziocmmn.lib provides and specifies common routines.

	eziopbdv.lib provides specific PLCBus device routines.
 	
 	ezioplc.lib provides methods to access the PLCBus with a controller
 		supporting the PLCBus natively.
 		
 	eziomgpl.lib provides methods to access the PLCBus with a BL1400(Micro-G)
 		or	BL1500(Micro-G2),which uses one PIO port to simulate the PLCBus driver.
 		
 	plcXP81In(int boardaddress) reads an input from the board
 	plcXP81Out(int boardaddress, int state) writes an output to the board
 	eioResetPlcBus() resets the PLC-bus
 	eioPlcRstWait() waits long enough for all boards on the PLC-bus to reset
*/
 
#use vdriver.lib
#use eziocmmn.lib
#use eziopbdv.lib

#use ezioplc.lib		//	uncomment for PK2100(Rugged Giant), PK2200(Little Star),
							// BL1200(Little PLC), BL1600(Little G)

//#use eziomgpl.lib	//	uncomment for BL1400(Micro-G) or BL1500(Micro-G2)

char TITLE[] = {"XP81xx Digital Input and Output"};

main()
{
int channum;
int i;

	VdInit();
	printf("%s\n\n", TITLE);
	eioResetPlcBus();			//	reset the PLCBus
	eioPlcRstWait();			//	delay ensures the PLCBus boards reset

	//	locate all possible jumper-set addresses from 0 to 7 and display status
	for (i = 0; i <= 7; ++i) {
		if (plcXP81In(i*32)==-1) {				// do a read to locate the board
			printf("Board %d is not located\n",i);
		} else {
			printf("Board %d is located\n",i);
			
			//	read each bank from 0 to 3 and display each input channel
			printf("Reading all 32 positions\n");
			for (channum = 0; channum < 32; ++channum) {
				printf(plcXP81In(i*32+channum)?"1":"0");	// read input of channel
			}
			printf("\n");
			printf("Press a key to continue...\n");
			while (!kbhit());
			getchar();
			
			//	enable each channel from 0 to 31
			printf("Enabling all 32 positions\n");
			for (channum = 0; channum < 32; ++channum) {
				plcXP81Out(i*32+channum,1);		// write state to output channel
			}
			printf("Press a key to continue...\n");
			while (!kbhit());
			getchar();
			
			//	disable each channel from 0 to 31
			printf("Disabling all 32 positions\n");
			for (channum = 0; channum < 32; ++channum) {
				plcXP81Out(i*32+channum,0);		// write state to output channel
			}
			printf("Press a key to continue...\n");
			while (!kbhit());
			getchar();
		}
		printf("\n");
	}
}
