#include <conio.h>
#include <stdio.h>
#include <dos.h>
/* Type Definitions */
typedef unsigned char UBYTE; /* Old habbit... */
/* Serial Port Definitions */
#define STATUS 0x308 /* Status Base Address */
#define CONTROL 0x300 /* Control Base Address */
#define TXREG 0x30C /* Transmit Base Address */
#define RXREG 0x304 /* Receive Base Address */
#define TRUE 1
/* Function Prototypes */
void InitUSART(void); /* Initialize USART */
void TxData(UBYTE); /* Transmit Data */
UBYTE RxData(void); /* Receive Data */
/*
InitUSART() - Initialize USART to 153600, 8 Data Bits, No Parity, 1
Stop
Bit
*/
void InitUSART(void)
{
outp(CONTROL, 0x00);
outp(CONTROL, 0x00);
outp(CONTROL, 0x00);
outp(CONTROL, 0x40); // Reset UART
outp(CONTROL, 0x4E); // Stop, no parity, 8-bit, %16 baud
outp(CONTROL, 0x05); // UART now ready
}
/*
TxData() - Send Data to Serial Port
Entry:
data = Data to transmit
*/
void TxData(UBYTE data)
{
UBYTE x;
/* Check for Tx Buffer Empty */
do
{
x = inp(STATUS);
x &= 0x01;
} while(x == 0);
outp(TXREG, data); /* Send Data */
}
/*
RxData() - Receive Data from the Serial Port
Exit:
data = Rx Data byte
*/
UBYTE RxData(void)
{
UBYTE x;
UBYTE data = 0;
while(TRUE) /* Check for Rx Data */
{
x = inp(STATUS);
x &= 0x02;
if(x == 0x02)
{
data = inp(RXREG); /* Get Data */
break;
}
/* Optional. Aborts if keypress */
if(kbhit()) /* Abort if Keypress */
{
getch();
printf("\n");
break;
}
}
return(data);
}
void main(void)
{
UBYTE DSR;
InitUSART();
while(TRUE)
putch(RxData());
}
Show replies by date