英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流
标题:
基于中断的、使用2个环形缓存区的串口底层驱动
[打印本页]
作者:
winnie
时间:
2009-3-16 15:02
标题:
基于中断的、使用2个环形缓存区的串口底层驱动
#define ComBufLen 78 //COM buffer length
unsigned char ComSendBuf[ComBufLen],*ComSendHead,*ComSendRear,TXnum=0;
unsigned char ComRecvBuf[ComBufLen],*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;
}
欢迎光临 英锐恩单片机论坛,Microchip单片机,模拟器件,接口电路,麦肯单片机,单片机应用交流 (http://enroobbs.com/)
Powered by Discuz! X3.2