//This sample measures frequency and pulse width and generates a frequency //using the 8254 counter/timer #include #include #include "8254ctr.h" #include unsigned AskForBaseAddress(unsigned int OldOne) { char msg[7]; int NewOne = 0, Success = 0, Dummy; int AddrInputPosX, AddrInputPosY; puts("Please enter the Base Address for your card (in hex)"); printf("or press ENTER for %X.\n>", OldOne); AddrInputPosX = wherex(); AddrInputPosY = wherey(); do { gotoxy(AddrInputPosX, AddrInputPosY); clreol(); msg[0] = 5; msg[1] = 0; cgets(msg); sscanf(msg + 2, "%x", &NewOne); Success = 1; Dummy = NewOne; if (msg[1] == 0) { gotoxy(AddrInputPosX, AddrInputPosY); printf("%X", OldOne); Success = 1; Dummy = OldOne; } } while(!Success); return (Dummy); } /* end of AskForBaseAddress */ unsigned AskForCountAddress(void) { char msg[7]; int NewOne = 0, Success = 0, Dummy; int AddrInputPosX, AddrInputPosY; puts("Please enter the offset for Counter 0 for your card (in hex)."); printf(">"); AddrInputPosX = wherex(); AddrInputPosY = wherey(); do { gotoxy(AddrInputPosX, AddrInputPosY); clreol(); msg[0] = 5; msg[1] = 0; cgets(msg); sscanf(msg + 2, "%x", &NewOne); Success = 1; Dummy = NewOne; } while(!Success); return (Dummy); } /* end of AskForCountAddress */ void main(void) { unsigned freq=0; float time = 0; unsigned BASE = 0xFCA0; unsigned count = 0; clrscr(); BASE=AskForBaseAddress(BASE); clrscr(); count=AskForCountAddress(); //get counter offset BASE+=count; //add counter offset to base address delay(100); puts("8254 Counter Sample"); puts("--This program measures frequency and pulse width and generates frequency."); puts("--To measure frequency, connect a source to Clk IN on Pin 33."); puts(""); puts("Press any key to continue."); getch(); while(!kbhit()){ freq = freq; time = frequency_measure(BASE); printf("\nThe frequency is %10.3f\n\n", time); puts("Press any key to continue to pulse width test."); gotoxy(1,wherey()-4); } getch(); clrscr(); //is this correct??? puts("--To measure pulse width, connect a source to Gate 1 on Pin 34."); puts("--Without a source, pulse width will read zero."); puts("--Ground Pin 37."); puts(""); puts("Press any key to continue."); getch(); while(!kbhit()){ time = pulse_width(BASE); printf(" Pulse1 %g \n", time); delay(1000); time = pulse_width(BASE); printf(" Pulse2 %g \n", time); delay(397); time = pulse_width(BASE); printf(" Pulse3 %g \n", time); time = pulse_width(BASE); printf(" Pulse4 %g \n", time); delay(300); time = pulse_width(BASE); printf(" Pulse5 %g \n", time); delay(10); time = pulse_width(BASE); printf(" Pulse6 %g \n\n", time); puts("Press any key to continue to next test."); gotoxy(1,wherey()-8); } getch(); clrscr(); while(!kbhit()){ puts("Generating frequency..."); puts("Verify signal by connecting a frequency meter to Out 2 on Pin 35."); puts("Press any key to continue."); generatefrequency(BASE, 25000); gotoxy(1,wherey()-3); } getch(); //delay(2000); }