1

Hello there i am using dsPIC30F4013 and i have been trying to interface uart with pic. I want to send some data to PC via uart and display it on terminal, but what i get is far from what i expect, to be exact i get nonsense. I use FTD232RL USB Serial Converter module to connect MCU with PC. I thought at first it could be baud rate difference but i double checked and even tried different baud rates but result is the same. I posted my code below. What could be a problem and how to fix it? Thanks in advance.

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <p30fxxxx.h>
#include "uart.h"

_FOSC(CSW_FSCM_OFF & XT_PLL4);
_FWDT(WDT_OFF);
_FBORPOR(MCLR_DIS);

#define period 7372

unsigned int counter;

void initTMR1(void)
{
 TMR1 = 0;
 PR1 = period;    
 IFS0bits.T1IF = 0;
 IEC0bits.T1IE = 1; 
 T1CONbits.TON = 1; 
}

void delay_ms(unsigned int time)
{
 counter = 0;
 while (counter < time);
}

void initUART()
{
 U1BRG = 0x0040; //baud rate 9600
 U1MODEbits.ALTIO = 1;
 IEC0bits.U1RXIE = 1;
 U1STA &= 0xfffc;
 U1MODEbits.UARTEN = 1;
 U1STAbits.UTXEN = 1;
}

void writeUART(unsigned int data)
{
 while(U1STAbits.TRMT == 0);
 if(U1MODEbits.PDSEL == 3)
     U1TXREG = data;
 else
     U1TXREG = data & 0xFF;
}

void RS232_putst(register const char *str)
{
  while((*str)!=0)
  {
    WriteUART(*str);
    str++;
  }
 }

void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void)
{
 TMR1 = 0;
 counter++;
 IFS0bits.T1IF = 0;
}


int main(void)
{
 ADPCFGbits.PCFG9 = 1;
 TRISBbits.TRISB9 = 0;
 LATBbits.LATB9 = 0;


 initUART();
 initTMR1();

 RS232_putst("START");
 delay_ms(2000);

 while(1)
 {
     RS232_putst(00000); 
     RS232_putst("This is test!!");
     LATBbits.LATB9 = ~LATBbits.LATB9;

     delay_ms(1000);
 }
 return 0;
}
A.F.
  • 33
  • 1
  • 7
  • Is it repeatable gibberish , if so what is it? Is there a good system ground? – Tony Stewart EE75 Jun 17 '18 at 18:21
  • Yes it is repeatable. I get something like this: ððððð<0>þ<0>Àð<0>Àð<0><0>þððð<0>ðð<0>>|ø<0>þð<0>ððð<0>þððð<0>ðð<0>>üð<0>>€<0>ððððð<0>>€<0>ðððð<0>ððð<0>>€<0>ðð<0><0>ððððð<0>þ<0>Àð<0>Àð<0><0>þððð<0>ðð<0>>|ø<0>þð<0>ððð<0>þððð<0>ðð<0>>üð<0>>€<0>ððððð<0>>€<0>ðððð<0>ððð<0>> , ant it just keep coming. Yes it is well grounded. – A.F. Jun 17 '18 at 18:36
  • I see repeating 8 but characters with msb=1 but no repeating string . The ratio of characters out/in might suggest a baud rate computation error with a slower output and msb overlap from start bit=1 – Tony Stewart EE75 Jun 17 '18 at 18:42
  • So what exactly you mean? You think i should try with slower baud rates? – A.F. Jun 17 '18 at 18:46
  • is parity and stop bits correct? Also, if you're sleeping the uC, the serial needs to be flushed before sleep. – Indraneel Jun 17 '18 at 19:42
  • 3
    Do you have a "USB to serial" converter with a 9-pin or some other consumer connector, or do you have a "Logic level USB UART" device with bare wires or a header connector? Typically he former will include an *inverting* level translator, while the latter will just have the USB interface chip. The level translator will usually accept low logic level signals, but the inversion ends up wrong and frequently results in problems like you see (also the transmit side of the level translator can damage your circuit). – Chris Stratton Jun 17 '18 at 19:59
  • Both modules use TTL which are inverted logic levels so Stop and Start bits =1 are logic “0” by checking Vdc idle and use scope or equivalent. – Tony Stewart EE75 Jun 17 '18 at 20:26
  • 1
    https://electronics.stackexchange.com/questions/17562/how-to-read-serial-data-from-oscilloscope – Tony Stewart EE75 Jun 17 '18 at 20:32
  • I have logic level usb connected to FTD232RL module with PC and from module i connect Rx and Tx with mcu pins respectively. – A.F. Jun 18 '18 at 09:47

2 Answers2

1

I figure it out, so here is a solution if someone ever come across this problem. The reason why i got nonsense on terminal is because of power supply not because of baud rate. All those modules draws certain amount of power, and they do not receive enough power to do their function and it happen to cause this kind of a problem. So in order to fix this you will have to use or make yourself a power supply with voltage regulator to ensure enough power for your circuit.

A.F.
  • 33
  • 1
  • 7
0

Do you have a scope? I do this setup repeatedly with PIC's and I always follow the same steps:

  1. start off at 9600
  2. set the PIC to send 0x55 in a loop
  3. use the scope to verify the bit period is close to 104us
  4. get the terminal settings right and check characters come through without problems.

If there is a problem, it will be that the bits are inverted, which is easily fixed in software. Once it all works, I usually push the baud rate up to something like 38400. Always goes flawlessly for me.

danmcb
  • 6,009
  • 14
  • 29
  • 3
    This is really a comment, not an answer, as you are providing debugging suggestions rather than able to identify or explain the problem. – Chris Stratton Jun 17 '18 at 23:00
  • No i don't have a scope. Let's assume that bits are inverted what should i change so i can fix that? – A.F. Jun 18 '18 at 09:42
  • just invert the byte in software right before you pass it to the UART - see if that fixes it. – danmcb Jun 18 '18 at 20:46