/***************************************************************************** * This is a demonstration program to be used with the CTR-5 Counter * * timer board. The program will set up all five counters to count down * * from a different level, then use interrupts to read the count at a given * * interval. The results are then displayed on the screen. Note that this * * program uses segments, which should not be used by anything else. For * * instance, this program as is, cannot be run in the Turbo C desktop, or * * the program will lock up the system. * *****************************************************************************/ #include #include #include #include #include #include "ctr5drvc.h" /* These variable 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 buf[100],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. * *****************************************************************************/ 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: Sets up the driver and counter board. * *****************************************************************************/ unsigned setup(Address) { unsigned I,status; /* initialize the board */ params[0] = Address; /* starting board address */ params[1] = 1; /* FOUT divider ratio = 1 */ params[2] = 15; /* FOUT source = F5 */ params[3] = 0; /* counter 2 compare disabled */ params[4] = 0; /* counter 1 compare disabled */ params[5] = 0; /* disable time of day */ task = 0; status = call_driver(); /* call driver */ if (status > 0) return(status); /* if status > 0 then error */ for (I = 1;I <= 5;I++) /* set up each counter */ { params[0] = I; /* counter number */ params[1] = 0; /* no gating required */ params[2] = 0; /* positive edge counting */ params[3] = 11; /* count source = F1 */ params[4] = 0; /* disable special gate */ params[5] = 0; /* reload from load */ params[6] = 1; /* count repetitively */ params[7] = 0; /* count binary */ params[8] = 0; /* count down */ params[9] = 2; /* terminal count toggle */ task = 1; status = call_driver(); /* call driver */ if (status > 0) return(status); /* if status > 0 then error */ task = 3; /* load counter */ params[0] = I; /* counter number */ params[1] = 1000 * I + 1000; /* number of counts to make */ status = call_driver(); /* call the driver */ if (status > 0) return(status); } task = 2; /* load all counters */ params[0] = 2; params[1] = 1; params[2] = 1; params[3] = 1; params[4] = 1; params[5] = 1; status = call_driver(); /* call the driver */ task = 2; /* arm the counters */ params[0] = 1; params[1] = 1; params[2] = 1; params[3] = 1; params[4] = 1; params[5] = 1; status = call_driver(); /* call the driver */ return(status); } /* end setup */ /***************************************************************************** * FUNCTION: get_readings -- local routine * * PURPOSE: Reads the data and displays it on the screen. * *****************************************************************************/ unsigned get_readings() { unsigned status,*temp,index,offpntbuf,i; task = 7; /* install interrupts */ params[0] = 10; /* number of interrupts */ params[1] = 0x7000; /* these are the segments to */ params[2] = 0x7100; /* store the readings from */ params[3] = 0x7200; /* each counter */ params[4] = 0x7300; params[5] = 0x7400; params[6] = 0; /* start on IP0 disabled */ params[7] = 5; /* IRQ5 used for interrupts */ status = call_driver(); /* call the driver */ if (status > 0) return(status); /* wait for the interrupts to complete */ task = 8; do { params[0] = 0; params[1] = 0; status = call_driver(); /* call the driver */ } while (params[0] > 0); /* transfer the imformation in the segments for each counter into */ /* the data array. */ params[2] = 0x6f00; for (i = 1;i <= 5;i++) /* for each counter */ { index = (i - 1) * 20; temp = &buf[index]; offpntbuf = FP_OFF(temp); task = 9; params[0] = 10; params[1] = 0; params[2] += 0x100; params[3] = offpntbuf; status = call_driver(); /* call the driver */ if (status > 0) return(status); } /* display the counter values */ clrscr(); gotoxy(10,5); printf("PASS CTR 1 CTR 2 CTR 3 CTR 4 CTR 5"); gotoxy(10,6); printf("---- ----- ----- ----- ----- -----"); for (i = 0;i < 10;i++) { gotoxy(10,7+i); printf("%3d%10d%10d%10d%10d%10d",i,buf[i],buf[i+20],buf[i+40],buf[i+60],buf[i+80]); } return(0); } /* end get_readings */ /***************************************************************************** * FUNCTION: main -- local routine * *****************************************************************************/ void main() { unsigned status; char ch; unsigned int Address; clrscr(); printf( " SAMPLE1.C : INTERRUPT OPERATION \n" " \n" " This is a demonstration program to be used with the CTR-5 Counter\n" " timer board. The program will set up all five counters to count down \n" " from a different level, then use interrupts to read the count at a given\n" " interval. The results are then displayed on the screen. Note that this\n" " program uses segments, which should not be used by anything else. For \n" " instance, this program as is, cannot be run in the Turbo C desktop, or \n" " the program will lock up the system. \n\n"); Address=AskForBaseAddress(0x350); clrscr(); printf( "\n\n\n Board Configuration: \n\n" " -- Base address is %X hex \n" " -- The IRQ5 jumper should be installed (required) \n" " -- On board clock should be set to 1 MHz (required) \n" " -- An external jumper from pin 1 to pin 30 (required) \n" " -- An external jumper from pin 11 to pin 2 (required) \n" " -- All remaining jumper settings should not be altered. \n" "\n\n\n\nPress any key to continue\n", Address); getch(); status = setup(Address); /* set up program and the driver */ if (status != 0) return; /* is status > 0 then board error */ do { /* display current values for the channels */ status = get_readings(); if (status != 0) return; /* if status > 0 then error */ /* check for program exit */ printf("\n\n Press E to exit the program. Press any other key to continue..."); while (!kbhit); /* wait for key press */ ch = getch(); /* read the char */ } while (ch != 'E' && ch != 'e'); clrscr(); } /* end main program */