maxwell_30
Newbie level 6
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:
my matlab code is:
and the connection between FT232 and PIC are as follows:
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: