/*-----------------------------------------------------------------------*/
/* filename=demo_08.c							 */
/*									 */
/* pulse width measurement						 */
/* IO BASE address = 0x200, all DIP switchs of SW1 in OFF position	 */
/*									 */
/* CLOCK1=1.6M, CH1=CLOCK1=1.6M, GATE1=EXTERNAL GATE CONTROL		 */
/* PULSE SIGNAL --> EXT_GATE1=PIN 21 OF CN1				 */
/* CLK=1.6M=0.000625 ms 						 */
/*-----------------------------------------------------------------------*/

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

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

void main(void)
{
int i,high,low;
unsigned int width;

outportb(BASE+4,0);	/* select 8254 chip_1 --> counter_1,2,3 */

while ((inportb(BASE+5)&0x10)==0) ; /* wait until EXT_GATE1 HIGH */
while ((inportb(BASE+5)&0x10)!=0) ; /* wait until EXT_GATE1 LOW  */

/* CLK1=CLOCK1=1.6M */
outportb(BASE+3,0x30);	/* mode_0, counter_0 */
outportb(BASE,0xFF);	/* counter_0 low byte first */
outportb(BASE,0xFF);	/* counter_0 high byte */

while ((inportb(BASE+5)&0x10)==0) ; /* wait until EXT_GATE1 HIGH */
while ((inportb(BASE+5)&0x10)!=0) ; /* wait until EXT_GATE1 LOW  */

outportb(BASE+3,0x00);	  /*latch counter_0 */
low=inportb(BASE)&0xff;
high=inportb(BASE)&0xff;
printf("\nhigh=%x,low=%x",high&0xff,low&0xff);
width=(0xff-high)*256+0xff-low+2;
printf("\nwidth=%u,high=%x,low=%x",width,high&0xff,low&0xff);
printf(" --> %f ms",(float)width*0.000625);
}
