/* ****************************************************************************** C Language Sample #3 : SAMPLE3.C This program will set up counter 2 to decrement from 101 hex, sending out a pulse when reaching zero. The value of the counter will be read and displayed until zero is reached. Because the counter is extremely fast, the program will rarely catch it at zero, so execution will end soon after that point is reached. ****************************************************************************** */ #include #include #include #include #include #include unsigned int Address; /* The Base address of the watchdog board. */ unsigned int Com_Adr; /* The Base Address of the communications segment of the board. */ /* ****************************************************************************** FUNCTION set_counter() Purpose: This function performs the setup operations on the 8253-5 counter/ timer chip. Counter two will be set for two byte load/read, mode zero, binary. ****************************************************************************** */ void set_counter(void) { int control_word; control_word = 0xB0; /* Set counter 2, Read/Load Least Sig Byte then Most Sig Byte, Mode 0, binary count */ outportb(Address+3,control_word); /* Write control word to the counter control register. */ outportb(Address+2,0x01); /* Write 101 hex to counter 2 register. */ delay(5); /* Provide a brief pause between writes. */ outportb(Address+2,0x01); /* Write 101 hex to counter 2 register. */ delay(25); } /* End set_counter() */ /* ****************************************************************************** FUNCTION read_counter() Purpose: This function reads the value of the counter. The value is read in two bytes, least significant byte first. The function will then combine the two and return that value. ****************************************************************************** */ unsigned read_counter(void) { unsigned read_back = 0, counter_value = 0; outportb(Address+3,0x80); /* Counter Latching: Allows for the reading of both bytes without halting the counter.*/ read_back = inportb(Address+2); /* Read Least Significant Byte */ counter_value = read_back; /* Move low byte into storage */ read_back = inportb(Address+2); /* Read Most Significant Byte */ counter_value += read_back << 8; /* Shift high byte and store */ return(counter_value); } /* End read_counter() */ /* ****************************************************************************** Function AskForBaseAddress Purpose: This function allows the user to input the address of the card. ****************************************************************************** */ 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 main() Purpose: This function controls program flow, making calls to the other functions, and prompting for user input. ****************************************************************************** */ void main() { int Dummy; unsigned counter_value = 0x101 ,read_back,last_value; clrscr(); puts(" SAMPLE 3.C : Use Of Counter/Timer Chip\n"); puts("This program demonstrates how to program the 8253-5 counter/timer"); puts("chip on the Watchdog Timer board. If the card has the optional relay,"); puts("the chip will send a pulse that will trip the relay. This should"); puts("cause two audible clicks, one for when the counter is enabled, and one"); puts("for the pulse. There will be a delay of about five minutes between"); printf("these occurences. A keystroke will stop the program.\n\n\n"); gotoxy(1,10); puts("COM ADDRESS"); Com_Adr = AskForBaseAddress(0x2A8); gotoxy(1,17); puts("WATCHDOG ADDRESS"); Address = AskForBaseAddress(0x350); clrscr(); printf("\n\n\nBoard Configuration:\n\n"); printf(" -- Base Address is %X hex.\n",Com_Adr); printf(" -- Watchdog Address is %X hex.\n",Address); printf(" -- All remaining jumper settings are irrelevant.\n\n\n"); printf("Press a key to continue"); getch(); clrscr(); outportb(Com_Adr+4, 4); /* The output to the relay is controlled by a bit in the UART. Setting this bit opens the line to the relay and causes the first click. Send new value to the UART. */ set_counter(); /* Set counter and start the countdown. */ do { last_value = counter_value; counter_value = read_counter(); gotoxy(1,1); printf("Value read from counter: %u\n",counter_value); } while (!kbhit() && (counter_value < last_value)); if (kbhit()) getch(); /* Strip keystroke from buffer. */ }