/*
 XP81OUTX.C
 
 XP81xx  Digital Input/Output
 This sample program does exactly what xp81out.c does, but without using
 the high level function plcXP81In and plcXP81Out. Instead this sample program
 uses the abstract PLC-bus functions _eioReadD0, eioReadD1, eioWriteWR and etc.
 to communicate with the XP81xx.
 
 
 Hardware Requirements:
 	1.	Any controller supporting the PLC-bus natively, BL1400(Micro-G)
 		or BL1500(Micro-G2).
 	2.	XP81xx, schematic

 Software Requirements:
 	1.	lib.dir
 	2.	default.h
 	3.	libraries listed below

 Instructions:
 Same as xp81out.c
 
 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.
 	
 	eioResetPlcBus() resets the PLC-bus
 	eioPlcRstWait() waits long enough for all boards on the PLC-bus to reset
	_eioPlcXP81Addr() computes the PLC-bus address of an XP81xx board based on
		its logical address (set by jumpers).
	_eioReadD0() reads the PLC-bus register D0
	_eioReadD1() reads the PLC-bus register D1
	_eioWriteWR() writes to the PLc-bus register WR
	_eioPlcAdr12() sets a 12-bit PLC-bus address
	_eioPlcAdr4() sets the least significant 4 bits of a PLC-bus address
*/
 
#use vdriver.lib
#use eziocmmn.lib
#use eziopbdv.lib
#use ezioplc.lib		//	uncomment #use ezioplc.lib for PK2100(Rugged Giant),
							// PK2200(Little Star),BL1200(Little PLC), BL1600(Little G)
//#use eziomgpl.lib	//	uncomment #eziomgpl.lib for BL1400(Micro-G) or BL1500(Micro-G2)


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

main()
{
	int banknum;
	int i;
	int brdAdr;
	int channum;

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

	//	locate all possible jumper-set addresses from 0 to 7
	for (i = 0; i < 8; ++i) {
		brdAdr = _eioPlcXP81Addr(i);	// convert board address to PLCBus format
		eioPlcAdr12(brdAdr);				//	send all three nibbles
		if (_eioReadD0()&1) {			//	check for board ID in bit position zero
			printf("Board %d is not located\n",i);
			}
		else {
			printf("Board %d is located\n",i);
			
			//	enable each bank from 0 to 3
			printf("Enabling all 32 positions\n");
			for (banknum = 0; banknum < 4; ++banknum) {
				eioPlcAdr4((brdAdr>>8) | banknum | 0x4);	//	send highest nibble
				for (channum = 0; channum < 8; ++channum) {
					_eioWriteWR(channum<<1|1);					//	turn on a channel
					}
				}
			printf("Press a key to continue...\n");
			while (!kbhit());
			getchar();
			
			//	disable each bank from 0 to 3
			printf("Disabling all 32 positions\n");
			for (banknum = 0; banknum < 4; ++banknum) {
				eioPlcAdr4((brdAdr>>8) | banknum | 0x4);	//	send highest nibble
				for (channum = 0; channum < 8; ++channum) {
					_eioWriteWR(channum<<1);					//	turn off a channel
					}
				}
			printf("Press a key to continue...\n");
			while (!kbhit());
			getchar();
			}
		printf("\n");
		}
}
