/* FILE: 3SQWAVE.C DATE: 03/18/99 AUTH: P.OH. Boondog Automation DESC: Counters are programmed in Mode 3 square wave User enters in desired counter and frequency between 16 Hz and 500 kHz */ #include #include #include #define crystalValue 1000000 #define TRUE 1 #define FALSE 0 void main(void) { int baseAddress; int counter0, counter1, counter2, counter, counterNumber; int CNTRL; int gateAddress, gateNumber, gateValue, gateOn; long freq; /* thus freq is a 32-bit value ranging from 0 to 4.2E9 */ long actFreq; unsigned int counterValue; /* counterValue ranges from 0 to 65535 */ int validChoice; window(5,5,75,30); clrscr(); gotoxy(1,1); cprintf("Base Address is assumed to be 608 decimal\n"); baseAddress = 608; counter0 = baseAddress; counter1 = baseAddress + 1; counter2 = baseAddress + 2; CNTRL = baseAddress + 3; gateAddress = baseAddress + 4; gateOn = FALSE; /* setup all counters for Mode 3 square wave, LSB then MSB loading */ /* Counter 0 Binary word: 00 11 011 0 = 54 d */ /* Counter 1 Binary word: 01 11 011 0 = 118 d */ /* Counter 2 Binary word: 10 11 011 0 = 182 d */ outportb(CNTRL, 54); /* counter 0 */ outportb(CNTRL, 118); /* counter 1 */ outportb(CNTRL, 182); /* counter 2 */ do { gotoxy(1,4); cprintf("Select a counter: \n"); gotoxy(1,5); cprintf("(0) Counter 0\n"); gotoxy(1,6); cprintf("(1) Counter 1\n"); gotoxy(1,7); cprintf("(2) Counter 2\n"); gotoxy(20,4); scanf("%d", &counter); switch(counter) { case 0 : /* counter 0 */ counter = counter0; validChoice = TRUE; gateValue = 1; /* set gate0 bit on */ gateNumber = counterNumber = 0; break; case 1: /* counter 1 */ counter = counter1; validChoice = TRUE; gateValue = 2; /* set gate1 bit on */ gateNumber = counterNumber = 1; break; case 2: /* counter 2 */ counter = counter2; validChoice = TRUE; gateValue = 4; /* set gate0 bit on */ gateNumber = counterNumber = 2; break; default: /* invalid choice */ gotoxy(1,2); cprintf("Must choose 0, 1 or 2 !!\n"); validChoice = FALSE; break; }; /* end of switch */ } while(!validChoice); /* frequency calculations */ do { gotoxy(1,16); cprintf("Enter an integer frequency between 16 to 500000 Hz: "); scanf("%ld", &freq); gotoxy(1,17); cprintf("Type 0 to quit or 1 to toggle gate\n"); if( freq > 1 ) { /* calculate values to load into counter */ counterValue = (unsigned int)(crystalValue / freq); actFreq = (long)(crystalValue/counterValue); /* load LSB first then MSB second */ outportb(counter, counterValue & 0x00FF); outportb(counter, counterValue >> 8 ); outportb(gateAddress, gateValue); gateOn = TRUE; } else if( freq == 1) { if(gateOn) { /* then toggle off */ outportb(counter, 0 & 0x00FF); outportb(counter, 0 >> 8); outportb(gateAddress, 0); gotoxy(1,22); cprintf("gate %d is turned off\n", gateNumber); gateOn = FALSE; } else { /* then gate was off, so toggle on */ outportb(counter, counterValue & 0x00FF); outportb(counter, counterValue >> 8); outportb(gateAddress, gateValue); gotoxy(1,22); cprintf("gate %d is turned on \n", gateNumber); gateOn = TRUE; } } gotoxy(53,16); cprintf(" "); /* clear input field */ gotoxy(1,12); cprintf("Counter %d Freq: Desired = %ld Hz, Actual = %ld Hz\n", counterNumber, freq, actFreq); gotoxy(1,15); cprintf("Enter another frequency if you want\n"); } while (freq != 0 ); clrscr(); cprintf("Finished! Turning off gates\n"); outportb(gateAddress, 0); /* turn off gate */ } /* end of main */