/*---------------------------------------------------- --------------- * Sample program for the IDI-48 demonstrating register level access * of input data *------------------------------------------------------------------*/ #include #include #include #include #include unsigned int Address; 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 */ void intro(void) { clrscr(); printf(" SAMPLE 1.C : IDI-48\n\n"); printf("Demonstration program for accessing IDI-48 input data. Program\n"); printf("reads each of the IDI-48's input ports' 16 input channels and\n"); printf("displays on/off status of those channels.\n\n\n"); Address=AskForBaseAddress(0x350); clrscr(); puts("\n\n\nBoard Configuration:\n"); printf(" -- Base Address is %X hex\n",Address); puts(" -- If you are using the IDI-48 option C card, all mode switches"); puts(" should be OFF."); puts(" -- All remaining jumper settings are irrelevant.\n"); puts("\n\n\nPress any key to continue."); getch(); clrscr(); }/* end of intro */ void main(void) { unsigned char port, valid, input, data, mask; int i,c,done; unsigned char ofs[] = { 0,1,2,4,5,6 }; /* Table of offsets to */ /* input registers */ intro(); valid = 0; while (!valid) { clrscr(); printf("\n\nEnter port number to read (0,1,2): "); switch (port = getch() - '0') { case 0: case 1: case 2: valid = 1; } /* end switch */ } /* end while not valid */ done = 0; while (!done) { clrscr(); gotoxy(32,1); printf(" Reading port %u\n\n", port); gotoxy(32,3); printf("Input Status\n"); gotoxy(32,4); printf("----------------\n"); /* Read the first 8 input channels of current port */ data = inportb(Address + ofs[2*port]); /* Check the status of each bit field and display result */ mask = 1; for (input = 0; input < 8; input++) { gotoxy(32,5+input); if (mask & data) printf("%3u%12s\n", input, " ON"); else printf("%3u%12s\n", input, "OFF"); mask <<= 1; } /* end of for input */ /* Read second 8 input channels for current port */ data = inportb(Address + ofs[(2*port)+1]); /* Check the status of each bit field and display result */ mask = 1; for (input = 0; input < 8; input++) { gotoxy(32,13+input); if (mask & data) printf("%3u%12s\n", input+8, " ON"); else printf("%3u%12s\n", input+8, "OFF"); mask <<= 1; } /* end of for input */ /* Check if done */ printf("\n\n"); gotoxy(10,24); printf("Press 'Q' to quit; press any other key to re-read the port\n"); c = getch(); switch(toupper(c)) { case 'Q': done = 1; break; default: break; } /* end switch c */ } /* end of while not done */ } /* end of main */