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