//------------------------------------------------------------- //串口驱动程序.8bit //------------------------------------------------------------- #include "....SystemCom.h" #include "....SystemQueue.h" #include "....SystemCommunicateQueue.h" #include "....Mega.h"
#define TX8_ON() do{ UCSR0B |= (1<<UDRIE0);}while(0) #define TX8_OFF() do{ UCSR0B &= ~(1<<UDRIE0);}while(0)
struct CommunicateQueue *psUart80;
//------------------------------------------------------------- //队列压入回调函数 //------------------------------------------------------------- void InUart80_QueueCallBack( void ) { TX8_ON(); }
//------------------------------------------------------------- //设置串口 //------------------------------------------------------------- void Uart80_Setup(U16 mBaud, U8 mMode, U16 mSendBufSize, U16 mReciBufSize ) { //设置寄存器 UBRR0H = (U8)(mBaud>>8); UBRR0L = (U8)mBaud;
UCSR0C = mMode | (0<<UCSZ02)|(1<<UCSZ01)|(1<<UCSZ00); //8BIT UCSR0A = (1<<U2X0); //倍速
//申请队列 psUart80 = CommunicateQueue_Register( mSendBufSize, mReciBufSize, InUart80_QueueCallBack ); if( mSendBufSize ){ //允许发送 UCSR0B |= (1<<TXEN0); }
if( mReciBufSize ){ //允许接收及中断 UCSR0B |= (1<<RXCIE0) | (1<<RXEN0); } }
//------------------------------------------------------------- //串口接收中断。 //------------------------------------------------------------- SIGNAL( SIG_UART0_RECV ) { Queue_Push( psUart80->psReci, UDR0 ); }
//------------------------------------------------------------- //串口发送中断,使用寄存器保持空中断。 //------------------------------------------------------------- SIGNAL( SIG_UART0_DATA ) { if( Queue_Num( psUart80->psSend ) ) { UDR0 = Queue_Pop( psUart80->psSend ); } else { TX8_OFF(); //无发送数据,关闭 } }
|