/* demo 9 : Signal Frequency Measure			       */
/* step 1 : Clock1 select 10M				       */
/* step 2 : CH1 select external GATE			       */
/* step 3 : CH2 select external GATE			       */
/* step 4 : external signal 1 connect to (pin2,pin1)	       */
/*	    pin2 = CLK1 input				       */
/*	    pin1 = GND					       */
/* step 5 : external signal 2 connect to (pin4,pin1)	       */
/*	    pin4 = CLK2 input				       */
/*	    pin1 = GND					       */
/* step 6 : connect pin14 to pin21 & pin22		       */
/*	    pin14=10ms High pulse			       */
/*	    pin21=external gate of CNT1 		       */
/*	    pin22=external gate of CNT2 		       */
/* step 7 : run demo9.exe, the frequency of external signal 1  */
/*	    & external signal 2 will be shown in the screen.   */
/* NOTE------------------------------------------------------- */
/* 1. this demo program measure two-channel only	       */
/* 2. you can modify this program to measure CNT0/1/2/3 &      */
/*    CNT/7/8/9/10 --> total 8 channels at the same time       */
/* ----------------------------------------------------------- */

#include <stdio.h>
#include <stdlib.h>
#include  <dos.h>
#include  <conio.h>
#include  <math.h>
#include  <graphics.h>

#define   WORD	    unsigned int
#define   DWORD     unsigned long
#define   UCHAR     unsigned char

void tmc10_di(WORD *wDi);
WORD tmc10_select8254(char cChip);
WORD tmc10_c0(char cConfig, char cLow, char cHigh);
WORD tmc10_c1(char cConfig, char cLow, char cHigh);
WORD tmc10_c2(char cConfig, char cLow, char cHigh);
WORD wBaseAddr,wIrq,wPLX;

#define NoError 		0

int main()
{
int  i,j,k;
WORD wBoards,wRetVal;
char c,cc[80];
unsigned int high,low,count,cout0,count2,low2,high2;
float f,t;

clrscr();
wBaseAddr=0x200;
wIrq=5;

/*	  (1) CNT5-CNT6 will generate square-wave, 10ms HIGH_PULSE */

outportb(wBaseAddr+5,0);
tmc10_select8254(1);		    /* select 8254-chip-1	   */
tmc10_c1(0x76,32,3);		    /* 8M/800=10K		   */
tmc10_c2(0xb6,200,0);		    /* 10k/200=0.2K		   */
				    /* so High_pulse=10 ms &	   */
				    /*	  Low_pulse =10 ms	   */

/*	  (2) read until COUT6 = LOW				   */
for (;;)
    {
    i=inportb(wBaseAddr+5)&0x10;    /* data sheet is error	   */
				    /* EXT_GATE1~4  in wBase+5	   */
				    /* EXT_GATE7~10 in wBase+4	   */
    if (i==0) break;
    if (kbhit()!=0) {getch(); return;}
    }

/*	  (3) initial CNT1 & CNT2				   */
tmc10_select8254(0);		    /* select 8254-chip-0	   */
tmc10_c0(0x30,0xff,0xff);	    /* down count from 0xffff	   */
tmc10_c1(0x70,0xff,0xff);	    /* down count from 0xffff	   */

/*	  (4) read until COUT6 = HIGH				   */
for (;;)
    {
    i=inportb(wBaseAddr+5)&0x10;    /* data sheet is error	   */
				    /* EXT_GATE1~4  in wBase+5	   */
				    /* EXT_GATE7~10 in wBase+4	   */
    if (i!=0) break;
    if (kbhit()!=0) {getch(); return;}
    }

/*	  (5) read until COUT6 = LOW				   */
for (;;)
    {
    i=inportb(wBaseAddr+5)&0x10;    /* data sheet is error	   */
				    /* EXT_GATE1~4  in wBase+5	   */
				    /* EXT_GATE7~10 in wBase+4	   */
    if (i==0) break;
    if (kbhit()!=0) {getch(); return;}
    }

low=inportb(wBaseAddr); 	   /* read the count of signal 1   */
high=inportb(wBaseAddr);
count=(0xff-high)*256+(0xff-low)+2;

low2=inportb(wBaseAddr+1);	   /* read the count of signal 2   */
high2=inportb(wBaseAddr+1);
count2=(0xff-high2)*256+(0xff-low2)+2;

/* High_pulse of COUT0 = 10 ms */

t=10.0/(float)count;	/* ms	 */
f=(1.0/t)*1000.0;	/* f=1/T */
printf("\nhigh=%x,  low=%x,  count=%d  : external frequency1 = %f Hz",high,low,count,f);

t=10.0/(float)count2;	/* ms	 */
f=(1.0/t)*1000.0;	/* f=1/T */
printf("\nhigh2=%x, low2=%x, count2=%d : external frequency2 = %f Hz",high2,low2,count2,f);

}

/* -------------------------------------------------------------------- */

WORD tmc10_select8254(char cChip)
{
outportb(wBaseAddr+4,cChip);
return(NoError);
}

WORD tmc10_c0(char cConfig, char cLow, char cHigh)
{
outportb(wBaseAddr+3   ,cConfig);
outportb(wBaseAddr     ,cLow);
outportb(wBaseAddr     ,cHigh);
return(NoError);
}

WORD tmc10_c1(char cConfig, char cLow, char cHigh)
{
outportb(wBaseAddr+3   ,cConfig);
outportb(wBaseAddr+1   ,cLow);
outportb(wBaseAddr+1   ,cHigh);
return(NoError);
}

WORD tmc10_c2(char cConfig, char cLow, char cHigh)
{
outportb(wBaseAddr+3   ,cConfig);
outportb(wBaseAddr+2   ,cLow);
outportb(wBaseAddr+2   ,cHigh);
return(NoError);
}

