0
\$\begingroup\$

I am using the diagram recommended in the SIM800L datasheet to connect it to a PIC12F1822 MCU operating at 3.3V and 16MHz using its internal RC oscillator.

(SIM800L TX) --- (1K Resistor) --- (MCU RX)

(SIM800L RX) --- (1K Resistor) --- (MCU TX)
              |
       (5.6K Resistor)
              |
            (GND)

I used 2400bps baud rate to send commands from MCU to SIM800L but some characters were lost during communication. I also checked 9600bps but the problem still exists. I removed the SIM800L module and connected the PIC to the PC using a USB to Serial adapter and still have the problem. This is my PIC program code. I am using MPLAB X IDE with XC8 (v1.35) compiler.

TXSTA = 0b00100010;       // enable TX, select asynchronous
RCSTA = 0b10010000;       // enable RX and EUSART
SPBRG = 207;              // 1200bps
...
// super loop
for (;;) {
    CLRWDT();                                     // clear watchdog timer
    if (UARTTXBuffInPos > UARTTXBuffOutPos) {     // if there is new data
        if ((TXIF) && (SSP1STATbits.P)) {         // if I2C is idle and last UART byte is sent
            TXREG = UARTTXBuff[UARTTXBuffOutPos]; // send data
            UARTTXBuffOutPos++;                   // next byte
        }
    } else {
        UARTTXBuffInPos = 0;                      // reset buffer pointer
        UARTTXBuffOutPos = 0;                     // reset buffer pointer
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ How does data get into UARTTXBuf? \$\endgroup\$
    – brhans
    Feb 18 at 4:48
  • \$\begingroup\$ Why are you looking at the SSP1STAT.P bit? It has noting to do with UART comms. \$\endgroup\$
    – brhans
    Feb 18 at 4:49
  • \$\begingroup\$ I fill UARTTXBUFF with data coming from I2C. In fact PIC used as a I2C<=>UART adapter because of lack of extra serial ports on my linux embedded board. And because of this I check SSP1STAT.P. I checked I2C data sending and receiving by making an echo feedback and everything was OK and also UART receiver works fine. There is only problem with UART transmitter. \$\endgroup\$
    – Vahid
    Feb 18 at 5:51
  • 1
    \$\begingroup\$ Most likely there is a race condition between the code you have listed and the IIC code that you did not list. I assume the IIC code updates UARTTXBuffInPos from an ISR when IIC data is received. If IIC data is received just after the empty buffer check, but before clearing UARTTXBuffInPos, that byte will get lost. \$\endgroup\$ Feb 20 at 16:18
  • \$\begingroup\$ @Steve, Thanks man. That's it. I moved the last two lines into ISR where the buffer is filling and the problem solved. Please write down your answer so I can accept it as the correct answer. \$\endgroup\$
    – Vahid
    Feb 20 at 21:19

2 Answers 2

1
\$\begingroup\$

Most likely there is a race condition between the code you have listed and the IIC code that you did not list. I assume the IIC code updates UARTTXBuffInPos from an ISR when IIC data is received. If IIC data is received just after the empty buffer check, but before clearing UARTTXBuffInPos, that byte will get lost. You will need to provide some sort of mutual exclusion when updating UARTTXBuffInPos.

\$\endgroup\$
1
\$\begingroup\$

Internal oscillators are not precise enough for UART communication.

Adjust the oscillator frequency or baud rate up or down to try narrow down when it works, or use autobaud detect feature of the MCU.

Or just use a crystal with the MCU, or even a ceramic resonator.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for your answer, I'll try it. But I already worked with UART using internal RC oscillator of PIC and there were no problems. \$\endgroup\$
    – Vahid
    Feb 18 at 4:39
  • \$\begingroup\$ @Vahid With the same IC, in same temperature and supply voltage? The manufacturing toletances can exceed UART limits, so if one IC works, another one might not. \$\endgroup\$
    – Justme
    Feb 18 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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