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

      PWM.C

      BL11xx/Little Giant with IOE-A/D expansion board

   pulse input at WAGO J6-1 

   counting clock can be selected from J17 of PCLK
   PAL fp04100 divide counting clock by 4
   for system clock of 9.216MHz each count is 0.434 uS
   count clock input to CTC3 TRG3
   use TRG2/POL as a polarity latch for positive/negative width
   POL = 0 Measure Negative Pulse Width
   POL = 1 Measure Positive Pulse Width

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

int i, l, m;
unsigned int n;
char flag, cnt;

main()
{

	hitwd();
   outport (CTC0, CTC3_VEC);  /* set interrupt vector */
   outport (PAL, 0);      		/* EN=0  */
   outport (POL, 0);      		/* POL=0 Measure Negtive Pulse Width */

  /* POL=1 Measure Positive Pulse Width */

   while(1)
   {
   	hitwd();
      cnt = 0;
      flag = 0;
		runwatch();
      
      outport (CTC3, 0x3);       /* software reset */
      outport (CTC3, 0xc5);      /* int, counter, time constant, cont. */
      outport (CTC3, 0xff);      /*   255   */
      ISET (ITC, 2);             /* enable INT2 */
      outport (PAL, 1);          /* EN=1  */

      while (!flag) {
      	runwatch();
      	hitwd();
      }
   
      m = inport (CTC3);
      n = cnt * 255 + (255 - m);
      ++l;
      printf ("CTC int cnt = %d   PCLK - TRG3 n = %u %d\n", cnt, n, l);
      for (i = 0; i < 10000; ++i) hitwd();
   }
}

#INT_VEC CTC3_VEC ctc3int

#asm nodebug
ctc3int::
   push af
   ld a, (cnt)
   inc a
   ld (cnt), a
   pop af
   ei
   reti
#endasm


#INT_VEC INT2_VEC int2int
interrupt ret int2int()
{
   outport (PAL,0);           /* EN=0 */
   IRES (ITC, 2);             /* disable INT2 */
   flag = 1;
}            

