Maker Pro
Maker Pro

Serial Communication with dsPIC30F4011

Hi..I've been trying to get my serial communication on the dsPIC to work. I need to be able to transmit and receive at least one character but all I seem to get is an junk transmission. I'm not sure if my code is right. I've tried a max232 circuit and an off the shelf usb-to-serial bridge. Both give the same output. I've also made sure the baud rates are correct. Please help.

Here's my code:
//------------------------------------------- Defines
#include <p30f4011.h>
#include <xc.h>
#include <libpic30.h>
#include <ports.h>
#include <dsp.h>
#include <math.h>

#define FCY (unsigned long) 30000000 //add your operating frequency
#define UART1_BAUD 9600
#define UART2_BAUD 9600
#define UBRG1_VALUE (FCY/UART1_BAUD)/16 - 1
#define UBRG2_VALUE (FCY/UART2_BAUD)/16 - 1
//------------------------------------------ Config bits
// Configuration settings
_FOSC(CSW_FSCM_OFF && FRC_PLL16); // Fosc=16x7.5MHz, i.e. 30 MIPS
// 16 x 7.5=120 MHz
//120/4=30MHz
//1/30M*x=delay
_FWDT(WDT_OFF); // Watchdog timer off
_FBORPOR(MCLR_EN); // Disable reset pin
int main(void)
{
unsigned char MSG[]= "o";

OpenUART1();
while (1)
{
PrintStringUART1(MSG);
WriteCharToUART1(11);
// WriteCharToUART1(13);
}
return 0;
}

void OpenUART1(void)
{
TRISFbits.TRISF2 = 1;
TRISFbits.TRISF3 = 0;

U1MODEbits.ALTIO = 0;
U1MODEbits.PDSEL = 0; // 8 bits no parity
U1MODEbits.STSEL = 0; //1 stop bit
U1BRG = 194; //calculated according to baud rate 9600

U1MODEbits.UARTEN = 1; //enable tx rx pins
U1STAbits.UTXEN = 1; // enable transmit
}

char BusyUART1 (void)
{
if(!IFS0bits.U1TXIF)
return 1;
else
{
IFS0bits.U1TXIF =0;
return 0;
}
}

void PrintStringUART1 (unsigned char *String)
{
while (*String)
{
WriteCharToUART1 (*String++);
}
}
void WriteCharToUART1 (unsigned char Data)
{
U1TXREG = Data;
while (BusyUART1()); __delay32(15000000);
}
 

Harald Kapp

Moderator
Moderator
Hi Pat, welcome to EP.

Please use the 'Insert... Code' button next time to make your post better readable.

Harald
 
The oscillator is 7.37, not 7.5 MHz. So the baud should be 191 instead of 194. Not a big difference, but who knows, combined with the oscillator error of 2%, might be enough to screw up.
 
In a while loop
Code:
while (1)
{
PrintStringUART1(MSG); //you are increasing index in a while loop. This function is a bit fishy
WriteCharToUART1(11);
}

and your array unsigned char MSG[]= "o"; is of a size 1 and

in the function PrintStringUART1() you are increasing you array index which I think spitting out the garbage because its not initialize.


try this:
char MSG[]= "Hello World";

when you send a message, count the byte
while(length of a string)
{
U1TXREG = MSG[i++];
wait for PIC to send a char
}
 
Top