#include #include #include #define ADDRESS (0x350) /* The Base address of the watchdog board. */ typedef unsigned int uint; void CtrMode(uint addr, char cntr, char mode) { int ctrl; ctrl = (cntr << 6) | 0x30 | (mode << 1); outportb(addr+3,ctrl); } void CtrLoad(uint addr ,int c,int val) { outportb(addr+c,val & 0x00FF); outportb(addr+c,(val>>8) & 0x00FF); } /* ****************************************************************************** FUNCTION set_counter() Purpose: This function performs the setup operations on the 82C54 counter/ timer chip. ****************************************************************************** */ void set_counter(void) { CtrMode(ADDRESS, 0, 3); // set counter 0 to mode 3 CtrLoad(ADDRESS, 0, 0xFFFF); // load counter 0 with full scale CtrMode(ADDRESS, 1, 2); // set counter 1 to mode 2 CtrLoad(ADDRESS, 1, 0x0010); // load counter 1 with 10 hex CtrMode(ADDRESS, 2, 1); // set counter 2 to mode 1 } /* End set_counter() */ /* ****************************************************************************** FUNCTION main() Purpose: This function controls program flow, making calls to the other functions, and prompting for user input. ****************************************************************************** */ void main() { unsigned read_back, out_byte; int aborted = 0; clrscr(); puts("C Sample #1: Use Of Counter/Timer Chip with Watchdog"); puts("This program demonstrates how to program the 82C54 counter/timer"); puts("chip on the Watchdog Timer board. The Watchdog address should be set"); puts("to 350 hex. A keystroke will stop the program."); puts(""); puts("Press any key to start."); getch(); /* Grab keystroke. Discard it. */ inp(ADDRESS + 7); /* Disable counters */ set_counter(); /* Set counter and start the countdown. */ outp(ADDRESS+7, 0); /* Enable counters */ for (read_back = 0; read_back <=1000; read_back++) { CtrMode(ADDRESS, 1, 2); // set counter 1 to mode 2 CtrLoad(ADDRESS, 1, 0x0010); // load counter 1 with 10 hex printf("Updating counter 1. Loop number : %hu", read_back); gotoxy(1, 7); } puts("\nWaiting for timeout . . ."); do { aborted = kbhit(); } while ((inp(ADDRESS+4) & 1 != 0) && !(aborted)); /* Check bit 0 of control register */ puts(""); if (aborted) puts("Timeout aborted!"); else puts("Watchdog timed out successfully."); }