// // Serial.c // // Source based on code snippet from PICLIST. // #include "conio.h" #include "viewer.h" /* Serial Port Definitions */ #define COM1_BASE 0x3F8 /* COM1 Base Address */ #define COM2_BASE 0x2F8 /* COM2 Base Address */ #define COM3_BASE 0x3E8 /* COM3 Base Address */ #define COM4_BASE 0x2E8 /* COM4 Base Address */ static int COM_BASE; /* InitUSART() - Initialize USART to 9600, 8 Data Bits, No Parity, 1 Stop Bit */ void InitUSART( int Port ) { switch( Port ) { case 1: COM_BASE = COM1_BASE; break; case 2: COM_BASE = COM2_BASE; break; case 3: COM_BASE = COM3_BASE; break; case 4: COM_BASE = COM4_BASE; break; default: COM_BASE = COM1_BASE; break; } outp(COM_BASE + 1, 0x00); /* Turn off interrupts */ outp(COM_BASE + 3, 0x83); /* Access Divisor Latch */ outp(COM_BASE + 0, 0x0C); /* Set 115200 Baud (115200/Divisor) */ /* Divisor = 115200/Baud rate For 9600 Baud: Divisor = 12 (0x0C) */ outp(COM_BASE + 3, 0x03); /* Access DFM reg. Configure Data Format */ outp(COM_BASE + 4, 0x00); /* Disable Loop, !OUTx. Disable !RTS, !DTR */ /* 0x01 = Disable Loop, !OUTx, !RTS. Enable !DTR 0x02 = Disable Loop, !OUTx, !DTR. Enable !RTS 0x03 = Disable Loop, !OUTx. Enable !RTS, !DTR */ } /* TxData() - Send Data to Serial Port Entry: data = Data to transmit */ void TxData( int data ) { int x; /* Check for Tx Buffer Empty */ do { x = inp(COM_BASE + 5); x &= 0x20; } while(x == 0); outp(COM_BASE + 0, data); /* Send Data */ } /* RxFlush() - Receive any data available from the Serial Port, ignore it Exit: data = Rx Data byte - last byte received */ int RxFlush( void ) { int x,data; do { x = inp(COM_BASE + 5); x &= 0x01; if (x == 1) { data = inp(COM_BASE + 0); /* Get Data */ } } while ( x == 1 ); return( data ); } /* RxData() - Receive Data from the Serial Port Exit: data = Rx Data byte */ int RxData(void) { int x; int data = 0; while( 1 ) /* Check for Rx Data */ { x = inp(COM_BASE + 5); x &= 0x01; if(x == 1) { data = inp(COM_BASE + 0); /* Get Data */ break; } if(kbhit()) /* Abort if Keypress */ { data = -1; break; } } return(data); }