Information Technology Reference
In-Depth Information
baud rate, the number of data bits and the type of parity. An outline of the modi-
fied function is given in Program 13.5.
Program 13.5
#define COM1BASE 0x3F8
#define COM2BASE 0x2F8
#define COM1 0
#define COM2 1
enum baud_rates {BAUD110,BAUD300,BAUD600,BAUD1200,
BAUD2400,BAUD4800,BAUD9600};
enum parity {NO_PARITY,EVEN_PARITY,ODD_PARITY};
enum databits {DATABITS7,DATABITS8};
#include <conio.h>
#include <dos.h>
#include <stdio.h>
void setup_serial(int comport, int baudrate, int parity,
int databits);
void send_character(int ch);
int get_character(void);
int main(void)
{
int inchar,outchar;
setup_serial(COM1,BAUD2400,EVEN_PARITY,DATABITS7);
:::::::::::etc.
}
void setup_serial(int comport, int baudrate,
int parity, int databits)
{
int tdreg,lcr;
if (comport==COM1)
{
tdreg=COM1BASE; lcr=COM1BASE+3;
}
else
{
tdreg=COM2BASE; lcr=COM2BASE+3;
}
_outp( lcr, 0x80);
/* set up bit 7 to a 1 to set Register address bit
*/
switch(baudrate)
{
case BAUD110: _outp(tdreg,0x17);_outp(tdreg+1,0x04); break;
case BAUD300: _outp(tdreg,0x80);_outp(tdreg+1,0x01); break;
case BAUD600: _outp(tdreg,0x00);_outp(tdreg+1,0xC0); break;
case BAUD1200: _outp(tdreg,0x00);_outp(tdreg+1,0x40);break;
case BAUD2400: _outp(tdreg,0x00);_outp(tdreg+1,0x30);break;
case BAUD4800: _outp(tdreg,0x00);_outp(tdreg+1,0x18);break;
case BAUD9600: _outp(tdreg,0x00);_outp(tdreg+1,0x0C);break;
}
:::::::::: etc.
}
13.6.24
One problem with Programs 13.2 and 13.3 is that when the return key is pressed
only one character is sent. The received character will be a carriage return which
returns the cursor back to the start of a line and not to the next line. Modify the re-
ceiver program so that a line feed will be generated automatically when a carriage
return is received. Note a carriage return is an ASCII 13 and line feed is a 10.
Search WWH ::




Custom Search