/*
	DMACOUNT.C

	PK21xx/Rugged Giant and PK22xx/Little Star Sample Program illustrating the
	use of DMA counters and watch window evaluation of functions.

	This is a sample program that illustrates the use of the high speed counter
	for detection of negative-going edges.

	For the PK21xx/Rugged Giant:

		As a mockup pulse generator, connect one side of a 2.2k resistor to +5v
		and the other side to OUT7 (or O7). Also connect O7 to C1A (for DMA0) or
		C2A (for DMA1). Connect C1B and C2B+ to ground. Connect C2B- to +5v.

	For the PK22xx/Little Star:

		Connect HV07 to PIN13 (DIGITAL INPUT 13) for DMA0 high speed counter.
		Connect HV07 to PIN14 (DIGITAL INPUT 14) for DMA1 high speed counter.

		For the High speed counter to work, the capacitor pack CN2 has to be
		removed.

	Using the watch window, type one of the following to get the appropriate
	responce.
	
	   "pulse(xxx)" will cause OUT7 (O7 or HV07) to generate xxx negative edges.

   	"DMA0Count(xxx)" will reload DMA 0 to count xxx negative edges.
	   "DMA1Count(xxx)" will reload DMA 1 to count xxx negative edges.

   DMA1Count() and DMA0Count() reset their respective flags when called.
   The program starts by reloading both DMA's.
*/

#use "default.h"
#use cplc.lib

#if !(BOARD_TYPE==CPLC_BOARD || BOARD_TYPE==L_STAR)
#fatal "This program only runs on a PK21xx/Rugged Giant or a PK22xx/Little Star"
#endif

#define NOLCD

main()
{
    unsigned count1, count0;
    unsigned old1, old0;
    
    uplc_init();

    old1 = old0 = count1 = count0;

    DMA0Count(100);
    DMA1Count(100);

    while (1)
    {
        while (!_DMAFLAG0 && !_DMAFLAG1)
        {
            runwatch();

            DMASnapShot(0, &count0);
            DMASnapShot(1, &count1);

            if (old0 != count0)
            {
                printf("Counter 0 has %u counts\n", count0);
                old0 = count0;
            }
            if (old1 != count1)
            {
                printf("Counter 1 has %u counts\n", count1);
                old1 = count1;
            }
        }
        if (_DMAFLAG0)
        {
            printf("DMA0 finished counting\n");
            _DMAFLAG0 = 0;
        }
        if (_DMAFLAG1)
        {
            printf("DMA1 finished counting\n");
            _DMAFLAG1 = 0;
        }
    }
}

/*****************************************************************************
int pulse(unsigned int times)
 - artificial generator of negative edges.

 - For the PK21xx/Rugged Giant:
   Tie one side of a 2.2k to 5v and the other side to O7. Use the O7 side
   to feed negative edges to C1A and C2A.

 - For the PK21xx/Little Star:
   Connect OUT7 to PIN13 (DMA0) and PIN14 (DMA1).
*****************************************************************************/

int pulse(unsigned int times)
{
    unsigned int i;
    unsigned int k;

    for (i = 0; i < times; i++)
    {
        outport(DRV7, 0);
        outport(DRV7, 0xff);
    }
    outport(DRV7, 0);
}

