[SOLVED] problem with UART RECEPTION: using dsPIC30F2020, matlab, FT232 & c30 compiler

Status
Not open for further replies.

maxwell_30

Newbie level 6
Joined
Mar 11, 2015
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
115
Hi guys,

i am trying to write a small program to test serial communication between dsPIC30F2020 and MATLAB via FT232.

As per the program, a random number is sent from matlab to the PIC. On reception of the number via UART, an interrupt is generated and an LED connected to pin B0 should glow.
But, am unable to do so. can someone please tell me where am i going wrong?

my code for the PIC is:

Code:
#include<p30f2020.h>
#include<libpic30.h>
#include"delay.h"

_FOSC(CSW_FSCM_OFF & OSC2_IO & HS );
_FOSCSEL(PRIOSC_PLL);
_FWDT(FWDTEN_OFF);
_FGS(CODE_PROT_OFF);
_FPOR(PWRT_OFF);

int rx_flag, tx_flag;

//UART Initialize and rs232 routines 115200 baud

// Function prototypes
void _ISR _U1RXInterrupt(void);
void _ISR _U1TXInterrupt(void);
void initUart(void);

void initUart(void)
{
    	U1MODEbits.UARTEN=  0;    //UART1 Disabled
	U1MODEbits.ALTIO =  1;   // Use alternate IO
	U1MODEbits.BRGH =   0;    //Use High Bit rate
	U1MODEbits.PDSEL =  0;;    //8 bit no parity
        U1MODEbits.STSEL =  0;    //One stop bit
    
        IPC2bits.U1RXIP =5;//Set UART1 RX Int Priority 
	IPC2bits.U1TXIP =4;//TX Priority 
	IFS0bits.U1RXIF =0; //Set UART1 RX Int Flag=0
    
    
        U1STAbits.UTXISEL0 = 0; 
        U1STAbits.UTXISEL1 = 0; 
	U1STAbits.UTXBRK  = 0; 
	U1STAbits.URXISEL = 0;
    
       //U1BRG = (FCY/16*Baud Rate)-1 WITH BRG
       //FCY = FCY = Primary Osc X 2 WITH PLL 
       U1BRG = 16; // 115200 baud  16 MHz Crystal BRGL
    
	U1MODEbits.UARTEN=  1;    //UART1 Enabled
      
        IEC0bits.U1RXIE = 1;//Enable UART1 Receiver Interrupt
 	IEC0bits.U1TXIE = 1;//Enable UART1 Tx Interrut
	
	U1STAbits.UTXEN   = 1;   // Enable transmit
    
} 

void __attribute__((__interrupt__, __auto_psv__)) _U1RXInterrupt(void)
{  
    rx_flag=1;
    _U1RXIF=0; //clear RX Interrupt Flag
}

void __attribute__((__interrupt__, __auto_psv__)) _U1TXInterrupt(void)
{
	tx_flag=1;
	_U1TXIF=0; //clear TX Interrupt Flag
}

int main()
{
 _TRISB0 = 0;
 delay_us(100);
 while(1)    
 {
     if(rx_flag==1)
     {
         _LATB0=1;
     };
 }
 
 return(0);
}

my matlab code is:

Code:
clc
clear all

s = serial('COM3');
set(s,'BaudRate',115200);
fopen(s);

% ENTER PIC RELATED CODE HERE
a= 001;
fwrite(s,a,'int8','async');
x=1;
k=fread(s);
disp(k);

pause(5);
fclose(s)
delete(s)

and the connection between FT232 and PIC are as follows:
 

re: problem with UART RECEPTION: using dsPIC30F2020, matlab, FT232 & c30 compiler

The first thing is that you are not calling the 'initUart' function from 'main'.
Which compiler are you using?
Can you use the debugger to step into the code to make sure that it is running at all. Also you are not reading the character form the Rx buffer - for some devices and in some circumstances (not sure if this is the case for you but...) this will cause a 'receive buffer overflow' that can stop further reception. You need to handle reception and errors correctly.
If a variable is altered in an ISR then it should be declared as volatile (and there are other cases where volatile should be used - I suggest you read up on those sometime).
Why are you connecting the \MCLR\ to the DTR line via a capacitor! At best this is useless but more likely it has the potential to be resetting your MCU at totally unexpected times.
At the early stages of learning and development I would suggest that you do NOT use interrupts - they add another layer of complexity that is not required. Put a test into the main loop for when the URXDA bit is set and then read a character from the Rx buffer. This is easier to debug as well.
Can you put a scope on the line from the FT232 to the MCU to make sure that it is sending a character?
Also can you transmit characters from the UART (0x55 or 0xaa are good choices as they give you a reasonable square-wave signal) and use a scope to make sure that the BAUD rate is correct?
Are you sure you have the oscillator set up correctly? What is the crystal frequency?
With some compilers you need to tell the 'delay' functions what the oscillator/instruction frequency is - often with a #define of the FCY symbol or similar. Do you need to do that to get an accurate delay?
Susan
 
re: problem with UART RECEPTION: using dsPIC30F2020, matlab, FT232 & c30 compiler

thanks a lot Susan! the first line itself helped me out..completely forgot to call initUart. My code is working now
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…