/***************************************************************************** * SAMPLE2.C : FREQUENCY MEASUREMENT * * * * This is a demonstration program to be used with the CTR-5 Counter * * timer board. The program will measure frequency from about 1Hz up to * * 65KHz. By changing the number of 1ms periods, the frequency range can * * be changed, but resolution is still only 16 bits. A modification of this* * program cascading two counters could yield 32 bit accuracy. * * This program must be built using the large model. * * * * The board should be set as follows: * * -- On board clock set to 1 MHz * * -- Input is applied to pin 13, counter 5 input. * * * * LAST MODIFICATION: 2/5/98 * * * *****************************************************************************/ #include #include #include #include #include #include "ctr5drvc.h" /* These variables MUST be declared as global. They are the ones whose offsets are passed to the CTR5DRVC routine. If they are not global then the driver will not find their segment. */ unsigned statcode,task; int params[10]; unsigned AskForBaseAddress(unsigned int OldOne) { char msg[7]; int NewOne = 0, Success = 0, Dummy; int AddrInputPosX, AddrInputPosY; puts("\nPlease 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 */ /***************************************************************************** * FUNCTION: call_driver -- local routine * * * * PURPOSE: Performs the call to the driver package. * * * * INPUT: None. * * * * CALLS: ctr5drv - entry point to driver package. * * * * OUTPUT: Returns the error code supplied by the driver routine. * * * *****************************************************************************/ unsigned call_driver() { int *temp; /* this section extracts the offset of the parameters that we will pass to the assembly driver */ ctr5drv(FP_OFF(&task),FP_OFF(params),FP_OFF(&statcode)); /* call the driver */ /* this section checks for an error code */ if (statcode > 0) { printf("A status error code of %i was detected.\n",statcode); printf("Program terminated."); return(statcode); } else return(0); } /* end call_driver */ /***************************************************************************** * FUNCTION: setup -- local routine * * * * PURPOSE: Initializes the driver package. * * * * INPUT: None. * * * * CALLS: call_driver - entry point to driver package. * * * * OUTPUT: Returns the error code supplied by the driver routine. * * * *****************************************************************************/ unsigned setup(unsigned int Address) { unsigned status; clrscr(); gotoxy(20,10); printf("FREQUENCY: Hz"); /* display frequency label on screen*/ gotoxy(15,13); printf("Press any key to exit program."); params[0] = Address; /* starting board address */ params[1] = 1; /* FOUT divider ratio = 1 */ params[2] = 14; /* FOUT source = F4 */ params[3] = 0; /* Counter 2 compare disabled */ params[4] = 0; /* Counter 1 compare disabled */ params[5] = 0; /* time of day disabled */ task = 0; status = call_driver(); /* call driver */ return(status); } /* end setup */ /***************************************************************************** * FUNCTION: get_readings -- local routine * * * * PURPOSE: Uses the driver to measure the freqency of an input. * * * * INPUT: None. * * * * CALLS: call_driver - entry point to driver package. * * * * OUTPUT: Returns the error code supplied by the driver routine. * * * *****************************************************************************/ unsigned get_readings() { unsigned status; params[0] = 1000; /* 1000 1ms intervals = 1 second */ params[1] = 5; /* counter 5 input is source */ params[2] = 0; /* this param returns counts */ task = 10; status = call_driver(); /* call driver */ gotoxy(31,10); printf("%8u",params[2]); /* display the value */ return(status); } /* end get_readings */ /***************************************************************************** * FUNCTION: main -- local routine * * * * PURPOSE: Controls program flow, detects when user is ready to exit. * * * * INPUT: None. * * * * CALLS: setup - set up program and drivers. * * get_readings - read the frequency and display. * * * * OUTPUT: None. * *****************************************************************************/ void main() { unsigned int Address; unsigned status; char ch; clrscr(); printf( " SAMPLE2.C : FREQUENCY MEASUREMENT \n" " \n" " This is a demonstration program to be used with the CTR-5 Counter \n" " timer board. The program will measure frequency from about 1Hz up to \n" " 65KHz. By changing the number of 1ms periods, the frequency range can \n" " be changed, but resolution is still only 16 bits. A modification of this\n" " program cascading two counters could yield 32 bit accuracy. \n" " \n"); Address=AskForBaseAddress(0x350); clrscr(); printf( "\n\n\n Board Configuration: \n\n" " -- Base address is %X hex \n" " -- On board clock should be set to 1 MHz (required) \n" " -- Input should be applied to pin 13, counter 5 input (required) \n" " -- All remaining jumper settings should not be altered. \n" " \n\n\n" "\nPress any key to Continue\n", Address); /* print base address at %X */ getch(); status = setup(Address); /* set up program and the driver */ if (status != 0) return; /* is status > 0 then board error */ while (!kbhit()) { status = get_readings(); /* read the frequency */ if (status != 0) return; /* if status > 0 then error */ } getch(); /* clear the exit char */ clrscr(); } /* end main program */