/*
Demo for 8084 (Pulse/Dir mode)

Software:
    Compiler: BC++ 3.1
    Compile mode: large
    Project: PulseDir.c
             8084L.Lib
             8000L.Lib

Hardware: I-8000 MCU + 8084

Channel configurations:
    Xor=0 for Low Actived (signal from High to Low ==> change count)
    Low pass filters disabled
    all channel are set to Pulse/Dir counter
    
Wiring:
    active:   connected to 4.5~24V for isolated input
              connected to 2~5V for non-isolated input (TTL signal)
    inactive: open or connected to GND
    
    For Ch0, Ch1(A0,B0)
        if (B0 is active)    --> A0 = Up counting Ch0, Ch1
        if (B0 is inactive)  --> A0 = Down counting Ch0, Ch1
        
    For Ch2, Ch3(A1,B1)
        if (B1 is active)    --> A1 = Up counting Ch2, Ch3
        if (B1 is inactive)  --> A1 = Down counting Ch2, Ch3

    For Ch4, Ch5(A2,B2)
        if (B2 is active)    --> A2 = Up counting Ch4, Ch5
        if (B2 is inactive)  --> A2 = Down counting Ch4, Ch5
        
    For Ch6, Ch7(A3,B3)
        if (B3 is active)    --> A3 = Up counting Ch6, Ch7
        if (B3 is inactive)  --> A3 = Down counting Ch6, Ch7        
*/

/*
[2004,Dec,23] by Kevin
    Use 8084 lib version 2.0.0
    
[2004,Mar,07] by Kevin
    Use 8084 lib version 2.0.1
    Clear all count at beginning.
*/

#include "..\..\..\..\lib\vp2k.h"
#include "..\..\..\..\lib\8084W.h"
main(void)
{
    int i,slot,channel,iRet;
    unsigned long T1,T2,T3;
    long Counter;
    int Overflow;
    unsigned int DIvalue;

    Print("/*********************************/\n\r");
    Print("/*   8084 demo (Pulse/Dir mode)  */\n\r");
    Print("/*                               */\n\r");
    Print("/*   [2004,Mar,07]               */\n\r");
    Print("/*********************************/\n\r");
    Print("Press any key to clear counter (A0/B0).\n\r");
    Print("Press 'q' to quit.\n\r\n\r");
    
    Print("Slot number (0~7)=");
	Scanf("%d",&slot);
    iRet=i8084W_InitDriver(slot);
    if (iRet==(-1))
    {
		Print("Initiate 8084 on slot%d error!\n\r",slot);
		Print("  Cannot find 8084.");
	}
	else
	{
		Print("Initiate 8084 on slot%d ok.\n\r",slot);
        if(iRet>0)
        {
            Print("  Some Pulse/Dir channels have one count offset.\n\r");
            Print("  Return code:%02X\n\r",iRet);
        }
        
        for (channel=0; channel<8; channel++)
        {
            i8084W_SetXorRegister(slot,channel,0); // XOR=0 (Low Actived)
            iRet=i8084W_SetChannelMode(slot,channel,0); // Pulse/Dir counter mode
                //mode 0: Pulse/Dir counter mode
                //     1: Up/Down counter mode
                //     2: frequency mode
                //     3: Up counter mode
            if(iRet>0)
                Print("The Pulse/Dir channel %d has one count offset.\n\r",channel);    
            
            i8084W_SetLowPassFilter_Status(slot,channel,0); //Disable LPF
            i8084W_SetLowPassFilter_Us(slot,channel,1);//Set LPF width= 0.001 ms
        }
        
        //Clear all count at beginning.
        for (channel=0; channel<8; channel++)
        {
            i8084W_ClrCnt(slot,channel);
        }
        
        T1=*TimeTicks;
        T2=T1;
        for (;;)
        {
            T3=*TimeTicks;
            if ((T3-T1) >50) // Auto scan 8084 every 50 ms (one time one slot)
            {
                T1=T3;
                i8084W_AutoScan();
            }
        
            if ((T3-T2) >500) // Print frequency every 500 ms
            {
                T2=T3;
                i8084W_ReadCntPulseDir(slot,0,&Counter,&Overflow);
                Print("Pulse/Dir:  A0/B0 Count=%08ld   ",Counter);
                
                i8084W_ReadCntPulseDir(slot,2,&Counter,&Overflow);
                Print("A1/B1 =%08ld   ",Counter);
                
                i8084W_ReadCntPulseDir(slot,4,&Counter,&Overflow);
                Print("A2/B2 =%08ld   ",Counter);                
                
                i8084W_ReadCntPulseDir(slot,6,&Counter,&Overflow);
                Print("A3/B3 =%08ld  ",Counter);
                
                i8084W_ReadDI_Xor(slot,&DIvalue);
                Print("DI=%x \n\r",DIvalue);  
                
            }
        
            if (Kbhit())
            {
                if(Getch()=='q') //Press 'q' to quit the program.
                {
                    return;
                }
                else //Press any key to clear counter 0.
                {
                    i8084W_ClrCnt_All(slot);
                    Print("Clear counter All.\n\r");
                    if(iRet>0)
                    Print("The Pulse/Dir channel 0 has one count offset.\n\r");   
                }
            }
        }
    }
    
    return;
}

