//A2D
//D2A
//TV remote  control IR recevice
//Two DI (1-30V)
//Two DO

#include 
#include 



network input SNVT_lev_disc nvi_AD = ST_NUL;
network input short nvi_DA;

network output SNVT_count nvo_TEMP;
network output short nvo_TV_signal;

network output SNVT_lev_disc nvo_DI = ST_NUL;


/////////////////////////////// I/O Objects /////////////////////////////////

IO_8 neurowire master select(IO_6) ioA2D;	// Analog to Digital
IO_1 output bit ioA2DSelect = 0;    // Initially unselected
#pragma ignore_notused ioA2DSelect

IO_8 neurowire master select(IO_5) ioD2A;	// Digital to Analog
IO_3 output bit ioD2ASelect = 1;    // Initially unselected
#pragma ignore_notused ioD2ASelect

IO_4 input infrared ded clock (7) io_ir_data;                 
IO_4 input bit io_ir_data_level;
unsigned int bits_read;
unsigned int irb[2];

IO_0 input bit DI1;
IO_1 output bit DO1=0;

IO_2 input bit DI2;
IO_3 output bit DO2=0;

IO_6 output bit CS = 0;
IO_7 output bit S0 = 0;

IO_5 output bit relay = 0;

unsigned long value;
unsigned short value2;
unsigned short data;

///////////////////////////////// Functions /////////////////////////////////

//  A2DConvert( ) -- Get Analog to Digital data from a Mux channel

unsigned long A2DConvert(unsigned muxAddr) {
    static long a2dData[5]
	= { -1, -1, -1, -1, -1 };   // data cache
    static int prevAddr = -1;	    // last converted address

    static union {
	struct {
	    unsigned	muxAddr  : 4;
	} addr;
	struct {
	    unsigned	b9to2 : 8;
	    unsigned	      : 6;
	    unsigned	b1to0 : 2;
	} data;
    } a2dControl;			// image of A/D registers

    if (muxAddr >= 5) return -1;	// address out of range

    a2dControl.addr.muxAddr = muxAddr;	// set up for conversion
    io_in(ioA2D, &a2dControl, 10);	// start conversion

    if (prevAddr >= 0)			// save results of last conversion
	a2dData[prevAddr] =
	    ((long)a2dControl.data.b9to2 << 2) | a2dControl.data.b1to0;
    prevAddr = muxAddr;			// save for next time
    return a2dData[muxAddr];		// get last results for this address
}

#pragma ignore_notused A2DConvert

/////////////////////////////////////////////////////////////////////////////

//  D2AConvert( ) -- Send Digital to Analog data to a Mux channel

void D2AConvert(unsigned data, unsigned muxAddr) {
    static unsigned d2aData[4];     // existing values
    static struct {
	unsigned    ch4     : 6;
	unsigned    ch3Hi   : 2;
	unsigned    ch3Lo   : 4;
	unsigned    ch2Hi   : 4;
	unsigned    ch2Lo   : 2;
	unsigned    ch1     : 6;
    } d2aControl;		    // image of D/A registers

    if (muxAddr >= 4) return;	    // address out of range
    d2aData[muxAddr] = data;	    // save data in buffer

    d2aControl.ch4   = d2aData[3];  // build serial stream
    d2aControl.ch3Hi = d2aData[2] >> 6;
    d2aControl.ch3Lo = d2aData[2];
    d2aControl.ch2Hi = d2aData[1] >> 4;
    d2aControl.ch2Lo = d2aData[1];
    d2aControl.ch1   = d2aData[0];

    io_out(ioD2A, &d2aControl, 8); // update D/A converters
}

#pragma ignore_notused D2AConvert


//////////////////////////////////////////////////////////////////////

stimer readdata;

when(nv_update_occurs(nvi_AD))
when(timer_expires (readdata))
{
	if (nvi_AD==ST_ON)				//enable the AD converter
	{
		value = A2DConvert(0);
		nvo_TEMP = value;
		readdata = 1;
	}
	else if (nvi_AD==ST_OFF)		//disable the AD converter
	{
		readdata = 0;
	}	
	
	if ( readdata != 0)				//loop 
	{
		value = A2DConvert(0);
		nvo_TEMP = value;
		readdata = 1;				//read data 1 sec
	}
}

when(nv_update_occurs(nvi_DA))		
{
	value2 = nvi_DA;

	if (value2 >0 && value2 <64)
	{		
		io_out (relay,1);

		delay (20);
		io_out(S0, 1);
		delay (20);
		D2AConvert(value2,3);
		io_out(S0, 0);
	}
	
	else if (value2 == 0)
	{
		io_out (relay,0);

		delay (20);
		io_out(S0, 1);
		delay (20);
		D2AConvert(value2,3);
		io_out(S0, 0);		
	}

	else if (value2 > 100)
		{
			if (value2 == 101)			//DO1 = HIGH
				{io_out (DO1, 1);}		
			else if (value2 == 102)		//DO1 = LOW
				{io_out (DO1, 0);}
			else if (value2 == 103)		//DO2 = HIGH
				{io_out (DO2, 1);}
			else if (value2 == 104)		//DO2 = LOW
				{io_out (DO2, 0);}
		}
}

when (io_changes(DI1)to 0)
{
	io_out (DO1,0);
	nvo_DI = ST_OFF;			//Display DI1 = HIGH
}

when (io_changes(DI1)to 1)
{
	io_out (DO1,1);
	nvo_DI = ST_ON;				//Display DI1 = LOW
}


when (io_changes(DI2)to 0)
{
	io_out (DO2,0);
	nvo_DI = ST_MED;			//Display DI2 = HIGH
}

when (io_changes(DI2)to 1)
{
	io_out (DO2,1);
	nvo_DI = ST_HIGH;			//Display DI2 = LOW
}


stimer TV_Reset;

when (io_changes(io_ir_data_level)to 0)		//Receive TV signal
{
	bits_read = io_in(io_ir_data, irb, 16, 64995UL, 64995UL + 62UL);
	data = irb[1];
}



when (data >0 && data<32)
{
	nvo_TV_signal = data;
	data = 255;
	delay (5000);
	TV_Reset = 1;
}
 
when (data >32 && data<128)
{
	nvo_TV_signal = data;
	data = 255;
	delay (5000);
	TV_Reset = 1;
} 

when (data >128 && data<254)
{
	nvo_TV_signal = data;
	data = 255;
	delay (5000);
	TV_Reset = 1;
} 

when(timer_expires (TV_Reset))
{
	nvo_TV_signal = 255;
}


when (wink)
{
	io_out(DO1, TRUE);
	delay(30000);
	io_out(DO1, FALSE);
	delay(30000);
	io_out(DO1, TRUE);
	delay(30000);
	io_out(DO1, FALSE);
	delay(30000);
	io_out(DO1, TRUE);
	delay(30000);
	io_out(DO1, FALSE);
}




    Source: geocities.com/hk/cloud_fan_school/Program/LCD/other

               ( geocities.com/hk/cloud_fan_school/Program/LCD)                   ( geocities.com/hk/cloud_fan_school/Program)                   ( geocities.com/hk/cloud_fan_school)                   ( geocities.com/hk)