📄 uart_module.c
字号:
/****************************** Interrupt UART **************************/
#int_rda
void serial_isr( )
{
RxBuff[Index] = getc( );
Index++;
if( Index == 1 )
{
if( RxBuff[0] != SYNC_MCU ) Index=0;
}
else if( RxBuff[3] == Index )
{
Index = 0;
TimeOut = 0;
RxFlag = TRUE;
}
}
/************************************************************************/
/**************************** Interrupt Timeout *************************/
#int_timer0
void interupt_timer0_isr( )
{
if( Index > 0 )
{
TimeOut++;
if( TimeOut == TIME_OUT )
{
Index = 0;
TimeOut = 0;
//printf("Watch dog on.....");
//printf("\n\rIndex = %u",Index);
}
}
}
/************************************************************************/
/****************************** Tx Function *****************************/
extern void uart_transmit( int Module, int Add_Sub, int PKG_Length )
{
static BOOLEAN Flag=0;
TxBuff[0] = SYNC_PC;
TxBuff[1] = IPConfig;
TxBuff[2] = Module;
TxBuff[3] = PKG_Length;
TxBuff[4] = Add_Sub;
UartCrc = 0x00;
for( k=0;k<(PKG_Length - 2);k++ )
{
TxBuff[5+k] = DatBuff[k];
}
for( k=0;k<(PKG_Length - 1);k++ ) UartCrc = TxBuff[k]^UartCrc;
if( UartCrc == SYNC_PC )
{
UartCrc = ( UartCrc^0xFF ) + 1;
TxBuff[PKG_Length-1] = UartCrc;
}
else TxBuff[PKG_Length-1] = UartCrc;
Flag = 1;
while(Flag)
{
for( k=0;k<PKG_Length;k++ )
{
putc( TxBuff[k] );
delay_ms( 5 );
}
Flag = 0;
}
}
/************************************************************************/
/*************************** CRC generate *******************************/
BOOLEAN crc_decode( void )
{
static BOOLEAN flag;
flag = FALSE;
RxFlag = FALSE;
UartCrc = 0x00;
for( i=0;i<PKG_RX_CRC;i++ ) UartCrc = RxBuff[i]^UartCrc;
if( UartCrc == RxBuff[PKG_RX_CRC] )flag = TRUE;
else if( UartCrc != RxBuff[PKG_RX_CRC] )
{
UartCrc = ( UartCrc^0xFF )-1;
if( UartCrc == SYNC_MCU ) flag = TRUE;
}
return( flag );
}
/************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -