/******************************************************************\

   IOE_OPTO.C

   BL11xx/Little Giant with I/O Expansion board

   Always run exp_init () first to initialize the PPI.

   int exp_init (int ppia, int ppib, int ppicl, int ppicu);

   Use 0 to specify as output.  Use -1 to specify as inputs.

   Digital I/O is accomplished by reading and writing to the 
   appropriate I/O registers.
   
   Use to determine the number of samples required to detect AC signal.
   Insert H11AA1 into U9 and apply AC input to J4-1 and J4-2.  If using
   120 VAC you will need to change the value of the resistor in RP1 to
   adjust the current flow through the optoisolater.
   
   A more accurate system would use either the CTC or PRT to generate
   a timed interrupt and the samples would be taken at these intervals.
      
\******************************************************************/
int off, on;


main()
{

   exp_init (-1,-1,-1,-1);                         /* PPI all inputs */

   while(1)
   {
   	hitwd();
      off = on = 0;
      count_wv ();
      printf ("On = %d Off = %d\n", on, off);
   }
}

nodebug count_wv ()                   /* nodebug for more accurate counts */
{
      /* make sure that the count starts at the start of a cycle */
      while (!(inport (PPIC) & 1)) hitwd();   
      while ((inport (PPIC) & 1)) hitwd();
      while (!(inport (PPIC) & 1)) hitwd();
      
      while (inport (PPIC) & 1) {
      	++off;    /* do the actual count now */
      	hitwd();
      }
      while (!(inport (PPIC) & 1)) {
      	++on;
      	hitwd();
      }
}

/***************************************** End of IOE_OPTO.C ************/
