基于中断的、使用2个环形缓存区的串口底层驱动
#define ComBufLen 78 //COM buffer lengthunsigned char ComSendBuf,*ComSendHead,*ComSendRear,TXnum=0;
unsigned char ComRecvBuf,*ComRecvHead,*ComRecvRear,RXnum=0;
void interrupt ISR(void) {
//COM receive
if (RCIF && RCIE) {
if (RXnum<COMBUFLEN) {
*(ComRecvRear++)=RCREG;
if (ComRecvRear==0 || ComRecvRear>=ComRecvBuf+ComBufLen) ComRecvRear=ComRecvBuf;
RXnum++;
RCIF=0;
}
}
//COM transmit
if (TXIF && TXnum>0) {
TXREG=*(ComSendHead++);
if (ComSendHead==0 || ComSendHead>=ComSendBuf+ComBufLen) ComSendHead=ComSendBuf;
TXnum--;
}
if (TXnum>0) TXIE=1; else TXIE=0;
INTCON=0xC0;
return;
}
void ComReset(void) {
di();
TXSTA=???
RCSTA=??? //Set USART parameters
SPBRG=???
TXnum=0;
RXnum=0;
ComSendHead=ComSendBuf;
ComSendRear=ComSendBuf;
ComRecvHead=ComRecvBuf;
ComRecvRear=ComRecvBuf;
TXIE=1; //Enable TX interrupt
RCIE=1; //Enable RX interrupt
ei();
return;
}
//Send a char to COM, note that no overflow is checked here
void ComPutChar(unsigned char data) {
di();
*(ComSendRear++)=data;
if (ComSendRear==0 || ComSendRear>=ComSendBuf+ComBufLen) ComSendRear=ComSendBuf;//pointer loop
TXnum++;
ei();
return;
}
//Get a char from COM, empty not be checked here
unsigned char ComGetChar() {
unsigned char ch;
di();
ch=*(ComRecvHead++);
if (ComRecvHead==0 || ComRecvHead>=ComRecvBuf+ComBufLen) ComRecvHead=ComRecvBuf;
RXnum--;
ei();
return ch;
}
页:
[1]