#use "default.h"
#use "kdm.lib"

/*

	ADSAMPL1.C

	A/D EXPANSION BOARD SAMPLE PROGRAM #1

   1)  Creates an analog table containing 1 entry for reading the temp 
       output of the LT1019 at AN0+.
   2)  Continuously reads the value, converts it to a real voltage, and
       displays the results on the CRT.
   3)  Uses compensation data stored in EEPROM for more accurate readings.

   SETUP - Connect U1 (LT1019) pin 3 to Wago H1 slot #4 (AN0+)
           Connect Wago H1 slot #1 (Gnd) to Wago H1 slot #3 (AN0-)

*/

#define TEMP 0

  /* Define the analog signal table - 1 entry
  ---------------------------------------------- */
  struct signal_rec adtab[1];

main()
{
int i, bd_addr, in_ch=0, an_ch=10;   /* Input: AN0, Read A/D Ch #10 (x2 Uni) */
float vol,tempc,tvol;


  /* Prelim-Setup 
  --------------- */
	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.         
  bd_addr = plcad_addr(3);      /* J1 1-2 n.c., 3-4 n.c. = board #3 */
  
  /* Create signal entry in analog signal table
  --------------------------------------------- */
  add_sig_table(TEMP,bd_addr,in_ch,an_ch,1);   /* Make table entry for temp */
 
  /* Set the analog mux for this signal and wait for the signal to settle 
  ----------------------------------------------------------------------- */
  set_mux(TEMP);                 
  mxdel18();

  printf("\nReading voltage and converting to temperature in Centigrade\n\n");
  while(1)
  {   
    /* Read signal 1000 times, take avg.
    ------------------------------------------------------ */
    tvol=0.0;
    for (i=0;i<1000;i++) {
    	hitwd();
      tvol += ad_conv(TEMP,plad_rd12(adch(TEMP)));
    }
    vol = tvol /1000.;

    /* convert to voltage, and display on CRT
    ------------------------------------------------------ */
    tempc = (vol / 0.002) - 273.15; 
    printf("Temperature pin voltage: %f Volts  Temp C: %5.2f\r",vol,tempc);
   }
}