0
\$\begingroup\$

I am working with a PIC16F1829. I have a UART interface that mostly works correctly. My issue is that whenever I type a character into CoolTerm, I get an echo of that character back or some other character. You see in the screenshot when I type "m" I get an "m" echoed back along with my data. Other times it's an "l" or some other character and then my data.

CoolTerm Screenshot

My question is, what is causing the echo back? It's not from CoolTerm.

I've tried so far: - Turning off any transmit on the PIC side. I still see this echo - Using Python to transmit and receive data, I still see this type of echo - Tried replacing the PIC - Tried different baud rates

This code was generated by the MCC generator


void EUSART_Initialize(void) { // Set the EUSART module to the options selected in the user interface.

// ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled; 
BAUDCON = 0x08;

// SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled; 
RCSTA = 0x90;

// TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave; 
TXSTA = 0x24;


// Baud Rate = 115200; SPBRGL 34; 
SPBRGL = 0x22;

// Baud Rate = 115200; SPBRGH 0; 
SPBRGH = 0x00;

}

uint8_t EUSART_Read(void) {

while(!PIR1bits.RCIF)
{
}

if(1 == RCSTAbits.FERR)
{
    char a = RCREG; //Read a byte to clear FERR
}
if(1 == RCSTAbits.OERR)
{
    // EUSART error - restart

    RCSTAbits.CREN = 0; 
    RCSTAbits.CREN = 1; 
}

return RCREG;

}

void EUSART_Write(uint8_t txData) {
while(0 == PIR1bits.TXIF) { }

TXREG = txData;    // Write the data byte to the USART.

} /** End of File */ void write_uart_str(char *str) { while (*str != '\0') { EUSART_Write(*str); (char *) str++; } } char *read_uart_str() { #define MAX_UART_SIZE 32 int i=0; char ch; char str[MAX_UART_SIZE];

for(i=0;i<MAX_UART_SIZE;i++)
{
    str[i]='\0';
}

do
{
    ch = EUSART_Read();
    str[i]=ch;
    i++;
}while (ch != '\0');

return (char *)str;

} char read_uart_ch() { return EUSART_Read(); }

void main(void) { // initialize the device SYSTEM_Initialize();

// When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits // Use the following macros to: // Enable the Global Interrupts INTERRUPT_GlobalInterruptEnable(); // Enable the Peripheral Interrupts INTERRUPT_PeripheralInterruptEnable(); // Disable the Global Interrupts //INTERRUPT_GlobalInterruptDisable(); // Disable the Peripheral Interrupts //INTERRUPT_PeripheralInterruptDisable(); char ch=0; write_uart_str((char*) ("\nRunning Version: ")); write_uart_str(VERSION); write_uart_str("\n"); accelerometer_init(); write_uart_str("\nSelect Option\n"); write_uart_str(menu_options); while (1) { // Add your application code ch=0; while(!ch) ch = read_uart_ch(); //write_uart_str("Read: "); //write_uart_str(ch); //write_uart_str("\n"); switch (ch) { case 'c': { accelerometer_calibrate(); write_uart_str("Calibrated Accelerometer\n"); break; } case 'i': { int id = accelerometer_get_deviceID(); //Should be 0xE5 char str_buf[16]; write_uart_str("Device ID: "); write_uart_str(itoa(str_buf, id, 10)); write_uart_str("\n"); break; } case 'x': { struct accel_xyz xyz = accelerometer_read(); char str_buf[16]; write_uart_str("\nX: "); write_uart_str(itoa(str_buf, xyz.x, 10)); write_uart_str("\n"); break; } case 'y': { struct accel_xyz xyz = accelerometer_read(); char str_buf[16]; write_uart_str("\nY: "); write_uart_str(itoa(str_buf, xyz.y, 10)); write_uart_str("\n"); break; } case 'z': { struct accel_xyz xyz = accelerometer_read(); char str_buf[16]; write_uart_str("\nZ: "); write_uart_str(itoa(str_buf, xyz.z, 10)); write_uart_str("\n"); break; } case 'm': { struct accel_xyz xyz = accelerometer_read(); char str_buf[16]; xyz=accelerometer_read(); write_uart_str(itoa(str_buf, xyz.x, 10)); write_uart_str(" "); write_uart_str(itoa(str_buf, xyz.y, 10)); write_uart_str(" "); write_uart_str(itoa(str_buf, xyz.z, 10)); write_uart_str("\n"); break; } case 'r': { struct accel_xyz xyz = accelerometer_read(); char str_buf[16]; write_uart_str("\nPress p to pause\n"); write_uart_str("X Y Z\n"); do { xyz=accelerometer_read(); write_uart_str(itoa(str_buf, xyz.x, 10)); write_uart_str(" "); write_uart_str(itoa(str_buf, xyz.y, 10)); write_uart_str(" "); write_uart_str(itoa(str_buf, xyz.z, 10)); write_uart_str("\n"); }while (1); //read_uart_ch() != 'p' break; } } }

}

I am using this RS232 Level translation module for the driver: It's connected to TX/RX pins on the PIC. https://www.digikey.com/product-detail/en/seeed-technology-co-ltd/101990008/1597-1306-ND/5487765

\$\endgroup\$
  • \$\begingroup\$ How do you know it's not from CoolTerm? Can you see the local echo setting off? And how can you "turn off any transmit" if you are transmitting data? \$\endgroup\$ – Eugene Sh. Apr 25 '17 at 17:55
  • \$\begingroup\$ I suggest trying another terminal emulator, also include the code in the pic. \$\endgroup\$ – Vladimir Cravero Apr 25 '17 at 18:06
  • \$\begingroup\$ also, l an m have codes near the carriage return, maybe it has something to do with that \$\endgroup\$ – Vladimir Cravero Apr 25 '17 at 18:09
  • 1
    \$\begingroup\$ Looks like maybe some crosstalk at the hardware level. Can you show us your PIC's RS232 driver circuit and how you have it connected to the PC? \$\endgroup\$ – brhans Apr 25 '17 at 18:18
  • \$\begingroup\$ @Eugene Sh- It's not CoolTerm because the Echo setting is off. I also have a Python script that I wrote and it sees the same characters. \$\endgroup\$ – I Gr Apr 26 '17 at 19:01
-2
\$\begingroup\$

I think I have found the problem.

When you get wrong characters being sent back, it tends to be a mismatch in bitrate:

  • Your PIC code uses // Baud Rate = 115200; SPBRGL 34;
  • CoolTerm is also using 115200

However looking at the datasheet of the TTL level driver, it states:

• Operates up to 250 kbit/s

If you go back in to your code for the PIC (and CoolTerm), change the bitrate of the UART to 250 kbit/s, it should solve the problem.

Also would suggest to hook it up to an oscilloscope to see exactly what you are getting using a single shot trigger.

\$\endgroup\$
  • \$\begingroup\$ I tried lowering the baud rate to 300 and I still have the same issue. Though now I consistently get back the same character I typed in. I know it's not CoolTerm's echo feature because I can enable that and then the character will show up twice. \$\endgroup\$ – I Gr Apr 26 '17 at 23:07

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.