4085 views|1 replies

85

Posts

0

Resources
The OP

Help: 8051 serial port interrupt receiving query sending data loss [Copy link]

Use C8051F020 to communicate with PC via serial port. The microcontroller receives data via interrupt and sends data via query. My program is to achieve: define 5 bytes of data as one frame, PC sends data to the microcontroller, when the data reaches one frame (5 bytes), the microcontroller sends this frame of data to the PC. I used 2 BUFFs, the first BUFF is used to receive the data in SBUF0 in sequence, and transfer the data in the first BUFF to the second BUFF. When the data in the second BUFF reaches 5 bytes, the data fetch flag is set, and the data in BUFF2 is sent from the serial port. Use the serial port debugging assistant for debugging, and the test phenomenon is as follows: Send 5 bytes of data as one frame (1 packet of data), for example: AA BB CC DD EE. Every time the microcontroller is reset or restarted, the first frame of data received after sending is always AA CC DD EE, and the second byte of data is lost; then continue to send without resetting, and AA BB CC DD will be received normally. EE, as long as it is not reset or restarted, it will be normal afterwards. I used hexadecimal and non-hexadecimal, and the second data was lost "for the first time", but it was normal afterwards. And I observed the second element in the array inbuf2[len2] in BUFF2, and the observation showed that the second element "BB" was normal. In layman's terms, the first time it was sent after reset, the second byte of data "should" be received normally, but it just couldn't be sent. I also tried to see if it was a delay problem, but I sent it manually byte by byte, and the above problem still occurred, so I thought it was not a delay problem. This program does not have a checksum code. If a checksum is added, will it improve? May I ask, how to add a checksum? Another small question, what if the two BUFFs are reset? Please help me analyze it, thank you! The program is as follows, I omitted some codes. #define len 5 //BUFF1 buffer length unsigned char idata inbuf[len]; unsigned char idata *inlast=inbuf; unsigned char idata *getlast=inbuf; bit inbufsign; //BUFF1 buffer not empty flag = 1, empty = 0 bit inbufful; //BUFF1 buffer full flag full = 1, not full = 0 bit read_flag=0; //Data access flag int i=0; unsigned char *str; unsigned int strlen; #define len2 5 //BUFF2 buffer length unsigned char idata inbuf2[len2]; unsigned char idata c,c1; //Main function// void main (void) { ... ... EA=1; //Interrupt open while(1) //Query and send { if(read_flag) //If the data access flag is set, the read data will be sent from the serial port { read_flag=0; sendstring(inbuf2,len2); } } } //Serial port receive interrupt// void serial() interrupt 4 { if(RI0) { RI0=0; if(!inbufful) //Check if BUFF1 is full { *inlast=SBUF0; //Put in data inlast++; //Increase the position by 1 inbufsign=1; if(inlast=inbuf+len) inlast=inbuf; //Address returns to the top and bottom if(inlast=getlast) inbufful=1; //BUFF1 is full } c1=getbyte(); inbuf2=c1; //Read the data in BUFF1 into the array of BUFF2 in sequence i++; if(i==5) { read_flag=1; i=0; //If 5 bytes are reached, set the number flag and start sending } } } //Get a byte from buffer BUFF1// unsigned char getbyte(void) { while(!inbufsign); //BUFF1 is empty and waits for ES0=0; c=*getlast; //Get data getlast++; //Increase the position of the last data taken by 1 inbufful=0; //BUFF1's full flag is cleared if(getlast=inbuf+len) getlast=inbuf; //Address goes to the top and returns to the bottom if(getlast=inlast) inbufsign=0; //Addresses are equal, BUFF1 is set to zero ES0=1; return(c); } //Send a character to the serial port // void sendchar(unsigned char ch) { SBUF0=ch; while(TI0==0); TI0=0; } //Send a string to the serial port, strlen is the length of the string // void sendstring(unsigned char *str,unsigned int strlen) { int k; for(k=0;k
#define len 5 //BUFF1 buffer length unsigned char idata inbuf[len]; unsigned char idata *inlast=inbuf; unsigned char idata *getlast=inbuf; bit inbufsign; //BUFF1 buffer not empty flag = 1, empty = 0 bit inbufful; //BUFF1 buffer full flag full = 1, not full = 0 bit read_flag=0; //Get number flag int i=0; unsigned char *str; unsigned int strlen; #define len2 5 //BUFF2 buffer length unsigned char idata inbuf2[len2]; unsigned char idata c,c1; //Main function// void main (void) { ... ... EA=1; //Interrupt open while(1) //Query and send { if(read_flag) //If the get number flag is set, the read number will be sent from the serial port { read_flag=0; sendstring(inbuf2,len2); } } } //Serial port receive interrupt// void serial() interrupt 4 { if(RI0) { RI0=0; if(!inbufful) //Check whether BUFF1 is full{ *inlast=SBUF0; //Put in data inlast++; //Put in position plus 1 inbufsign=1; if(inlast=inbuf+len) /////////////////////////////????????????????????? inlast=inbuf; //Address to the top and back to the bottomif(inlast=getlast) ///////////////////////////????????????????????? inbufful=1; //BUFF1 full flag} c1=getbyte(); inbuf2=c1; //Read the data in BUFF1 into the array of BUFF2 in sequencei++; if(i==5) { read_flag=1; i=0; //If it reaches 5 bytes, set the number flag and start sending} } } //Get a byte from buffer BUFF1// unsigned char getbyte(void) { while(!inbufsign); //BUFF1 waits for ES0=0; c=*getlast; //Get datagetlast++; //The last data position is increased by 1 inbufful=0; //The full flag of BUFF1 is cleared to 0 if(getlast=inbuf+len) /////////////////////////////????????????????????? getlast=inbuf; //The address goes to the top and returns to the bottomif(getlast=inlast) /////////////////////////////????????????????????? inbufsign=0; //Addresses are equal, BUFF1 is set to empty flag ES0=1; return(c); } //Send a character to the serial port// void sendchar(unsigned char ch) { SBUF0=ch; while(TI0==0); TI0=0; } //Send a string to the serial port, strlen is the length of the string// void sendstring(unsigned char *str,unsigned int strlen) { int k; for(k=0;k
Help: 8051 serial port interrupt receiving query sending data loss


This post is from Embedded System

Latest reply

#define len 5 //BUFF1 buffer length unsigned char idata inbuf[len]; unsigned char idata *inlast=inbuf; unsigned char idata *getlast=inbuf; bit inbufsign; //BUFF1 buffer not empty flag = 1, empty = 0 bit inbufful; //BUFF1 buffer full flag full = 1, not full = 0 bit read_flag=0; //Get number flag int i=0; unsigned char *str; unsigned int strlen; #define len2 5 //BUFF2 buffer length unsigned char idata inbuf2[len2]; unsigned char idata c,c1; //Main function// void main (void) { ... ... EA=1; //Interrupt open while(1) //Query and send { if(read_flag) //If the get number flag is set, the read number will be sent from the serial port { read_flag=0; sendstring(inbuf2,len2); } } } //Serial port receive interrupt// void serial() interrupt 4 { if(RI0) { RI0=0; if(!inbufful) //Check whether BUFF1 is full{ *inlast=SBUF0; //Put in data inlast++; //Put in position plus 1 inbufsign=1; if(inlast=inbuf+len) /////////////////////////////????????????????????? inlast=inbuf; //Address to the top and back to the bottomif(inlast=getlast) ///////////////////////////????????????????????? inbufful=1; //BUFF1 full flag} c1=getbyte(); inbuf2=c1; //Read the data in BUFF1 into the array of BUFF2 in sequencei++; if(i==5) { read_flag=1; i=0; //If it reaches 5 bytes, set the number flag and start sending} } } //Get a byte from buffer BUFF1// unsigned char getbyte(void) { while(!inbufsign); //BUFF1 waits for ES0=0; c=*getlast; //Get datagetlast++; //The last data position is increased by 1 inbufful=0; //The full flag of BUFF1 is cleared to 0 if(getlast=inbuf+len) /////////////////////////////????????????????????? getlast=inbuf; //The address goes to the top and returns to the bottomif(getlast=inlast) /////////////////////////////????????????????????? inbufsign=0; //Addresses are equal, BUFF1 is set to empty flag ES0=1; return(c); } //Send a character to the serial port// void sendchar(unsigned char ch) { SBUF0=ch; while(TI0==0); TI0=0; } //Send a string to the serial port, strlen is the length of the string// void sendstring(unsigned char *str,unsigned int strlen) { int k; for(k=0;k  Details Published on 2008-8-20 10:28

78

Posts

0

Resources
2
#define len 5 //BUFF1 buffer length unsigned char idata inbuf[len]; unsigned char idata *inlast=inbuf; unsigned char idata *getlast=inbuf; bit inbufsign; //BUFF1 buffer not empty flag = 1, empty = 0 bit inbufful; //BUFF1 buffer full flag full = 1, not full = 0 bit read_flag=0; //Get number flag int i=0; unsigned char *str; unsigned int strlen; #define len2 5 //BUFF2 buffer length unsigned char idata inbuf2[len2]; unsigned char idata c,c1; //Main function// void main (void) { ... ... EA=1; //Interrupt open while(1) //Query and send { if(read_flag) //If the get number flag is set, the read number will be sent from the serial port { read_flag=0; sendstring(inbuf2,len2); } } } //Serial port receive interrupt// void serial() interrupt 4 { if(RI0) { RI0=0; if(!inbufful) //Check whether BUFF1 is full{ *inlast=SBUF0; //Put in data inlast++; //Put in position plus 1 inbufsign=1; if(inlast=inbuf+len) /////////////////////////////????????????????????? inlast=inbuf; //Address to the top and back to the bottomif(inlast=getlast) ///////////////////////////????????????????????? inbufful=1; //BUFF1 full flag} c1=getbyte(); inbuf2=c1; //Read the data in BUFF1 into the array of BUFF2 in sequencei++; if(i==5) { read_flag=1; i=0; //If it reaches 5 bytes, set the number flag and start sending} } } //Get a byte from buffer BUFF1// unsigned char getbyte(void) { while(!inbufsign); //BUFF1 waits for ES0=0; c=*getlast; //Get datagetlast++; //The last data position is increased by 1 inbufful=0; //The full flag of BUFF1 is cleared to 0 if(getlast=inbuf+len) /////////////////////////////????????????????????? getlast=inbuf; //The address goes to the top and returns to the bottomif(getlast=inlast) /////////////////////////////????????????????????? inbufsign=0; //Addresses are equal, BUFF1 is set to empty flag ES0=1; return(c); } //Send a character to the serial port// void sendchar(unsigned char ch) { SBUF0=ch; while(TI0==0); TI0=0; } //Send a string to the serial port, strlen is the length of the string// void sendstring(unsigned char *str,unsigned int strlen) { int k; for(k=0;k
Help: 8051 serial port interrupt receiving query sending data loss


This post is from Embedded System

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

    EEWorld
    subscription
    account

    EEWorld
    service
    account

    Automotive
    development
    circle

    Robot
    development
    community

    About Us Customer Service Contact Information Datasheet Sitemap LatestNews

    Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

    Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
    快速回复 返回顶部 Return list