|
楼主 |
发表于 2014-2-2 10:54:29
|
显示全部楼层
再发一个采用中断接收单字节的程序,效果很好。接收大量的数据,也不会乱码。
#define Uart_IN_IO GPIO3 //GPIO3=接收!
#define MAX_RS232_CNT 20 //通讯的字节数
//**************************************************************
unsigned char Get_Uart_Rece_Dat (void ){
unsigned char i,bt;
BPS_Delay();
//************************************************************
bt=8;
i=0;
while(bt--){
i>>=1;
if(Uart_IN_IO) i |=0x80;
//*************************************************************
//延时等待下一个位
BPS_Delay();
}
//************************************************************
//等待结束位 (1)
//BPS_Delay();
return i;
}
//-----------------------------------------------------------------------
static char getBuf[MAX_RS232_CNT],cnt=0; //接收缓存!
static bit fFlag=0;
//********************************************************************
void Uart_Rece (void ){
//***********************************************************
//产生中断,开始接收!查询!
PR2=TtK/2+TtK;
getBuf[cnt]=Get_Uart_Rece_Dat();
if(++cnt >=sizeof(getBuf)){
fFlag=1;
cnt=0;
}
}
//********************************************************************
void SendBack(void){
// unsigned char i;
if(fFlag){
fFlag=0;
//**************************************
//数据校验!
SendOut(getBuf,MAX_RS232_CNT);//数据回发,检验是否正确!
}
} |
|