/*-----------------------------------------------------------------------*/
/* filename=demo_06.c							 */
/*									 */
/* software event counter						 */
/* IO BASE address = 0x200, all DIP switchs of SW1 in OFF position	 */
/* NOTE : (1) The 8254 need 2 CLKs to write initial counter values	 */
/*	  (2) The starting two event CLKs will be used to initialize 8254*/
/*	  (3) Before initialization, the event counters holds their old  */
/*	      value.							 */
/*-----------------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>

#define  BASE 0x200		/* IO BASE address = 0x200 */

void main(void)
{
int i,high,low;
float event_counter,c65536;
char s0;

c65536=0.0; s0=0;
outportb(BASE+4,0);	/* select 8254 chip_1 --> counter_1,2,3 */
/* CLK1=EXTERNAL CLOCK */
outportb(BASE+3,0x30);	/* mode_0, counter_0 */
outportb(BASE,0xFF);	/* counter_0 low byte first */
outportb(BASE,0xFF);	/* counter_0 high byte */

/* WARNING : Before the starting two event CLKS come, the event_counters
	     hold their old value */
for (;;)
	{
	outportb(BASE+3,0x00);	/* latch counter_0 */
	low=inportb(BASE)&0xff;
	high=inportb(BASE)&0xff;
	if (high==0) s0=1;
	if ((high==0xff) & (s0==1))
		{
		c65536+=1.0;
		s0=0;
		}
	event_counter=c65536*65536.0+(0xFF-high)*256.0+(0xFF-low)+2;
					      /* plus the starting two CLK */

	printf("\nevent_counter=%f,high=%x,low=%x",
		event_counter,high&0xff,low&0xff);
	delay(500);
	if (kbhit()!=0) {getch(); return;}
	}
}
