RS232接口规范及编程资料(下)
serint.c
/* CONSOLE.C -- serial I/O code */
/*---------------------------------------------------------------------------*/
/* Initialize serial port hardware and variables */
#include
#include "serint.h"
void SerInitialize() {
SerFlags = 0;
FlagTransIdle = 1;
FlagCvtInCR = 1; /* want to turn CRs into LFs */
RR_iHead = RR_iTail = RR_cLev = RR_cMax = 0;
TR_iHead = TR_iTail = TR_cLev = TR_cMax = 0;
UnGotCh = -1;
/*--- set up Timer 1 to produce serial rate */
TCON &= 0x3F; /* clear run & interrupt flags */
TMOD &= 0x0F; /* flush existing Timer 1 setup */
TMOD |= 0x20; /* flush existing Timer 1 setup */
SCON = 0x50; /* flush existing Timer 1 setup */
PCON |= 0x00;
TH1 = TL1 = T1RELOAD & 0x00FF; /* flush existing Timer 1 setup */
TR1 = 1; /* start the timer */
ES = 1; /* enable serial interrupts */
}
/*---------------------------------------------------------------------------*/
/* Serial console interrupt handler */
/* If transmitter output is disabled, we fake trans interrupts until empty */
void SerInt() interrupt 4
{
if(RI) { /* receiver interrupt active? */
if(RR_cLev
RR_iHead++; /* tick the index */
RR_cLev++; /* tick size counter */
if(RR_iHead==INRINGSIZE) RR_iHead = 0; /* hit end of array yet? */
}
RI = 0; /* indicate we have it */
}
if(TI) { /* transmitter interrupt active? */
if(TR_cLev) { /* anything to send? */
SBUF = TRing[TR_iTail]; /* fetch next char and send it */
TR_cLev--; /* tick size counter */
TR_iTail++; /* tick the index */
if(TR_iTail==OUTRINGSIZE) TR_iTail = 0; /* hit end of array yet? */
} else FlagTransIdle = 1;/* no, flag inactive */
TI = 0; /* indicate done with it */
}
}
/*---------------------------------------------------------------------------*/
/* Send character to console */
/* Can strip LFs, in which case you get CR instead of LF/CR */
int putch(int TransChar)
{
putc(TransChar); /* if not LF, handle normally */
if(TransChar==\n) putc(\r); /* if LF, send a CR */
}
int putc(int TransChar)
{
while(TR_cLev>=OUTRINGSIZE); /* wait for space in ring */
ES = 0;
TRing[TR_iHead] = TransChar; /* point to char slot */
TR_iHead++; /* tick counter & index */
TR_cLev++;
if(TR_iHead==OUTRINGSIZE) TR_iHead = 0;
if(FlagTransIdle) {
FlagTransIdle = 0; /* kickstart transmitter if idle */
TI = 1;
}
ES = 1;
return(TransChar);
}
/*---------------------------------------------------------------------------*/
/* Decide if there are any pending chars */
/* Returns nonzero if theres a char */
int chkch() {
return(RR_cLev); /* tack on current level */
}
/*---------------------------------------------------------------------------*/
/* Wait for the serial transmitter to go idle */
/* If the transmitter is disabled, thats considered to be the same thing */
void SerWaitOutDone() {
while (TR_cLev); /* wait for ring empty */
while(!FlagTransIdle); /* wait for last char */
}
/*---------------------------------------------------------------------------*/
/* Flush the input buffer */
/* Returns number of chars flushed */
int SerFlushIn() {
ES = 0; /* turn off serial interrupts */
RR_iTail = 0; /* reset ring variables */
RR_iHead = 0;
RR_cLev = 0;
ES = 1; /* turn on serial interrupts */
}
/*---------------------------------------------------------------------------*/
/* Get character from console */
/* CRs turn into LFs unless were not doing that... */
int getch() {
int RetVal;
ES = 0; /* avoid interruptions */
if(RR_cLev) { /* anything pending? */
RetVal = RRing[RR_iTail];
if(RetVal==\r) RetVal = \n; /* use LF instead of CR */
RR_iTail++; /* tick size index & counter */
RR_cLev--;
if(RR_iTail==INRINGSIZE) RR_iTail = 0; /* hit end of array yet? */
} else RetVal = -1;
ES = 1;
return(RetVal);
}
/*---------------------------------------------------------------------------*/
/* Send string to console */
/* putstr(char *pString); */
/* The ?putstr entry point has *pString in DPTR */
int putstr (char *pstring)
{
while(*pstring) { /* fetch character if zero done */
putch(*pstring);
pstring++; /* continue... */
}
}
RS232接口规范编程资 相关文章:
- RS232接口规范及编程资料(上)(12-12)
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)