6) 芯片使用例程:
#define RS485_Receive 1
#define RS485_Send 0
//--定義串口緩存:緩存為循環(huán)鏈表
unsigned char rs_buffer[RS_BUFFER_SIZE]; //串口接收緩存
int rs_buffer_pointer; //串口緩存首指針(指向最后一個接受到的字節(jié))
int rs_buffer_amount; //串口緩字節(jié)數(shù)
//--定義結(jié)束
//--定義串口與MCU的硬件連接
sbit RS_Mode=P0^0;
//--定義結(jié)束
void RS_Init()
{
TH1=FF;
TL1=FF;
TMOD=0x21;
PS=1;
EA=1;
ET1=0;
SM0=0;
SM1=1;
SM2=0;
TR1=1;
REN=1;
RI=0;
TI=0;
ES=1;
RS_Mode=RS485_Receive;
rs_buffer_poiter=-1;
rs_buffer_amount=0;
}
void RS_Receive() interrupt 4 using 2 //中斷方式接收串口數(shù)據(jù)
{
ES=0;
if(RI)
{
//此處處理串口緩存
rs_buffer_pointer++;
rs_buffer_pointer=rs_buffer_pointer%RS_BUFFER_SIZE;
rs_buffer[rs_buffer_pointer]=SBUF;
rs_buffer_amount++;
RI=0;
}
ES=1;
}
void RS_Send(unsigned char data) //用戶函數(shù),發(fā)送一個字節(jié)的數(shù)據(jù)
{
ES=0;
RS_Mode=RS485_Send;
SBUF=data;
RS_Mode=RS485_Receive;
ES=1;
while(TI==0);
TI=0;
}
評論