maxwell_30
Newbie level 6
hi again everyone!
I am trying to interface a matlab gui with dspic30f2020 over serial communication. My matlab code sends a number (here 67) when i press a certain pushbutton. The muc is then supposed to check if the value received is 67 and if it is, then it has to take a certain action (defined in the if loop).
The problem is, my MUC is able to read the received value and to verify if it is 67 i am also sending the received value back to matlab just before the if loop...which also comes out to be 67. But, for some reason, my code is not entering the if loop inspite of the received value being 67..can someone please help me out..
part of my code for dspic30f2020 is:
matlab code:
I am trying to interface a matlab gui with dspic30f2020 over serial communication. My matlab code sends a number (here 67) when i press a certain pushbutton. The muc is then supposed to check if the value received is 67 and if it is, then it has to take a certain action (defined in the if loop).
The problem is, my MUC is able to read the received value and to verify if it is 67 i am also sending the received value back to matlab just before the if loop...which also comes out to be 67. But, for some reason, my code is not entering the if loop inspite of the received value being 67..can someone please help me out..
part of my code for dspic30f2020 is:
Code:
int RXflag=0;
int input_string[3];
void __attribute__((__interrupt__, __auto_psv__)) _U1TXInterrupt(void){
//TXflag=1;
_U1TXIF=0; //clear RX Interrupt Flag
}
void __attribute__((__interrupt__, __auto_psv__)) _U1RXInterrupt(void){
RXflag=1;
_LATE4= ~_LATE4;
_U1RXIF=0; //clear RX Interrupt Flag
}
void putValue(int x)
{
while(U1STAbits.UTXBF==1) continue; //Checks if Buffer is Full and w8ts for it to have one
location empty
U1TXREG=x;
return; //Loops till Transmission is over
}
int getValue(void){
while(RXflag==0) continue;//Loops till RX Flag=1 meaning one character has been recived
RXflag=0; //resets RX flag
return(U1RXREG);
}
void vref_read(void)
{
unsigned char AN2msb,AN2lsb,AN3msb,AN3lsb;
ADCPC0bits.SWTRG1 = 1; //start conversion of AN1 and AN2 store 200 samples each
while(ADCPC0bits.PEND1){} //conv pending becomes 0 when conv complete
AN2lsb = ADCBUF2 &0x00ff; // lsb of the ADC result
AN2msb = (ADCBUF2 &0xff00)>>8; // msb of the ADC result
AN3lsb = ADCBUF3 &0x00ff; // lsb of the ADC result
AN3msb = (ADCBUF3 &0xff00)>>8; // msb of the ADC result
putValue(AN2msb);putValue(AN2lsb);putValue(AN3msb);putValue(AN3lsb);
}
int main()
{
__delay32(3200);
//Delay();
init_ports();
adc_index=0;
initUart();
initialiseADC();
__delay32(3200);
_LATE4=0;
[B]
while(1) // part where i am having trouble
{
input_string[0]=getValue();
putValue(input_string[0]);
if(input_string[0] == 67)
{
_LATE4= ~ _LATE4;
vref_read();
}
}
[/B]
return(0);
}
matlab code:
Code:
clc;
s=serial(upper(get(handles.ip_port,'String')));
set(s,'BaudRate',115200,'InputBufferSize',1,'Timeout',10);
fopen(s);
s;
b=67;
k=0;
fwrite(s,b,'int8');
disp('done')
k=fread(s);
disp('k');
disp(k); //I am getting the output till here, after this i get timeout
error because no values are transmitted from the MUC
if k==67
AN1lsb=fread(s);
AN1msb=fread(s);
AN2lsb=fread(s);
AN2msb=fread(s);
end