This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Serial data between two 8051

I would like to have my two 89C51RD2 communicate with each other via UART. They are connected such that one TX pin is connected to the other's RX pin and vice versa.

I tried sending byte by byte and it is working. However, when I tried to transmit larger amount of data at one go, the receiving end isnt able to read the data.

I feel that I am missing something silly. Is the microcontroller able to store the incoming data while I poll and get them byte by byte?

Would somebody please advise. Thank you.

Cheers

Parents Reply Children
  • Here is my code used to do the polling whether anything is recieved

    char char_poll(char xdata *chr){
    
    
    	if (RI==1){			//Character is available
    		*chr = SBUF;            //Store recieved character into variable chr
    		RI = 0;
    		return 1;
    	}else{				//Character not available
    		return 0;
    	}
    
    return(chr);
    
    

    Another function is the one that is called to makes sure that it gets all data recieved

    char data_poll(void)
    {
      char xdata c;
    
      while(char_poll(&c)) {
    
            /*Process data byte by byte*/
      }
    
    }
    

    Thanks

  • Your problem is that the second time char_poll() is called the second byte almost certainly won't have arrived in SBUF, so zero is returned causing data_poll() to exit.

    Bear in mind that at, say, 9600 baud roughly one character arrives per millisecond - char_poll() is being called much faster than that.

    In your existing reception scheme you would need to wait for RI to go true in char_poll() for some time rather than returning immediately if RI is false.

    Stefan

  • You could always do something like this:

    while (1) {
        if (char_poll(&c)) {
            /* Process data byte by byte */
        }
    }
    

    The only condition now is that if your program needs to do something other than just process this serial data, you'll have to have your byte by byte processing detect the end of whatever "message" you're getting and break out of the loop.

  • In that case if I call data_poll() periodically by placing it in an infinite loop, then the problem would not exist isnt it? As the char_poll() will still come in the next time round to read the data.

    Am I right to say that, a potential problem would be that transmit side is too fast such that before the reciever can retrieve the previous data sent from SBUF, another byte is written to it and hence data is lost?