//发送数据
void USCIA0_Send(char *p_data,unsigned int i_length,unsigned int i_time)
{
unsigned int i_ctr = 0;
S_Assist.C_CircutBusyFlag = 1;
USCIA0_TX_EN;
Delayms(1);
S_HartData.I_RxCtr = 0;
S_HartData.I_RxTotalCtr = 0;
S_HartData.C_RxOK = 0;
S_HartData.I_TxCtr = 0;
S_HartData.C_TxOK = 0;
S_HartData.I_TxTotalCtr = i_length+4;
memset(&S_HartData.C_TxBuf[0],0xFF,4);
memcpy(&S_HartData.C_TxBuf[4],p_data,i_length);
UCA0TXBUF = S_HartData.C_TxBuf[0];
do
{
i_ctr++;
}while(!S_HartData.C_RxOK && i_ctr<=i_time);
S_Assist.C_CircutBusyFlag = 0;
}
#pragma vector=USCIAB0TX_VECTOR
__interrupt void USCI0TX_ISR(void)
{
if(S_HartData.I_TxCtr >= S_HartData.I_TxTotalCtr)
{
S_HartData.I_TxCtr = 0;
S_HartData.I_TxTotalCtr = 0;
S_HartData.C_TxOK = 1;
}
else
{
S_HartData.I_TxCtr++;
UCA0TXBUF = S_HartData.C_TxBuf[S_HartData.I_TxCtr];
}
if(S_HartData.C_TxOK == 1)
{
if(++S_HartData.C_ModbusTxCtr >= 6)
{
USCIA0_RX_EN;
S_HartData.C_ModbusTxCtr = 0;
S_HartData.C_TxOK = 0;
}
}
}
unsigned char C_Rx_FF_Ctr = 0;
unsigned char C_Rx_Total_Ctr = 0;
//--接收中断函数----
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
unsigned char c_rx = 0;
c_rx = UCA0RXBUF;
if(c_rx == 0xFF)
{
if(++C_Rx_FF_Ctr >= 3)
{
S_HartData.I_RxCtr = 0;
C_Rx_Total_Ctr = 0xFF;
S_HartData.C_RxHead = 1;
}
}
else
{
C_Rx_FF_Ctr = 0;
}
if(S_HartData.C_RxHead == 1)
{
if(c_rx == 0x02 || c_rx == 0x82)
{
S_HartData.C_RxHead = 2;
S_HartData.I_RxCtr = 0;
S_HartData.C_RxBuf[S_HartData.I_RxCtr++] = c_rx;
}
else
{
if(c_rx == 0xFF)
{
C_Rx_FF_Ctr++;
}
else
{
C_Rx_FF_Ctr = 0;
S_HartData.C_RxHead = 0;
}
}
}
else if(S_HartData.C_RxHead == 0x02)
{
S_HartData.C_RxBuf[S_HartData.I_RxCtr++] = c_rx;
if(S_HartData.C_RxBuf[0] == 0x02)
{
if(S_HartData.I_RxCtr >= 4)
{
if(C_Rx_Total_Ctr == 0xFF)
{
C_Rx_Total_Ctr = S_HartData.C_RxBuf[3]+5;
}
}
}
else
{
if(S_HartData.I_RxCtr >= 8)
{
if(C_Rx_Total_Ctr == 0xFF)
{
C_Rx_Total_Ctr = S_HartData.C_RxBuf[7]+9;
}
}
}
if(S_HartData.I_RxCtr >= C_Rx_Total_Ctr)
{
C_Rx_FF_Ctr = 0;
S_HartData.C_RxHead = 0;
S_HartData.I_RxTotalCtr = S_HartData.I_RxCtr;
S_HartData.C_RxOK = 1;
}
}
}
|