Page 1 of 1

Lost Communication between PIC and PC - Help!

PostPosted: Tue May 13, 2025 10:11 pm
by dynforceu
Hi everyone,
I'm having a major headache trying to get my PIC16F877A to consistently communicate with my PC over UART. I'm using a USB to Serial adapter (FTDI) and have checked the wiring countless times.
The problem is intermittent. Sometimes the PIC sends data that the PC receives perfectly, other times it's just garbage or nothing at all. I've tried different baud rates, checked for framing errors, and even swapped out the PIC itself, but the issue persists.
Here's a snippet of my code (using MPLAB XC8):
```c
void main(void) {
// Configuration bits... (omitted for brevity)
// UART initialization
TXSTA = 0x20; // Enable transmit, asynchronous mode
RCSTA = 0x90; // Enable receive, serial port
SPBRG = 25; // 9600 baud rate (assuming 4MHz oscillator)
while(1) {
// Send a test message
TXREG = 'H';
while(!TXIF);
TXIF = 0;
__delay_ms(100);
TXREG = 'e';
while(!TXIF);
TXIF = 0;
__delay_ms(100);
TXREG = 'l';
while(!TXIF);
TXIF = 0;
__delay_ms(100);
TXREG = 'l';
while(!TXIF);
TXIF = 0;
__delay_ms(100);
TXREG = 'o';
while(!TXIF);
TXIF = 0;
__delay_ms(100);
TXREG = '!';
while(!TXIF);
TXIF = 0;
__delay_ms(1000);
}
}
```
I suspect there might be an issue with the timing or maybe some interference. I've tried adding decoupling capacitors near the PIC, but it didn't seem to help.
Has anyone encountered a similar problem? Any suggestions on what to check next? I'm pulling my hair out! Any help would be greatly appreciated.
Thanks in advance!.

Re: Lost Communication between PIC and PC - Help!

PostPosted: Mon May 19, 2025 4:15 am
by ric
// Configuration bits... (omitted for brevity)

Is that really where you have your CONFIG bit setting lines?
They should be at the start of the C file, outside your main function and BEFORE you #include any header files at all.

(Also, it would be really useful to let us see your CONFIG lines. There can be problems in them, such as forgetting to disable the WDT, or enabling LVP without grounding the PGM pin.)

Note, all the ""TXIF = 0;" lines are pointless. That is a read-only flag. It is cleared in hardware when you write to TXREG.

Do you have access to a scope or logic analyser to examine the signals?
Chances are it is a problem with your USB-UART adaptor.

Another possibility is, if you are powering your PIC from a grounded power supply, and the PC is a laptop with an ungrounded mains supply, then you are grounding your laptop via your PIC module, which can cause noise problems.