/*************************************************************************** * * * 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 unsigned int Base; #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 */ /************************************************************************** * * * PROCEDURE initialize - Performs the setup and initialization of the * * 16450 chip. * * * * * **************************************************************************/ //add 10h to base 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(Base + LINE_CTRL,0x80); /* Set DLAB high for Divisor latch enable */ outportb(Base + L_DIVISOR,12); /* Load lower latch with 12 */ outportb(Base + 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(Base + 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(Base + MODEM_CTRL,0x10); /* these inputs are to just insure that both the receiver buffer register and receiver shift register are clear. */ delay(50); inportb(Base + RECEIVE); /* clear rx registers */ delay(50); inportb(Base + RECEIVE); } /* ****************************************************************************** Function AskForBaseAddress Purpose: This function allows the user to input the address of the card. ****************************************************************************** */ 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(); puts(" SAMPLE 1.C : LOOP BACK TEST PROGRAM\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."); gotoxy(1,7); Base = AskForBaseAddress(0x2A8); clrscr(); printf("\n\n\nBoard Configuration:\n\n"); printf(" -- Base Address is %X hex\n",Base); printf(" -- Disable jumpers should not be jumpered (required)\n"); printf(" -- Invert jumpers should not be jumpered (required)\n"); printf(" -- CTR0/CTR2 and CTR0/CTR1 should not be jumpered (required)\n"); printf(" -- All remaining jumper settings are irrelevant.\n\n\n"); printf("Press a key to continue."); getch(); clrscr(); delay(0); /* initialize the Borland delay function */ initialize(); /* setup the 16450 chip */ flag = 0; for (ch = 'A';ch <= 'Z';ch++) { outportb(Base + TRANSMIT,ch); do { ch1 = inportb(Base + LINE_STAT); /* send the char */ } while ((ch1 & 1) == 0); /* check if bit 1 is a 1, if so, data is ready */ ch1 = inportb(Base + 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. "); } }