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.
-
\$\begingroup\$ I haven't done much with Matlab but the first thing I'd try is a terminal program (say Realterm if you don't have one) and see if that gives the same result to take Matlab out of the loop. The equivalent of 40 in ASCII is the ( character so you can just send that (and anything else at the moment) and see if the same comes back. \$\endgroup\$PeterJ– PeterJ2015年03月07日 07:07:17 +00:00Commented Mar 7, 2015 at 7:07
-
\$\begingroup\$ Can you post a little more code of the microcontroller? The way it stands, the uC will check for data and if there is no data available, it will do nothing of your tasks (it will just jump over it). Also, if the whole thing was inside a while loop, you do not want to initialize your UART again and again. What type of variable is setPoint. In your matlab: What type of variables are setup and setpoint. \$\endgroup\$Tom L.– Tom L.2015年03月07日 07:47:01 +00:00Commented Mar 7, 2015 at 7:47
-
\$\begingroup\$ i have uploaded my code above . In matlab i have created three buttons performing different functions and the problem lies in the refresh button. \$\endgroup\$dcmotor– dcmotor2015年03月07日 08:43:31 +00:00Commented Mar 7, 2015 at 8:43
-
\$\begingroup\$ In my trial and error i noticed whether i send 40 or any other number my output is either 75 or 90.Why it is so????????? \$\endgroup\$dcmotor– dcmotor2015年03月07日 10:27:26 +00:00Commented Mar 7, 2015 at 10:27
-
\$\begingroup\$ You will need to segment that problem further. I would say that your microcontroller code will not work that way. For the matlab part I have no idea. I will write an answer and detail what you should do \$\endgroup\$Tom L.– Tom L.2015年03月07日 11:44:21 +00:00Commented Mar 7, 2015 at 11:44
1 Answer 1
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?)
-
\$\begingroup\$ Actually in my start button i was transmitting a lot of data from pic to matlab .when stop button was pressed , matlab stop taking the input but data was still transmitted to matlab from pic . So my input buffer was filling up.Now when i pressed refresh button to read the value entered in edit text ,what it did was that it displayed the values stored in the input buffer thats why for data=40 it gave me like 75 or 90 whatever was stored in my input buffer.I got my answer anyways thank you for your reply. \$\endgroup\$dcmotor– dcmotor2015年03月07日 12:20:30 +00:00Commented Mar 7, 2015 at 12:20