How to ensure serial transmission and reception of required data without any garbage values in the 8051

8051baudratecserialuart

I'm trying to continuously transmit the character 'L' from the microcontroller 8051 to the PC at a baud rate of 9600. The frequency of the 8051 is 16 MHz, and I accordingly loaded the BRL register with "0XFC" for baud rate generation. However, I can see a stream of garbage values on the hyper terminal; I even tried changing the auto reload values from 0xFB to 0xFF to no avail. Is there any way for me to generate the exact Baud Rate in the microcontroller or a way for me to fix this problem?

Edit: Also, even if I transmit just a single alphabet from the PC to the controller, and the code is written in a way for the controller to echo everything sent from the PC sometimes the character the controller sends in response is different from what I'd sent.

My code is as follows:

#include "reg_c51.h"

char uart_data='L';

void main (void) 
{
    SCON = 0x40;                     
    BDRCON &=0xE0;              
    BDRCON |=0x08;               
    BRL=0xFC;                    
    IEN0 = 0xF0;                            
    BDRCON |=0x10;              

    while(1)                  
    {
        SBUF = uart_data;
        while(TI==0);
        TI=0;
    }
}

Best Answer

That's actually a subtle "gotcha" in asynchronous serial communications — if there are never any gaps in the data that are at least as long as a character time, it's possible for the receiver to get out of sync with respect to the byte boundaries, and there's no way to recover. Any '1' followed by a '0' could be construed as being a stop bit/start bit pair.

One possible fix would be to send a byte of all-zeros (or all-ones) every now and then. If the receiver is out of sync, this will force it to resynchronize on the next start bit.