/*************************************************************************** * 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 #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 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 initialize - Performs the setup and initialization of the * * 16450 chip. * **************************************************************************/ void initialize(Address) { /* 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); } /*************************************************************************** * 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 int Address; unsigned char ch,ch1,flag; clrscr(); printf(" Sample One : Internal loopback test.\n\n"); puts("This program uses the loop back feature of the 16450 chip to transmit "); puts("a sequence of characters and in turn receive them. No external jumpers"); puts("are required to run this program.\n "); clrscr(); printf("\n\n\nBoard Configuration:\n\n"); printf(" -- Base Address is %x hex\n",Address); puts(" -- All remaining jumper settings are irrelevant\n\n\n\n"); printf("\nPress any key to continue, or E to exit.\n"); if(toupper(getch())=='E') return; delay(0); /* initialize the Borland delay function */ initialize(Address); /* setup the 16450 chip */ 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 */ { clrscr(); printf("\n\n\nAll characters were transmitted without error. "); } }