串口接收数据丢失,返回数据只有最后一位,最后一位数据将数组填满了?
时间:10-02
整理:3721RD
点击:
#include <REG51.H>
#include <string.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
//ò???ê???μ?51μ¥???úμ??§??′óD?
#define FOSC_110592M
//#define FOSC_12M
sbit P10=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
sbit P13=P1^3;
sbit P14=P1^4;
sbit P15=P1^5;
int zdf=0;/////′??ú?óê?êybiaozhiwei
unsigned char xdata temp_data_read[21],temp_data_tele_num[15],temp_data_date[21];
//ò???ê??a?úoó·¢?íμ?ê??úμ??úèY£?·¢?íμ?o????ú3ìDò?DDT???£
unsigned int rec_data_len_uart=0; //±ê??Buffer_Uart0?óê?êy×é
unsigned char idata Buffer_Uart0_Rec[25]={0}; //Uart0?D???óê?êy×
uchar recive;
int tmp;
tmp=3;
//uchar infor;
//×¢òa£??T???óê?μ?D?o??1ê?·¢?ííêD?o?£????á???D??·t??3ìDòμ?
/*3?ê??ˉ3ìDò£¨±?D?ê1ó?£?·??ò?T·¨ê?·¢£?£?′?3ìDò???áê1ó??¨ê±?÷1*/
void SerialInti()//3?ê??ˉ3ìDò£¨±?D?ê1ó?£?·??ò?T·¨ê?·¢£?
{
TMOD=0x20;//?¨ê±?÷12ù×÷?£ê?2:8??×??ˉ?????¨ê±?÷
#ifdef FOSC_12M //?ú?aà??ù?Y?§??′óD?éè??2?í?μ?êy?μ3?ê??ˉ′??ú
TH1=0xf3;//×°è?3??μ£?2¨ì??ê2400
TL1=0xf3;
#else
TH1=0xfd;//×°è?3??μ£?2¨ì??ê9600
TL1=0xfd;
#endif //end of SOC_12M
TR1=1;//′ò?a?¨ê±?÷
SM0=0;//éè??′?DDí¨??1¤×÷?£ê?£?£¨10?aò?2?·¢?í£?2¨ì??ê?é±?£?óé?¨ê±?÷1μ?ò?3??ê????£?
SM1=1;//(í?é?)?ú′??£ê???£??¨ê±?÷ò?3?ò?′??í·¢?íò?????μ?êy?Y
REN=1;//′?DD?óê??êDí??£¨òa?èéè??sm0sm1?ù?a′?DD?êDí£?
EA=1;//?a×ü?D??
ES=1;//?a′?DD?ú?D??
}
void clear_rec_data()
{
uchar i,temp_len;
temp_len=strlen(Buffer_Uart0_Rec);
if(temp_len>25)
{
temp_len=25;
}
for(i=0;i<25;i++)
{
Buffer_Uart0_Rec='\0';
}
rec_data_len_uart=0;
}
/*′?DDí¨???D??£?ê?·¢íê3é????è????D??*/
void Serial_interrupt() interrupt 4
{
unsigned char temp_rec_data_uart0;
temp_rec_data_uart0 = SBUF;//?áè??óê?êy?Y
Buffer_Uart0_Rec[rec_data_len_uart]=temp_rec_data_uart0; //?óê?êy?Y
rec_data_len_uart ++;
RI=0;//?óê??D??D?o???á?£?±íê????ìD??óê?
if(rec_data_len_uart>24)
{
rec_data_len_uart=0; //′óí·?aê??óê?êy?Y
}
if((temp_rec_data_uart0=='s')&&(rec_data_len_uart>4))
{
zdf=1;
}
}
void Uart1Send(uchar c)
{
SBUF=c;
while(!TI);//μè′y·¢?ííê3éD?o?£¨TI=1£?3???
TI=0;
}
//′?DD?úá?D?·¢?ícharDíêy×é£?ó?μ????1o?/0??í£?1
void Uart1Sends(uchar *str)
{
while(*str!='\0')
{
SBUF=*str;
while(!TI);//μè′y·¢?ííê3éD?o?£¨TI=1£?3???
TI=0;
str++;
}
}
void sendU()//chuankoufanhuishuju
{
int i,temp_len;
temp_len=strlen(Buffer_Uart0_Rec);
//for(i=0;i<strlen(Buffer_Uart0_Rec);i++)
for(i=0;i<25;i++)
{
Uart1Send(*(Buffer_Uart0_Rec+i));
}
}
//?óê±oˉêy′ó??ê?1s
void DelaySec(int sec)
{
uint i , j= 0;
for(i=0; i<sec; i++)
{
for(j=0; j<65535; j++)
{
}
}
}
void main()
{
uchar i = 0;
SerialInti();
DelaySec(10);//
P14=1; //
while(1)
{
if(zdf==1)
{
sendU();
clear_rec_data();
zdf=0;
DelaySec(1);
}
}
DelaySec(2);
}
#include <string.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
//ò???ê???μ?51μ¥???úμ??§??′óD?
#define FOSC_110592M
//#define FOSC_12M
sbit P10=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
sbit P13=P1^3;
sbit P14=P1^4;
sbit P15=P1^5;
int zdf=0;/////′??ú?óê?êybiaozhiwei
unsigned char xdata temp_data_read[21],temp_data_tele_num[15],temp_data_date[21];
//ò???ê??a?úoó·¢?íμ?ê??úμ??úèY£?·¢?íμ?o????ú3ìDò?DDT???£
unsigned int rec_data_len_uart=0; //±ê??Buffer_Uart0?óê?êy×é
unsigned char idata Buffer_Uart0_Rec[25]={0}; //Uart0?D???óê?êy×
uchar recive;
int tmp;
tmp=3;
//uchar infor;
//×¢òa£??T???óê?μ?D?o??1ê?·¢?ííêD?o?£????á???D??·t??3ìDòμ?
/*3?ê??ˉ3ìDò£¨±?D?ê1ó?£?·??ò?T·¨ê?·¢£?£?′?3ìDò???áê1ó??¨ê±?÷1*/
void SerialInti()//3?ê??ˉ3ìDò£¨±?D?ê1ó?£?·??ò?T·¨ê?·¢£?
{
TMOD=0x20;//?¨ê±?÷12ù×÷?£ê?2:8??×??ˉ?????¨ê±?÷
#ifdef FOSC_12M //?ú?aà??ù?Y?§??′óD?éè??2?í?μ?êy?μ3?ê??ˉ′??ú
TH1=0xf3;//×°è?3??μ£?2¨ì??ê2400
TL1=0xf3;
#else
TH1=0xfd;//×°è?3??μ£?2¨ì??ê9600
TL1=0xfd;
#endif //end of SOC_12M
TR1=1;//′ò?a?¨ê±?÷
SM0=0;//éè??′?DDí¨??1¤×÷?£ê?£?£¨10?aò?2?·¢?í£?2¨ì??ê?é±?£?óé?¨ê±?÷1μ?ò?3??ê????£?
SM1=1;//(í?é?)?ú′??£ê???£??¨ê±?÷ò?3?ò?′??í·¢?íò?????μ?êy?Y
REN=1;//′?DD?óê??êDí??£¨òa?èéè??sm0sm1?ù?a′?DD?êDí£?
EA=1;//?a×ü?D??
ES=1;//?a′?DD?ú?D??
}
void clear_rec_data()
{
uchar i,temp_len;
temp_len=strlen(Buffer_Uart0_Rec);
if(temp_len>25)
{
temp_len=25;
}
for(i=0;i<25;i++)
{
Buffer_Uart0_Rec='\0';
}
rec_data_len_uart=0;
}
/*′?DDí¨???D??£?ê?·¢íê3é????è????D??*/
void Serial_interrupt() interrupt 4
{
unsigned char temp_rec_data_uart0;
temp_rec_data_uart0 = SBUF;//?áè??óê?êy?Y
Buffer_Uart0_Rec[rec_data_len_uart]=temp_rec_data_uart0; //?óê?êy?Y
rec_data_len_uart ++;
RI=0;//?óê??D??D?o???á?£?±íê????ìD??óê?
if(rec_data_len_uart>24)
{
rec_data_len_uart=0; //′óí·?aê??óê?êy?Y
}
if((temp_rec_data_uart0=='s')&&(rec_data_len_uart>4))
{
zdf=1;
}
}
void Uart1Send(uchar c)
{
SBUF=c;
while(!TI);//μè′y·¢?ííê3éD?o?£¨TI=1£?3???
TI=0;
}
//′?DD?úá?D?·¢?ícharDíêy×é£?ó?μ????1o?/0??í£?1
void Uart1Sends(uchar *str)
{
while(*str!='\0')
{
SBUF=*str;
while(!TI);//μè′y·¢?ííê3éD?o?£¨TI=1£?3???
TI=0;
str++;
}
}
void sendU()//chuankoufanhuishuju
{
int i,temp_len;
temp_len=strlen(Buffer_Uart0_Rec);
//for(i=0;i<strlen(Buffer_Uart0_Rec);i++)
for(i=0;i<25;i++)
{
Uart1Send(*(Buffer_Uart0_Rec+i));
}
}
//?óê±oˉêy′ó??ê?1s
void DelaySec(int sec)
{
uint i , j= 0;
for(i=0; i<sec; i++)
{
for(j=0; j<65535; j++)
{
}
}
}
void main()
{
uchar i = 0;
SerialInti();
DelaySec(10);//
P14=1; //
while(1)
{
if(zdf==1)
{
sendU();
clear_rec_data();
zdf=0;
DelaySec(1);
}
}
DelaySec(2);
}
还能把代码整理好!
程序问题,你程序中就只能接受一个字节。建议你接受多个字节的时候用中断接收,这样可以挨个储存