c语言485通信源程序
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<fe)< p="">
#define PARITY_ERROR (1<<upe)< p="">
#define DATA_OVERRUN (1<<ovr)< p="">
#define DATA_REGISTER_EMPTY (1<<udre)< p="">
#define RX_COMPLETE (1<<rxc)< p="">
// USART1 Receiver buffer
#define RX_BUFFER_SIZE1 128
uchar rx_buffer1[RX_BUFFER_SIZE1];
truncated c语言
uchar rx_process_buffer1[160];
uchar rx_process_counter1;
#if RX_BUFFER_SIZE1<256
unsigned char rx_rd_index1,rx_counter1;
#else
unsigned int rx_rd_index1,rx_counter1;
#endif
// This flag is set on USART1 Receiver buffer overflow
bit rx_buffer_overflow1;
// USART1 Receiver interrupt service routine
volatile bit comm_flg=false;
bit comm_flg1=true;
uchar comm_state=0;
uchar comm_over;
interrupt [USART0_RXC] void usart0_rx_isr(void)
{
char status,data;
uchar i;
status=UCSR0A;
data=UDR0;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer1[rx_rd_index1]=data;
if (++rx_rd_index1 == RX_BUFFER_SIZE1) rx_rd_index1=0;
if (++rx_counter1 == RX_BUFFER_SIZE1)
{
rx_counter1=0;
rx_buffer_overflow1=1;
}
}
comm_over=250;
if(rx_counter1==2)
{
if((rx_buffer1[0]!=0xa3)||(rx_buffer1[1]!=0xa4)){rx_counter1=0;rx_rd_index1=0; } }
else if((rx_counter1>=13)&&(rx_counter1==rx_buffer1[7]+10))
{
if(comm_state!=0){comm_flg1=true;}
else
{
for(i=0;i< p="">
rx_process_counter1=rx_counter1;
rx_counter1=0;rx_rd_index1=0;
comm_flg=true;
}
}
//ledon(10);
}
// USART1 Transmitter buffer
#define TX_BUFFER_SIZE1 96
char tx_buffer1[TX_BUFFER_SIZE1];
#if TX_BUFFER_SIZE1<256
unsigned char tx_rd_index1,tx_counter1;
#else
unsigned int tx_rd_index1,tx_counter1;
#endif
// USART1 Transmitter interrupt service routine
interrupt [USART0_TXC] void usart1_tx_isr(void)
{
ledon(10);
if (tx_counter1)
{
--tx_counter1;tx_rd_index1++;
if(tx_counter1>0){while ( !( UCSR0A & (1<
if (tx_rd_index1 == TX_BUFFER_SIZE1) tx_rd_index1=0;
}
}
void send_bytes(uchar len)
{
if(len==0)return;
tx_counter1=len;
tx_rd_index1=0;
PORTD=PORTD|0X10;delay10us(300);
UDR0=tx_buffer1[tx_rd_index1];
}
<>
</rxc)<>
</udre)<>
</ovr)<>
</upe)<>
</fe)<>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。