📄 uart.c
字号:
////////////////////////////////////////////////////////////////////////////////
// Copyright(C) Gniy.Liu RHOSON, Crop. 2007-2008
// Filename: uart.c
// Description: None
////////////////////////////////////////////////////////////////////////////////
#define _UART_C
#include "proj_inc.h"
#if (UART_ENABLE)
// This flag is set on USART Receiver buffer overflow
BOOL rx_buf_overflow;
void UartInit(void)
{
U08 i;
g_bUartRxOk = FALSE;
for(i=0;i<RX_BUF_SIZE;i++) rx_buf[i] = 0;
rx_wr_index=rx_cnter=0;
for(i=0;i<TX_BUF_SIZE;i++) tx_buf[i] = 0;
tx_wr_index=tx_rd_index=tx_cnter=0;
g_RxTmr = 0;
g_TxTmr = 0;
#if (_MCUTYPE_==_MCU_STC89C52_) || (_MCUTYPE_==_MCU_W79E632_)
SCON = 0x50; //8 Data, 1 Stop, No Parity
PCON |= 0x80; //double
RCAP2H = RELOAD_CNT_HIGH;
RCAP2L = RELOAD_CNT_LOW;
TH2 = RELOAD_CNT_HIGH;
TL2 = RELOAD_CNT_LOW;
// T2CON TF2 EXF2 RCLK TCLK EXEN2 TR2 C/T2 CP/RL2
T2CON = 0x34;
ES = 1;
#elif (_MCUTYPE_==_MCU_M8_)
UCSRA=0x00;
UCSRB=0xD8;
UCSRC=0x86;
UBRRH = HIBYTE(UBRR_CONST);
UBRRL = LOBYTE(UBRR_CONST);
#elif ( (_MCUTYPE_==_MCU_M64_) || (_MCUTYPE_==_MCU_M1281_) )
UCSR0A=0x00;
UCSR0B=0xD8;
UCSR0C=0x86;
UBRR0H = HIBYTE(UBRR0_CONST);
UBRR0L = LOBYTE(UBRR0_CONST);
#endif
}
U08 PutChar(U08 c)
{
g_TxTmr=0;
#if (_MCUTYPE_==_MCU_STC89C52_) || (_MCUTYPE_==_MCU_W79E632_)
ES = 0;
#endif
#if (_MCUTYPE_==_MCU_STC89C52_) || (_MCUTYPE_==_MCU_W79E632_)
if(tx_cnter)
#elif (_MCUTYPE_==_MCU_M8_)
if(tx_cnter || ((UCSRA & DATA_REGISTER_EMPTY)==0))
#elif ( (_MCUTYPE_>=_MCU_M64_) || (_MCUTYPE_==_MCU_M1281_) )
if(tx_cnter || ((UCSR0A & DATA_REGISTER_EMPTY)==0))
#endif
{
tx_buf[tx_wr_index]=c;
if(++tx_wr_index == TX_BUF_SIZE) tx_wr_index=0;
#if ((_MCUTYPE_>=_MCU_M8_)||(_MCUTYPE_<=_MCU_M1281_))
++tx_cnter;
#endif
}
else
{
#if (_MCUTYPE_==_MCU_STC89C52_) || (_MCUTYPE_==_MCU_W79E632_)
SBUF = c;
#elif (_MCUTYPE_==_MCU_M8_)
UDR=c;
#elif ( (_MCUTYPE_==_MCU_M64_) || (_MCUTYPE_==_MCU_M1281_) )
UDR0=c;
#endif
}
#if (_MCUTYPE_==_MCU_STC89C52_) || (_MCUTYPE_==_MCU_W79E632_)
++tx_cnter;
#endif
#if (_MCUTYPE_==_MCU_STC89C52_) || (_MCUTYPE_==_MCU_W79E632_)
ES = 1;
#endif
return c;
}
void PutBuf(U08 cnt)
{
U08 i;
for(i=0; i<cnt; i++)
{
PutChar(g_TxBuf[i]);
}
}
void PutStr(char flash *str)
{
while(*str)
{
PutChar(*str);
++str;
}
}
#if (_MCUTYPE_==_MCU_STC89C52_) || (_MCUTYPE_==_MCU_W79E632_)
void UartInptIsr(void) interrupt 4 using 2
{
if(TI)
{
TI=0;
if(tx_cnter>1)
{
--tx_cnter;
SBUF = tx_buf[tx_rd_index];
if (++tx_rd_index == TX_BUF_SIZE) tx_rd_index=0;
}
else tx_cnter = 0;
g_TxTmr = 0;
}
if(RI)
{
RI=0;
rx_buf[rx_wr_index] = SBUF;
if(++rx_wr_index == RX_BUF_SIZE) rx_wr_index = 0;
if(++rx_cnter == RX_BUF_SIZE)
{
rx_cnter = 0;
rx_buf_overflow = 1;
}
g_RxTmr = C_RX_TIMEOUT;
}
}
#endif
#if ((_MCUTYPE_>=_MCU_M8_)&&(_MCUTYPE_<=_MCU_M1281_))
#if (_MCUTYPE_==_MCU_M8_)
#pragma vector=USART_RXC_vect
#elif (_MCUTYPE_ == _MCU_M64_)
#pragma vector=USART0_RXC_vect
#elif (_MCUTYPE_==_MCU_M1281_)
#pragma vector=USART0_RX_vect
#endif
__interrupt void usart0_rx_isr(void)
{
char status,data;
#if (_MCUTYPE_==_MCU_M8_)
status=UCSRA;
data=UDR;
#elif ( (_MCUTYPE_==_MCU_M64_) || (_MCUTYPE_==_MCU_M1281_) )
status=UCSR0A;
data=UDR0;
#endif
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buf[rx_wr_index]=data;
if (++rx_wr_index == RX_BUF_SIZE) rx_wr_index=0;
if (++rx_cnter == RX_BUF_SIZE)
{
rx_cnter=0;
rx_buf_overflow=1;
}
}
g_RxTmr = C_RX_TIMEOUT;
}
#if (_MCUTYPE_==_MCU_M8_)
#pragma vector=USART_TXC_vect
#elif (_MCUTYPE_ == _MCU_M64_)
#pragma vector=USART0_TXC_vect
#elif (_MCUTYPE_==_MCU_M1281_)
#pragma vector=USART0_TX_vect
#endif
__interrupt void usart0_tx_isr(void)
{
if (tx_cnter)
{
--tx_cnter;
#if (_MCUTYPE_==_MCU_M8_)
UDR=tx_buf[tx_rd_index];
#elif ( (_MCUTYPE_==_MCU_M64_) || (_MCUTYPE_==_MCU_M1281_) )
UDR0=tx_buf[tx_rd_index];
#endif
if (++tx_rd_index == TX_BUF_SIZE) tx_rd_index=0;
g_TxTmr = 0;
}
}
#endif
#endif // #if (UART_ENABLE)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -