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.
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