#define COMM_TIMEOUT 0xff
unsigned char g_ch=0;
unsigned char g_flag=0;
void InitComm()
{
T2CON=0x30;
RCAP2H=255;
RCAP2L=184;
SCON=0x50;
ES=1;
EA=1;
TR2=1;
}
////////////////////////////////////////////
//function:Receive 1 byte
//return:0--ok 1---failure
unsigned char ReadComm(unsigned char *ch)
{
unsigned int timeout = COMM_TIMEOUT;
timeout = COMM_TIMEOUT;
while(timeout--)//µ÷ÊÔÍê³Éºó£¬ÇëʹÓó¬¹ýʱ
{
if(RI==1)
{
RI=0;
for(i=0;i <5;i++)
{
}
*ch = SBUF;
RI=0;
return ID_OK;
}
}
return ID_ERR;
}
////////////////////////////////////////////
//function:Send 1 byte
//return:0--ok 1---failure
unsigned char WriteComm(unsigned char ch)
{
unsigned int xdata timeout=COMM_TIMEOUT;
SBUF=ch;
while(timeout--)
{
if(TI==1)
{
TI=0;
return ID_OK;
}
}
return ID_ERR;
}
//
void Comm_ISR() interrupt 4
{
ES=0;
if(RI==1)
{
ReadComm(&g_ch);
g_flag=1;
}
ES=1;
}
void main(void)
{
unsigned char xdata testbuf[100]="12345678901234567890";
unsigned char xdata command=0;
g_flag=0;
InitComm();//Baud9600);
SendResponse(0x01,testbuf,3);
EA=1;
ES=1;
while(1)
{
if(g_flag)
{
g_flag=0;
WriteCom(g_ch);
}
}
}
/////////////////////////////////////////////
对照一下程序,如果有问题,可能就是你的RXD线没有接通,
或者,你的+5V电源没有接对。