Read and write error in matlab

MATLABpic

I am trying to send a data=40 from matlab gui to pic16f877A via serial communication.
To crosscheck value is properly transmitted to pic i also transmitted back that data from pic to matlab and displayed that data in edit text in matlab gui.
I noticed that edittext value is 75.
the data received was transmitted as it is without any manipulation then why does the value changed.
can anyone help me . Is there any problem in my typecasting?

mikroc code:

 unsigned int setPoint;
 char start;

main function

 void main() {
 UART1_Init(9600);
 setPoint=100;       // initially setpoint is set to 100
 . 
 .
 .

    while(1){
  if (UART1_Data_Ready()){
       start = UART1_Read();

    }
 if(start=='s'){             // if start button is pressed

  do something....
 }
else if(start=='g'){          // if stop button is pressed
   do something....
}

else if(start=='r'){         // if refresh button is pressed
 UART1_Write(1);
 refresh=1;

 while(refresh==1){
 if(UART1_Data_Ready()){
 setPoint=UART1_Read();      // reads data from matlab

  refresh=0;
   UART1_Write(setPoint);      //transmits back to matlab 
 }
 }
 }
 }
 }

Matlab code:

executed when start button is pressed

   function start_Callback(hObject, eventdata, handles)  // start button in matlab

    s = serial('COM2');
    set(s,'BaudRate',9600);
    set(s,'Timeout',10);
    set(s,'ReadAsyncMode','continuous');
    fopen(s);
    handles=guidata(hObject);
    handles.set=s;                         //passed serial comuunication object(s),so that it can be accessed in other callbacks
    guidata(hObject,handles);
    fprintf(s,'%c','s');                // send 's' to mikroc
    i=1;

     do something here....

executed when stop button is pressed

     function stop_Callback(hObject, eventdata, handles)  //stop button in matlab

     setup=handles.set;              //setup is serial communication object(setup=s)
     fprintf(setup,'%c','g');      //send 'g' to mikroc

     do something here.....

executed when refresh button is pressed (problem lies in refresh button)

     function Refresh_Callback(hObject, eventdata, handles) //refresh button in matlab

     setup=handles.set;
     fprintf(setup,'%c','r');   // send r to mikroc
     refresh=0;
     while(refresh==0)
     if(setup.BytesAvailable>0)
     refresh = fread(setup,1,'uint8');
      end
      end
      setpoint=str2num(get(handles.edit1,'String'));  // take data from edit box (data =40)
       fwrite(setup,setpoint);     //writes data(40) to pic

       setp=fread(setup,1,'uint8');    // receive data from pic
       string1 = sprintf('set = %i', setp);   //display in edit text
       set(handles.edit5, 'String', string1);
       fclose(setup)
       delete(setup)

The output displayed in edit text is 75 how 40 changed to 75. I am new to matlab gui.

Best Answer

You need to segment that problem a little.

Step 1) Check your uC Code

  • Attach a terminal tool (such as PuTTY) instead of Matlab to your uC; also show us your physical setup. You are aware that you MUST NOT directly connect the uC to your PC, right (there needs to be some level translating device in between)? Use your settings for setting up PuTTY (probably 9600 baud, 8 data, 1 stop bits, no parity, no flow control)
  • Use PuTTY to send characters and check what the uC is actually doing (you can actually debug the uC, correct?)
  • Make a very simply program that just does the following

Example code:

char c;
UART1_Init(9600);
  while(1){
    if (UART1_Data_Ready()){
      c = UART1_Read();
      UART1_Write(c);
      UART1_Write(c);
      UART1_Write(c);
    }
  }  

I should echo each character you send three times back (why three? So we know it's not the echo of your terminal program). You need to get this to work, otherwise there is no use in using Matlab, it just complicates your problem.

If you want it to be even simpler, try the following

char c = 'z';
UART1_Init(9600);
while(1){
  UART1_Write(c);
}

This will just send the character 'z' over and over again, you should see a long line of 'z's in your terminal program.

Step 2) Correct the Matlab Code

Once you have the setup from above running and working, you should now work on your matlab code. Chances are high it will work right away, but for that you might need someone else.

Debugging Info) If you encounter strange characters there are many potential issues. Just to name a few

  • Wrong Baudrate or generally wrong settings for your serial interface
  • Microcontroller was configured wrong for that baudrate (do you need to setup any clocks first?)