/***************************************************************************** * This sample program will sequentially turn on all bits of the relay input* * and sequentially turns them off. Each time it sets a new bit, both the * * relay output and the relay input are read and the data displayed. This * * demonstrates how to read and write to a port, and to use the read back * * function of the board. * *****************************************************************************/ #include #include #include "kbinp.h" void IOPermission() { if(iopl(3)<0) { fprintf(stderr, "NO IO PERMISSION\n"); } } unsigned int Address; /* this typedef allow use to duplicate the boolean type as used in pascal */ typedef enum {FALSE,TRUE} boolean; unsigned AskForBaseAddress(unsigned int OldOne) { char msg[3]; unsigned int newone; unsigned int temp = 0; int index = 0; int letter; puts("Please enter the Base Address for your card (in hex)"); printf("or press ENTER for %X.\n>", OldOne); while(temp==0) { newone=0; index=0; while((letter = getchar())!='\n') //Read char by char and store in Msg msg[index++] = letter; sscanf(msg, "%X", &newone); if(newone==0){ //Check to see if address was inputed temp=OldOne; //Assign default if not } else if((newone>=0x100)&&(newone<=0xffff)){ //Check to see if inputed addres is valid temp=newone; } else { //If Newone invalid ask for new value. printf("\nEnter the Base Address or press ENTER for %x > ", OldOne); for(index=0; index<=3; index++) msg[index]=0; } //Run while until temp = valid address } return(temp); } /* end of AskForBaseAddress */ /***************************************************************************** * FUNCTION: signon -- local routine * * PURPOSE: To display an initial sign on message on the screen. * *****************************************************************************/ void signon() { puts(" I I R O - 8 C A R D S A M P L E P R O G R A M\n\n\n" "This sample program will sequentially turn on all bits of the relay input\n" "and sequentially turns them off. Each time it sets a new bit, both the\n" "relay output and the relay input are read and the data displayed. This\n" "demonstrates how to read and write to a port, and to use the read back\n" "function of the board.\n"); Address=AskForBaseAddress(0x354); system("clear"); puts("\n\n\nBoard Configuration:\n"); printf(" -- Base Address is %X hex\n",Address); puts(" -- The IRQ5 jumper should be installed (required)\n"); puts(" -- All remaining jumper settings are irrelevant.\n\n\n"); puts("Press enter to continue\n"); while(getch()!='\n'); system("clear"); puts("\nRELAY OUTPUT RELAY READBACK OPTO INPUT\n" "------------- -------------- ----------\n"); puts(" Press enter to exit program \n"); } /* end signon */ /***************************************************************************** * FUNCTION: write_port_data -- local routine * * PURPOSE: Displays the current port values on the screen. * *****************************************************************************/ void write_port_data(unsigned char current[3]) { unsigned index1,index; unsigned char value; int i; i=1; for (index = 0;index <= 2;index++) /* for each array member */ { value = current[index]; if (i==1) { system("tput cup 2 2"); i++; } else if (i==2) { system("tput cup 2 20"); i++; } else if (i==3) { system("tput cup 2 38"); } for (index1 = 0;index1 <= 7;index1++) /* for each bit in array member */ { if (value % 2) putchar('1'); else putchar('0'); value = value >> 1; /* roll next diaplay bit */ } puts("\n"); } } /* end write_port_data */ /***************************************************************************** * FUNCTION: main -- external routine * *****************************************************************************/ int main() { unsigned char current[3]; boolean shift_left; signon(); /* print the start up message */ current[0] = 0; shift_left = TRUE; IOPermission(); do /* loop until key pressed */ { outb(current[0],Address); /* write value to relay output */ current[1] = inb(Address); /* read back what we wrote to relays */ current[2] = inb(Address+1); /* read the data on OPTO INPUTS */ write_port_data(current); /* write new data to screen */ system("sleep 1"); /* compute value to turn on/off nesxt 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; }while(kbhit()==0); return 0; } /* end main program */