/*
Demo for 8084 (Frequency mode)

Software:
    Compiler: BC++ 3.1
    Compile mode: large
    Project: Freq.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 Freq mode
    
Wiring:
    Ch0=A0=Freq
    Ch1=B0=Freq
    Ch2=A1=Freq
    Ch3=B1=Freq
    Ch4=A2=Freq
    Ch5=B2=Freq
    Ch6=A3=Freq
    Ch7=B3=Freq
*/


/*                  
[2004,Dec,23] by Kevin
    Use 8084 lib version 2.0.0
    
[2004,Mar,07] by Kevin
    Uses 8084 lib version 2.0.1
*/

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

    Print("/*********************************/\n\r");
    Print("/*   8084 demo (Freq mode)       */\n\r");
    Print("/*                               */\n\r");
    Print("/*   [2004,Mar,07]               */\n\r");
    Print("/*********************************/\n\r");
    Print("Press any key 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 ok10000.\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)
            i8084W_SetChannelMode(slot,channel,2); // Frequency mode
                //mode 0: Pulse/Dir counter mode
                //     1: Up/Down counter mode
                //     2: frequency mode
                //     3: Up counter mode

            i8084W_SetLowPassFilter_Status(slot,channel,0); //Disable LPF
            i8084W_SetLowPassFilter_Us(slot,channel,1); //Set LPF width= 0.001 ms
            i8084W_SetFreqMode(slot,channel,1);  
            tout=10000;
            i8084W_SetFreqTimeoutValue(slot,channel,tout);
            //tout=i8084W_ReadFreqTimeoutValue(slot,channel);
            //Print ("tout= %d",tout);
                //mode 0: Auto mode: update time = 330 ms
                //     1: Low mode: update time = 1000 ms
                //     2: High mode: update time = 100 ms
        }
        
        T1=GetTimeTicks();
        T2=T1;
        for (;;)
        {
            T3=GetTimeTicks();
            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_ReadFreq(slot,0,&Frequency);
                Print("A0=%06lu    ",Frequency);
                
                i8084W_ReadFreq(slot,1,&Frequency);
                Print("B0=%06lu    ",Frequency);
                
                i8084W_ReadFreq(slot,2,&Frequency);
                Print("A1=%06lu    ",Frequency);
                
                i8084W_ReadFreq(slot,3,&Frequency);
                Print("B1=%06lu    ",Frequency);
                
                i8084W_ReadFreq(slot,4,&Frequency);
                Print("A2=%06lu    ",Frequency);
                
                i8084W_ReadFreq(slot,5,&Frequency);
                Print("B2=%06lu    ",Frequency);
                
                i8084W_ReadFreq(slot,6,&Frequency);
                Print("A3=%06lu    ",Frequency);
                
                i8084W_ReadFreq(slot,7,&Frequency);
                Print("B3=%06lu",Frequency);
                
                i8084W_ReadDI_Xor(slot,&DIvalue);
                Print(" DI=%x \n\r",DIvalue);            
            }
        
            //Press any key to quit program.
            if (Kbhit()) {Getch(); return;}
        }
    }
    
    return;
}

