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;
}