我完全忘了发密码了。代码确实陈旧易碎,所以要尽量少批评。是的,对于接收器没有边界检查,但是因为头部和尾部是UTIT8YT型,缓冲器是256字节的,因此它永远不会溢出聪明的裤子(我不得不在工作中解释这无数次)。然而,在我使用它的时候,它运行得非常好。TDE的东西,BTW,在那里,因为这属于半双工RS485代码。
以上来自于百度翻译
以下为原文
I totally forgot to post code. The code is really old and crusty, so keep criticisms to a minimum. Yes, there is no bounds checking for the receiver but that is because the head and tail are of type uint8_t and the buffer is 256 bytes: therefore it can never overflow smarty-pants (I've had to explain this numerous times at work). However, it worked very well for the time that I used it. The TDE stuff, btw, is there because this pertains to half-duplex RS485 code.
void __ISR(_TIMER_2_VECTOR, IPL4SOFT) Timer2Handler(void)
{
uint8_t dummy_val;
#if MAX_NUMBER_UARTS >= 1
//RX1
while (U1STAbits.URXDA)
if (U1STAbits.FERR || U1STAbits.PERR)
dummy_val = U1RXREG; //dummy read
else if (U1STAbits.OERR)
U1STAbits.OERR = 0; //clear FIFO and receive buffer
else
rb[0].arr[rb[0].tail.val++] = U1RXREG; //grab a byte
//TX1
if (uart_is_tx_flag[0])
while (!U1STAbits.UTXBF)
{
if (rb[0].head.val != rb[0].tail.val)
U1TXREG = rb[0].arr[rb[0].head.val++];
else if (!U1STAbits.TRMT)
break;
else
{
if (!UART1_TDE_STATE())
uart_is_tx_flag[0] = false;
UART1_TDE_OFF();
UART1_LED_OFF();
break;
}
}
#endif
#if MAX_NUMBER_UARTS >= 2
//RX2
while (U2STAbits.URXDA)
if (U2STAbits.FERR || U2STAbits.PERR)
dummy_val = U2RXREG; //dummy read
else if (U2STAbits.OERR)
U2STAbits.OERR = 0; //clear FIFO and receive buffer
else
rb[1].arr[rb[1].tail.val++] = U2RXREG; //grab a byte
//TX2
if (uart_is_tx_flag[1])
while (!U2STAbits.UTXBF)
{
if (rb[1].head.val != rb[1].tail.val)
U2TXREG = rb[1].arr[rb[1].head.val++];
else if (!U2STAbits.TRMT)
break;
else
{
if (!UART2_TDE_STATE())
uart_is_tx_flag[1] = false;
UART2_TDE_OFF();
UART2_LED_OFF();
break;
}
}
#endif
#if MAX_NUMBER_UARTS >= 3
//RX3
while (U3STAbits.URXDA)
if (U3STAbits.FERR || U3STAbits.PERR)
dummy_val = U3RXREG; //dummy read
else if (U3STAbits.OERR)
U3STAbits.OERR = 0; //clear FIFO and receive buffer
else
rb[2].arr[rb[2].tail.val++] = U3RXREG; //grab a byte
//TX3
if (uart_is_tx_flag[2])
while (!U3STAbits.UTXBF)
{
if (rb[2].head.val != rb[2].tail.val)
U3TXREG = rb[2].arr[rb[2].head.val++];
else if (!U3STAbits.TRMT)
break;
else
{
if (!UART3_TDE_STATE())
uart_is_tx_flag[2] = false;
UART3_TDE_OFF();
UART3_LED_OFF();
break;
}
}
#endif
#if MAX_NUMBER_UARTS >= 4
//RX4
while (U4STAbits.URXDA)
if (U4STAbits.FERR || U4STAbits.PERR)
dummy_val = U4RXREG; //dummy read
else if (U4STAbits.OERR)
U4STAbits.OERR = 0; //clear FIFO and receive buffer
else
rb[3].arr[rb[3].tail.val++] = U4RXREG; //grab a byte
//TX4
if (uart_is_tx_flag[3])
while (!U4STAbits.UTXBF)
{
if (rb[3].head.val != rb[3].tail.val)
U4TXREG = rb[3].arr[rb[3].head.val++];
else if (!U4STAbits.TRMT)
break;
else
{
if (!UART4_TDE_STATE())
uart_is_tx_flag[3] = false;
UART4_TDE_OFF();
UART4_LED_OFF();
break;
}
}
#endif
#if MAX_NUMBER_UARTS >= 5
//RX5
while (U5STAbits.URXDA)
if (U5STAbits.FERR || U5STAbits.PERR)
dummy_val = U5RXREG; //dummy read
else if (U5STAbits.OERR)
U5STAbits.OERR = 0; //clear FIFO and receive buffer
else
rb[4].arr[rb[4].tail.val++] = U5RXREG; //grab a byte
//TX5
if (uart_is_tx_flag[4])
while (!U5STAbits.UTXBF)
{
if (rb[4].head.val != rb[4].tail.val)
U5TXREG = rb[4].arr[rb[4].head.val++];
else if (!U5STAbits.TRMT)
break;
else
{
if (!UART5_TDE_STATE())
uart_is_tx_flag[4] = false;
UART5_TDE_OFF();
UART5_LED_OFF();
break;
}
}
#endif
// clear the interrupt flag
mT2ClearIntFlag();
}