/*     
   CTCDEMO.C

   BL11xx/Little Giant

   This program sets up CTC for periodic interrupts 200 times per
   second. It counts to 5000 and then exits. This takes 25 seconds.

   DOS:   While the program is running, enter "counter" and strike the
   		 carriage return repeatedly.

	Windows: Add counter to the watch list using
				Inspect|Add/Del Watch Expression	and hit Ctrl U repeatedly.	

   The current value of "counter" is 
   displayed in the watch window. You can also experiment with the 
   command inport (CTC2) in the watch window.

   This program shows the use of the CTC interrupt and the use of the
   runwatch () function. The runwatch () function executes watch
   statements while the target program is executing so that it
   is possible to "snoop" while a program runs.

   This program shows the use of the "shared" keyword. Whenever
   a shared variable is accessed the access is performed in an atomic
   fashion by turning off the interrupts (if on) during the multi-
   byte read or store of the variable. This avoids the possiblity
   of an interrupt routine changing a variable between reading
   the first bytes and the last bytes, which results in reading a 
   corrupted value.

   This program shows the method for writing interrupt routines
   using the C language.  It also shows how to set an interrupt vector
   with #INT_VEC. 

   Examine LGIANT.LIB for the source of the routine setctc ().

*/

shared unsigned long  int counter;   /* shared between different interrupt levels */

main()
{
   outport ( DCNTL, 0x30 );
   counter = 0x0L;            /* initialize counter to zero */

   setdaisy ( 3 );            /* setup KIO daisy chain      */

   setctc ( 2, 1, 180, 1 );   /* initialize ctc, enable interrupts */

   while(1)  
   {
   	hitwd();
      runwatch ();            /* enables watches to run while target runs */
      if ( counter >= 5000) 
         break;
      outport ( ENB485, !( counter & 64 ) );  /* flash light */
   }
   
   outport ( CTC2, 3 );       /* reset CTC2 */

   puts ( "Counter has reached 5000.\n" );

}

#INT_VEC CTC2_VEC ccc         /* set interrupt vector for interrupt routine */

/* this interrupt routine increments the variable "counter" */

interrupt reti int ccc ()
{
   counter = counter + 1;  
}

/***********************************************End of CTCDEMO.C *****/
