/*************************************************************************** * SAMPLE 1 * * LOOP BACK TEST PROGRAM * * * * This program uses the loop back feature of the 16450 chip to transmit * * a sequence of characters and in turn receive them. No external jumpers * * are required to run this program. * * * ***************************************************************************/ #include #include #include #include #include #include #define Receive 0 /* Receiver buffer register */ #define Transmit 0 /* Transmitter holding register */ #define L_Divisor 0 /* Divisor latch, least significant byte */ #define U_Divisor 1 /* Divisor latch, most significant byte */ #define Int_Enb 1 /* Interrupt enable register */ #define Int_Id 2 /* Interupt identification register */ #define Line_Ctrl 3 /* Line control register */ #define Modem_Ctrl 4 /* Modem control register */ #define Line_Stat 5 /* Line status register */ #define Modem_Stat 6 /* Modem status register */ unsigned int Address; /************************************************************************** * PROCEDURE initialize - Performs the setup and initialization of the * * 16450 chip. * **************************************************************************/ void Initialize(void) { /* First, set up the divisor latches for 9600 baud. A divisor of 12 is required since the 1.8432 clock is used. The upper divisor is set to 0 and the lower to 12. */ outportb(Address + Line_Ctrl,0x80); /* Set DLAB high for Divisor latch enable */ outportb(Address + L_Divisor,12); /* Load lower latch with 12 */ outportb(Address + U_Divisor,0); /* Load upper latch with 0 */ /* Now set the line control register for the following communications protocol bit0, bit1 = 1 -- character is eight bits bit2 = 0 -- one stop bit bit3 = 0 -- no parity is enabled bit4 = 0 -- doesn't matter with parity disabled bit5 = 0 -- doesn't matter with parity disabled bit6 = 0 -- disable break control bit7 = 0 -- DLAB bit, 0 disables divisor latches */ outportb(Address + Line_Ctrl,0x03); /* Enable the loop back feature of the chip with bit 4 of modem control register, now all transmitted characters will be looped back to the receiver, without external connections. */ outportb(Address + Modem_Ctrl,0x10); /* these inputs are to just insure that both the receiver buffer register and receiver shift register are clear. */ delay(50); inportb(Address + Receive); /* clear rx registers */ delay(50); inportb(Address + Receive); } /**************************************************************************** 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 */ /*************************************************************************** * * * PROCEDURE main - transmit the letters 'A' through 'Z', and in turn, * * receive the characters, and check if the correct char * * is received. The program will print an error message * * for each bad character received. If no errors occur, * * the program will indicate so at the end of program * * execution. * * * ***************************************************************************/ void main() { unsigned char Ch, Ch1, Flag; clrscr(); printf(" SAMPLE 1.C : WDGSIO Internal loopback test.\n\n"); printf("This program uses the internal loop back feature of the 16450 chip to transmit\n"); printf("to transmit a series of characters and in turn receive them. No external\n"); printf("jumpers are required.\n"); gotoxy(1,7); Address = AskForBaseAddress(0x2A8); clrscr(); printf("\n\n\nBoard Configuration:\n\n"); printf(" -- Base Address is %X hex\n",Address); printf(" -- All remaining jumper settings are irrelevant.\n\n\n"); printf("Press a key to continue"); getch(); Initialize(); /* setup the 16450 chip */ clrscr(); Flag = 0; for (Ch = 'A';Ch <= 'Z';Ch++) { outportb(Address + Transmit,Ch); do { Ch1 = inportb(Address + Line_Stat); /* send the char */ } while ((Ch1 & 1) == 0); /* check if bit 1 is a 1, if so, data is ready */ Ch1 = inportb(Address + Receive); /* read the input char */ /* check if char received is the one we sent */ if (Ch1 != Ch) { printf("error with character %c, received char %c.\n",Ch,Ch1); Flag = 1; } } if (Flag == 0) /* then no errors occured */ { printf("All characters were transmitted without error. "); } }