RS485总线C程序,单片机控制,主机带分机。#include
#define uchar unsigned char
#define byte unsigned char
#define BYTE unsigned char
#define uint unsigned int
#define word unsigned int
#define word unsigned int
#define WORD unsigned int
#define Timeout 300
sbit Ctrl485=P1^0;
uchar xdata combuff[];
void init_serialcomm(void)
{
TMOD=0x22;
TL1=0xfd;
TH1=0xfd;
SCON=0x50;
PCON=0x80;
TR1=1;
IE=0x90;
EA=1;
Ctrl485=1;
}
byte readRi(uint x)
{ while(RI==0)
{
if(x==0)return(0);
x--;
}
RI=0;
return(1);
}
void rec_serial_data(uchar rec_leng,uchar*sbufRam)
{
uchar i;
sbufRam[0]=SBUF;
RI=0;
rec_leng=1;
do
{
if(readRi(Timeout)==1)
{
sbufRam[i]=SBUF;
rec_leng++;
}
else return;
}while(RI==0);}
void tarn_serial_data(uchar tran_leng,uchar*sbufRam)
{
uchar i;
Ctrl485=0;
for(i=0;i
1