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

/*
	ADSAMPL2.C

	A/D EXPANSION BOARD SAMPLE PROGRAM #2

   This Program Demonstrates the differences between compensated and
   uncompensated readings of each A/D channel.

   1)  Creates a compensated and uncompensated analog table entry for 
       each A/D channel 0-18.  All channels use input mux pair AN0 +/-.
   2)  Displays actual voltage using both compensated and non-compensated
       calculations.

   Use <f3> to compile.
   Use <f2> to set breakpoints if desired.
   Use <f9> to run.
*/

/* Define the analog signal table - 18 entries
---------------------------------------------- */
struct signal_rec adtab[18];

main()
{
int i,bd_addr,rd;
float volc,volu;
char utag,ctag;

  /* 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 */


  printf("\nVOLTAGE READINGS AT EACH A/D CHANNEL\n\n");
  printf("* denotes maximum channel value\n\n");

  for (i=0; i<18; i++)
  {
  	hitwd();
   /* Create signal entry in analog signal table
    --------------------------------------------- */
    add_sig_table(0,bd_addr,0,i,1);  /* Create Signal Table w/calibration */
    add_sig_table(1,bd_addr,0,i,0);  /* Create Signal Table w/o calibration */
 
    /* Set the analog mux for this signal and wait for the signal to settle 
    ----------------------------------------------------------------------- */
    set_mux(0);                 
    mxdel18();

    /* Read signal, convert to voltage, and display on CRT
    ------------------------------------------------------ */
    rd = plad_rd12(adch(0));
    if (rd == 2047 * (polarity(adch(0))+1) + polarity(adch(0))) 
      ctag='*'; 
    else 
      ctag= ' ';

    volc = ad_conv(0,rd);
    volu = ad_conv(1,rd);
    printf("ch%d: w/comp: %fV%c     w/o comp: %fV%c\n",i,volc,ctag,volu,ctag);

   }
}
  

