# scrambled uart output

I am new to pic uart communicatioon with PC using RS232. I have tried the below code and used Hyperterminal to view the output. But the output gets scrambled here and there each time when I reset pic. I have tried changing stopbits from 1 to 0 in the hyperterminal but the output remains the same. The settings kept on hyperterminal is

1. Baudrate 9600
2. Parity none
3. Stopbits 1 & 2(for both the output remains the same.)
4. No. Of bits 8

Am using PIC16F877a and have coded using MPLAB XC8 compiler The outputs are there in the attachment

Could you please tell where i did the mistake.

void main()

{
TRISC = 0x80;
TXSTA=0x24;
RCSTA=0x90;
SPBRG=0x81;
unsigned int i=0;
char name[]="Sample Output";

while(name[i]!='\0')
{
TXREG=name[i];
while(!TXIF);
__delay_ms(1000);
i++;

}
while(1);
}


• How did you come up with 0x81 for SPBRG? How is your micro clocked? – Vladimir Cravero May 24 '17 at 12:10
• How is the PIC wired to the PC? What cable and connector is used? – Entrepreneur May 24 '17 at 13:25
• @Vladimir Cravero ..Am using 20Mhz on board crystal oscillator for clocking. And I choosed 0x81 by the formula SPBRG = (FOSC-(baud rate*16))/(9600*16) – Sreez.. May 25 '17 at 2:32
• @Entrepreneur I have used a DB9 connector which is then connected to a RS232-to-USB converter cable – Sreez.. May 25 '17 at 2:37
• Definitely don't want to guess stop bits, it could throw you out of sink – sstobbe May 25 '17 at 3:34