void InitSio(void)
{
u16 RELOAD_COUNT = 0;
//使用独立波特率发生器作为波特率发生器
S2CON = 0x50; //0101,0000 8位可变波特率,无奇偶校验位,允许接收
RELOAD_COUNT = (int)(XTAL/BaudRateVal/32/12 + 0.5);
RELOAD_COUNT = 256 - RELOAD_COUNT;
//BRT = 0xFA;//22.118400MHz,12T,SMOD=0,9600bps
BRT = RELOAD_COUNT;
AUXR = 0x11; // T0x12,T1x12,UART_M0x6,BRTR,S2SMOD,BRTx12,EXTRAM,S1BRS
// ES = 1; //允许串口1中断
// ES2 = 1
IE2 = 0x01; //允许串口2中断,ES2=1
EA = 1; //开总中断
}
//----------------------------------------------------------------------------------------------
void WrRiBuf(unsigned char c)
{
disjoin_send(c);
//ribuf[rirear] = c;
//rirear++;
if(rirear >= RIBUFMAX)
{
rirear = 0;
Rs485dislow++;
}
if(ribufcount != RIBUFMAX) ribufcount ++;
}
//-----------------------------------------------------------------------------
unsigned char RdRiBuf()
{
char p;
unsigned char c;
p = rirear - ribufcount;
if(p < 0) p = RIBUFMAX + p;
ribufcount--;
c = ribuf[p];
return c;
}
//----------------------------------------------------------------------------------------------
void IntComm() interrupt 8
{
u16 k = 0;
k = S2CON ;
k = k & 0x01;
if(k==1) //RI
{
IE2 = 0x00; //允许串口2中断,ES2=1
S2CON = S2CON & 0xFE; //1111,1110
WrRiBuf(S2BUF);
IE2 = 0x01; //允许串口2中断,ES2=1
S2CON = S2CON & 0xFE; //1111,1110
}
else
{
S2CON = S2CON&0xfd;
}
}
void handlecomm()
{
char c;
//char i;
if (ribufcount)
c = RdRiBuf();
if(ribuf[0] == 'F'&&ribuf[1] == 'F'){RIBUFMAX = 20;}
if(ribuf[0] == 'A'&&ribuf[1] == '0'){RIBUFMAX = 23;}
manage_key485DataTestdislowSub(); //显示接收到的数据
}
//-----------------------------------------------------
void disjoin_send( u8 hex)//把十六进制数拆开ASCII显示在屏上
{
//把十六进制数拆开ASCII
u8 high;
u8 low;
high = hex>>4;//去掉低四位,直接得到高四位
low = hex&0x0f;
high = CharHex(high);//转换为高位的ASCII码
low = CharHex(low);
ribuf[rirear] = high;
delayAny(2);
rirear++;
ribuf[rirear] = low;
delayAny(2);
rirear++;
ribuf[rirear]= 0x20;
rirear++;
//if(rirear>RIBUFMAX*3-1)rirear1 = 0;
//if(rirear >=20) Rs485dislow = Rs485dislow+1;
}
//把一个十六进制转换为一个ASCII字符
unsigned char CharHex(char ch)
{
if((ch>=0)&&(ch<=9))
return ch+0x30;
else if((ch>=10)&&(ch<=15))
return ch+'A'-10;//即变为从0开始对应字符A
else return (-1);
}
main()
{
handlecomm();
}
我的函数怎么得不到我想要的东西.我想接收一个字符串转一行.
1,我开始接收更短的可以.我变换长字符长度更长的串是.在变回短的.短的后面就增加了几个字符.如果我重新开机.又可以接收到短的长度.不会在后面多字符.
2.我接收时怎么有错码.要不disjoin_send().直接PC发时不会错码.