#use "default.h"

/*
  DASAMPL1.C - XP8500/DAC PLCBus Expansion board sample program #1.
               For any board with a PLCBus.


     This program simply increments the voltage output on both 
  XP8500/DAC output channels.  The board written to is the one one with 
  the highest logical address (0-63) found on the PLCBus.  To watch
  the output increment, connect a voltage meter to either output.
  Alternatively, you can place an LED in series with a 1K resistor 
  between either output and ground and watch the intensity gradually
  increase as the voltage rises.


  Setup - Hardware            |  DAC wago H1 connections (from left)              
                              |  -----------------------------------   
  1. Connect the XP8500/DAC   |  1 - Ch2 output      5 - Vcc (+5v)     
     board to a controller    |  2 - Gnd             6 - V-            
     with a standard PLCBus   |  3 - Ch1 output      7 - V+ (+15v)
     expansion cable.         |  4 - Gnd             8 - +24VI         
                               ----------------------------------------
     See the PLCBus Expansion Boards Technical Manual for more details.


  2. Connect voltage meter or LED/resistor network to either output.
  
     LED/Resistor Network      
     --------------------     /
                           /\/
                          /
     DAC output         |\ |     /\   /\       |\
     Ch1 or 2     *-----| >|-----  \ /  \  ----| >  Gnd 
     H1-1 or H1-3       |/ |        \    \/    |/   H1-2 or H1-4
                   
                         LED      1K Resistor


  3. Make sure J1 and J2 are jumpered to connect pins 2-3.  This
     configures both channels for voltage output.

  4. Apply +24V power to the controller.
*/


main()  
{
int i,logical_addr,plcbus_addr,da_addr,hi_addr,ifail,vfail;
float adv,exp,err,offset,ma; 

	reset_pbus();           // reset the plcbus
	reset_pbus_wait();      // wait for reset to end   

	if(sysclock() > 0x1e00)
		reset_pbus_wait();   // for boards faster than 9 Mhz
									// double wait.         

	/* Locate D/A Nodes - save addr of last found 
	--------------------------------------------- */
	for(logical_addr=0;logical_addr<64;logical_addr++)
	{
		hitwd();
		if(find_dac(logical_addr))
		hi_addr = logical_addr;
	}

   /* Set PLCBus address to last XP8500/DAC board located and initialize DAC
   ------------------------------------------------------------------ */
   set12adr(plcdac_addr(hi_addr));
   dac_init();           
   dac_switch_on();       /* turn on high-side switch if installed */


   /* Increment Voltages on DAC Channels V/I1 and V/I2
   ------------------------------------------------- */
   while(1)
   {
     	for (i=0;i<4097;i+=1)
     	{
     		hitwd();
     		plc_fdac1(i);         /* Use fast/big dac write function on ch1 */
       	dac_latch1();         /* assert DAC value previously written */
       	plc_sdac2(i);         /* Use slow/small DAC write function on ch2 */
       	dac_latch2();         /* assert DAC value previously written */
     	}
   }
}