/*-----------------------------------------------------------------------*/
/* filename=demo_03.c							 */
/*									 */
/* Square wave generator						 */
/* IO BASE address = 0x200, all DIP switchs of SW1 in OFF position	 */
/* CLOCK1 select 8M,  CLOCK2 select 80K 				 */
/* CH1 select CLOCK2, COUT1=80K/10=8K --> single counter		 */
/* COUT5=8M/100=80K,COUT6=80K/80=1K --> cascaded counter		 */
/* CH7 select CLOCK1, CH8 select COUT7, CH9 select COUT8		 */
/* COUT7=8M/100=80K,COUT8=80K/80=1K,COUT9=1K/100=10 -->multi-counter	 */
/*-----------------------------------------------------------------------*/

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

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

void main(void)
{
/* ---------------- single counter --------------------------------------*/
outportb(BASE+4,0);	/* select 8254 chip_1 --> counter_1,2,3 */
/* CLK1=CLOCK2=80K --> COUT1=CLK1/10=8K */
outportb(BASE+3,0x36);	/* mode_3, counter_0 */
outportb(BASE,10);	/* counter_0 low byte first */
outportb(BASE,0);	/* counter_0 high byte */

/* ---------------- cascaded counter ------------------------------------*/
outportb(BASE+4,1);	/* select 8254 chip_2 --> counter_4,5,6 */
/* CLK5=CLOCK1=8M  --> COUT5=CLK5/100=80K */
outportb(BASE+3,0x76);	/* mode_3, counter_5 */
outportb(BASE+1,100);	/* counter_5 low byte first */
outportb(BASE+1,0);	/* counter_5 high byte */
/* CLK6=COUT5=80K  --> COUT6=CLK6/80=1K */
outportb(BASE+3,0xb6);	/* mode_3, counter_6 */
outportb(BASE+2,80);	/* counter_6 low byte first */
outportb(BASE+2,0);	/* counter_6 high byte */

/* ---------------- multiple counter ------------------------------------*/
outportb(BASE+4,2);	/* select 8254 chip_3 --> counter_7,8,9 */
/* CLK7=CLOCK1=8M  --> COUT7=CLK7/100=80K */
outportb(BASE+3,0x36);	/* mode_3, counter_7 */
outportb(BASE,100);	/* counter_7 low byte first */
outportb(BASE,0);	/* counter_7 high byte */
/* CLK8=COUT7=80K --> COUT8=CLK8/80=1K */
outportb(BASE+3,0x76);	/* mode_3, counter_8 */
outportb(BASE+1,80);	/* counter_8 low byte first */
outportb(BASE+1,0);	/* counter_8 high byte */
/* CLK9=COUT8=1K --> COUT9=CLK9/100=10 */
outportb(BASE+3,0xb6);	/* mode_3, counter_9 */
outportb(BASE+2,100);	/* counter_9 low byte first */
outportb(BASE+2,0);	/* counter_9 high byte */
}
