1
\$\begingroup\$

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;
}
\$\endgroup\$
9
  • \$\begingroup\$ Is it repeatable gibberish , if so what is it? Is there a good system ground? \$\endgroup\$ Jun 17 '18 at 18:21
  • \$\begingroup\$ 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. \$\endgroup\$
    – A.F.
    Jun 17 '18 at 18:36
  • \$\begingroup\$ 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 \$\endgroup\$ Jun 17 '18 at 18:42
  • 3
    \$\begingroup\$ 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). \$\endgroup\$ Jun 17 '18 at 19:59
  • 1
    \$\begingroup\$ electronics.stackexchange.com/questions/17562/… \$\endgroup\$ Jun 17 '18 at 20:32
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

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

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.