Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] problem with uart TRANSMISSION (using dsPIC30F2020, matlab and ft232)

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 everyone,
i have written a small code to transmit 5 numbers from matlab to the PIC via FT232.
Depending on the number received, a particular led blinks. When the number 5 is received, the MUC transmits back all the 5 numbers received till then to the pc @ 115200 bps.

For some reason my UART transmission is not working and am getting more than 300 random values in matlab when am expecting only 5 values (though the UART reception is working and the leds are blinking as per the number received).

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

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

char Buf[10];
char * Receiveddata = Buf;

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

void __attribute__((__interrupt__, __auto_psv__)) _U1RXInterrupt(void)
{    
    x = U1RXREG & 0x0F;
    _U1RXIF=0;              //clear RX Interrupt Flag
    *(Receiveddata) = x;
    Receiveddata++;
}

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

}


 void putHex() 
 { int i=0;
     for(;i<5;i++)
     {
     while(U1STAbits.UTXBF==1) continue;  //Checks if Buffer is Full and w8ts for it to have one location empty 
		U1TXREG=Buf[i];
     }
    return;     //Loops till Transmission is over 
}  

int main()
{
 _TRISB0 = 0;
 _TRISB1 = 0;
 _TRISB2 = 0;
 _TRISB3 = 0;
 
 delay_us(100);
 initUart();
 while(1)    
 {
     if(x==1)
     {  
         _LATB0=1;
         delay_ms(5000);
         _LATB0=0;
         delay_ms(5000);
     }
     else if(x==2)
     {
        _LATB1=1;
         delay_ms(5000);
         _LATB1=0;
         delay_ms(5000);
     }
     else if(x==3)
     {
        _LATB2=1;
         delay_ms(5000);
         _LATB2=0;
         delay_ms(5000);
     }
     else if(x==4)
     {
        _LATB3=1;
         delay_ms(5000);
         _LATB3=0;
         delay_ms(5000);
     }
     else if(x==5)
     {
         putHex();
         while(!U1STAbits.TRMT) continue;
     }
 } 
     
 return(0);
}

can somebody please tell me what is the problem in my code?

my matlab code is:

Code:
k=0;
s = serial('COM3');
set(s,'BaudRate',115200);
fopen(s);

% ENTER PIC RELATED CODE HERE
for i=1:5
    b=input('Enter');
    fwrite(s,b,'int8','async');
end

while k ~= 5
    k=fread(s);
    disp(k);
end

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

I suggest that you go back to my reply to your similar question about reception with the UART - especially the part where I talk about ensuring the PIC frequency is correct as that is the common cause for receiving more characters than expected in the time.
Also check the part where I talk about using a scope on the line to check the BAUD rate.
Susan
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top