/***************************************************************************** * 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. * *****************************************************************************/ #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. * *****************************************************************************/ void Signon(void) { #define L1 " I O D C A R D S A M P L E P R O G R A M " #define L3 " Connect a loopback cable from PORT A to PORT B of PPI0." #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(19,1); printf("%s",L1); gotoxy(14,4); printf("%s", L3); gotoxy(25,10); printf("%s", L7); getch (); gotoxy(17,6); printf("%s",L4); gotoxy(17,7); printf("%s",L5); gotoxy(25,10); printf("%s",L6); } /* 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 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 - Allows the user to input the base 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 */ /***************************************************************************** * FUNCTION: main -- external routine * *****************************************************************************/ void main() { unsigned char Current[3]; boolean shift_left; clrscr(); printf(" SAMPLE 1.C : IOD24E\n"); printf("This demonstrates how to read and write to a port, and how to use the\n"); printf("read back function of the 8255 chip.\n"); gotoxy(1,5); PortA = AskForBaseAddress(0x300); PortB = PortA + 1; PORTC = PortA + 2; PortControl = PortA + 3; clrscr(); puts("Board Configuration\n"); printf(" -- Base Address is %X in hex.\n",PortA); printf(" -- All other jumper settings are irrelevant.\n\n\n"); printf("Press a key to continue..."); getch(); Signon(); /* print the start up message */ outportb(PortControl,0x8b); /* send the control byte */ Current[0] = 0; shift_left = TRUE; while(!kbhit()) /* loop until key pressed */ { outportb(PortA,Current[0]); /* write value to port a */ delay(10); 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 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; }; clrscr(); } /* end main program */