/***************************************************************************** * MODULE: SAMPLE1.C * * This program demonstrates how to activate the relays when multiple ROB-8As* * are used with a digital I/O board. Which ROB-8A to use is selected by * * setting bit C2 high along with a bit pattern for card number in bit C0 and* * C1. C2 must be set low again to read the relays through port B. * * If you're using only one ROB-8A, writing to port C is not necessary and * * all address switches on the ROB-8A can be set low. * * LAST MODIFICATION: 12/7/99 * * * *****************************************************************************/ #include #include #include #include #include #include /* these are the port address offsets for reading and control of the IOD */ unsigned int PortA; unsigned int PortB; unsigned int PortC; unsigned int PortControl; /* this typedef allow use to duplicate the boolean type as used in pascal */ typedef enum {FALSE,TRUE} boolean; /***************************************************************************** * FUNCTION: signon -- local routine * * * * PURPOSE: To display an initial sign on message on the screen. * * * * INPUT: None. * * * * OUTPUT: None. * * * *****************************************************************************/ void signon(void) { #define L1 " R O B - 8 A C A R D S A M P L E P R O G R A M " #define L4 " PORT A OUTPUT PORT A INPUT PORT B INPUT " #define L5 " ------------- ------------ ------------ " #define L6 " PRESS ANY KEY TO EXIT PROGRAM " #define L7 " PRESS ANY KEY TO CONTINUE" int index; /* print the string display */ clrscr(); gotoxy(15,2); printf("%s",L1); gotoxy(27,10); printf("%s", L7); getch (); gotoxy(17,6); printf("%s",L4); gotoxy(17,7); printf("%s",L5); gotoxy(27,10); printf("%s",L6); } /* end signon */ /***************************************************************************** * FUNCTION: write_port_data -- local routine * * * * PURPOSE: Displays the current port values on the screen. * * * * INPUT: current: array with the port reading to display. * * * *****************************************************************************/ void write_port_data(unsigned char current[3]) { unsigned x,y,index1,index; unsigned char value; x = 20; /* x coordinate to write value to screen */ y = 8; for (index = 0;index <= 2;index++) /* for each array member */ { value = current[index]; for (index1 = 0;index1 <= 7;index1++) /* for each bit in array member */ { gotoxy(x,y); if (value % 2) putchar('1'); else putchar('0'); value = value >> 1; /* roll next diaplay bit */ x++; } x+= 10; } } /* end write_port_data */ /****************************************************************************** Function AskForBaseAddress Parameters: OldOne Returns: Returns base address of Com 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 -- external routine * * * * INPUT: None. * * * *****************************************************************************/ void main() { unsigned char current[3]; char robcard; unsigned int rob = 0; unsigned int DONE; boolean shift_left; DONE= FALSE; robcard='1'; clrscr(); puts(" SAMPLE 1.C : ROB-8A READ/WRITE "); puts(""); puts("This sample program will sequentially turn on all bits in port a"); puts("of a digital I/O board and then sequencially turn them off. Each"); puts("time a new bit is set, the relay is activated on the selected"); puts("ROB-8A and the input from both port a and port b are read and"); puts("the data displayed. Also demonstrated is how to select "); puts("which ROB-8A to activate when multiple cards are being used."); gotoxy(1,9); PortA = AskForBaseAddress(0x2A0); PortB = PortA + 1; PortC = PortA + 2; PortControl = PortA + 3; clrscr(); printf("\n\n\nDigital I/O Board Configuration:\n\n"); printf(" -- Base Address is %X in hex\n",PortA); printf(" -- All remaining jumper settings are irrelevant\n"); printf("\n\n\nROB-8A Board Configuration:\n\n"); printf(" -- On DIP swtich S1, set S5 ON\n"); printf(" -- Set a different Address switch ON for each card.\n"); printf("Press a key to continue\n"); getch(); signon(); /* print the start up message */ outportb(PortControl,0x8a); /* send the control byte */ current[0] = 0; shift_left = TRUE; puts(""); puts("Select which ROB-8A to activate"); puts("1. Card 1"); puts("2. Card 2"); puts("3. Card 3"); puts("4. Card 4"); while(!DONE){ if(kbhit()) robcard = getch(); switch(robcard){ //set bit C2 high to select ROB-8A card //bits C0 and C1 are a bit pattern for card number case '1': outportb(PortC,4);rob=0;break; case '2': outportb(PortC,5);rob=1;break; case '3': outportb(PortC,6);rob=2;break; case '4': outportb(PortC,7);rob=3;break; default: DONE = TRUE;break; } outportb(PortA,current[0]); // write value to port a delay(10); outportb(PortC,rob); //allow read from relays current[1] = inportb(PortA); //read back what we wrote to port A current[2] = inportb(PortB); //read the data on port B write_port_data(current); // write new data to screen delay(700); /* compute value to turn on/off next bit in line */ if (current[0] == 0) shift_left = TRUE; if (current[0] == 255) shift_left = FALSE; if (shift_left) current[0] = (current[0] << 1) + 1; else current[0] = (current[0] - 1) >> 1; } clrscr(); } /* end main program */