/***************************************************************************** * MODULE: SAMPLE1.C * * * * This sample program will sequentially turn on all bits in port a and then* * seqencially turn them off. Each time it sets a new bit, both port a and * * port b 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 8255 chip. * * If the port a pins are jumpered to the port b pins, then a board test * * program results, with port b being used to verify what has been written * * to port a. The program will use port 0 of cards with mulitple 8255's. * * * * LAST MODIFICATION: 2/5/98 * * * *****************************************************************************/ #include #include #include #include #include "kbinp.h" /* 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; void IOPermission() { if(iopl(3)<0) { fprintf(stderr, "NO IO PERMISSION\n"); } } /***************************************************************************** * FUNCTION: signon -- local routine * * * * PURPOSE: To display an initial sign on message on the screen. * * * * INPUT: None. * * * * OUTPUT: None. * * * *****************************************************************************/ void signon(void) { /* print the string display */ system("clear"); printf(" DIO CARD SAMPLE PROGRAM \n\n" " Connect a loopback cable from PORT A to PORT B of PPI0. \n" " PRESS ENTER TO CONTINUE \n"); while(getch()!='\n'); system("clear"); printf("\n\n PORT A OUTPUT PORT A INPUT PORT B INPUT \n"); printf(" ------------ ------------ ------------ \n"); system("tput cup 12 0"); printf(" PRESS ENTER TO EXIT PROGRAM \n"); } /* 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 index1,index; unsigned char value; int i; i=1; system("tput cup 0 0 "); /* x coordinate to write value to screen */ for (index = 0;index <= 2;index++) /* for each array member */ { value = current[index]; if (i==1) { system("tput cup 3 5"); i++; } else if (i==2) { system("tput cup 3 21"); i++; } else if (i==3) { system("tput cup 3 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 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[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: main -- external routine * * * * INPUT: None. * * * *****************************************************************************/ int main() { unsigned char current[3]; boolean shift_left; system("clear"); puts(" SAMPLE 1.C : DIO READ/WRITE \n\n"); puts("This sample program will sequentially turn on all bits in port a and then\n"); puts("sequencially turn them off. Each time it sets a new bit, both port a and \n"); puts("port b are read and the data displayed. This demonstrates how to read \n"); puts("and write to a port, and to use the read back function of the 8255 chip. \n"); PortA = AskForBaseAddress(0x2A0); IOPermission(); PortB = PortA + 1; PortC = PortA + 2; PortControl = PortA + 3; system("clear"); printf("\n\n\nBoard Configuration:\n\n"); printf(" -- Base Address is %X in hex\n",PortA); printf(" -- All remaining jumper settings are irrelevant\n\n\n\n\n"); printf("Press enter to continue\n"); while(getch()!='\n'); signon(); /* print the start up message */ outb(0x8b,PortControl); /* send the control byte */ current[0] = 0; shift_left = TRUE; do /* loop until key pressed */ { outb(current[0],PortA); /* write value to port a */ current[1] = inb(PortA); /* read back what we wrote to port A */ current[2] = inb(PortB); /* read the data on port B */ 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 */