1
\$\begingroup\$

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);
}

enter image description here

\$\endgroup\$
9
  • 2
    \$\begingroup\$ How did you come up with 0x81 for SPBRG? How is your micro clocked? \$\endgroup\$ – Vladimir Cravero May 24 '17 at 12:10
  • \$\begingroup\$ How is the PIC wired to the PC? What cable and connector is used? \$\endgroup\$ – Entrepreneur May 24 '17 at 13:25
  • \$\begingroup\$ @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) \$\endgroup\$ – Sreez.. May 25 '17 at 2:32
  • \$\begingroup\$ @Entrepreneur I have used a DB9 connector which is then connected to a RS232-to-USB converter cable \$\endgroup\$ – Sreez.. May 25 '17 at 2:37
  • \$\begingroup\$ Definitely don't want to guess stop bits, it could throw you out of sink \$\endgroup\$ – sstobbe May 25 '17 at 3:34
1
\$\begingroup\$

I think you need to poll the Transmit Shift Register Status TRMT rather than TXIF. When TRMT==1 the register is empty and it is ready to transmit a byte.

But upon a second look I notice that the wrong characters are always off by the same bit in the ASCII code. When a character is wrong the first (low order) bit in the ASCII character is supposed to be 1, but it is replaced with a 0. 'S' turns into 'R', 'O' into 'N', 'u' into 't'. That seems like a signal integrity issue over the data cable.

While I do think that you will need to poll the TRMT bit, I don't think that your current problem is software. Look for anything that may affect the quality of the cable connection.

\$\endgroup\$
2
  • \$\begingroup\$ But while doing the loop back test, the results are positive. If anything was wrong with the cable then i should have known at that time right? By the way I did replace TXIF with TRMT. :) \$\endgroup\$ – Sreez.. May 25 '17 at 4:55
  • \$\begingroup\$ thanks to you. Finally figured it out that the problem was with the cable. And the error was also seen during the loop back test when i gave repeated 'A' as input. :) \$\endgroup\$ – Sreez.. May 25 '17 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.