微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > RS232接口规范及编程资料(下)

RS232接口规范及编程资料(下)

时间:12-12 来源:互联网 点击:

 

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_cLevRRing[RR_iHead] = SBUF; /* pick up the character and stick in ring */
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... */
}
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top