oscar rodriguez
/
Matlab2013serialKL25Z
Conect Matlab with MBED FRDM-KL25Z serial
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 //FRDM-KL25Z CONECTED TO MATLAB 2013// 00003 // Copyright (c) 2014 Oscar Rodriguez-- Oscargrodri@hotmail.com 00004 /* 00005 * Permission is hereby granted, free of charge, to any person obtaining a copy 00006 * of this software and associated documentation files (the "Software"), to deal 00007 * in the Software without restriction, including without limitation the rights 00008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 * copies of the Software, and to permit persons to whom the Software is 00010 * furnished to do so, subject to the following conditions: 00011 * 00012 * The above copyright notice and this permission notice shall be included in 00013 * all copies or substantial portions of the Software. 00014 * 00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 * THE SOFTWARE. 00022 */ 00023 00024 Serial pc(USBTX, USBRX); 00025 00026 AnalogIn ain(PTB0); 00027 00028 int main() 00029 { 00030 while(true) { 00031 pc.printf(" %f ",ain.read()*5); 00032 wait(.1); 00033 } 00034 } 00035 00036 /* This is the Matlab Script just copy ir as is only change the Com number 00037 00038 delete(instrfind({'Port'},{'COM8'})); %ajustar puerto serie! 00039 00040 clear 00041 clc 00042 00043 %User Defined Properties 00044 serialPort = 'COM8'; % define COM port # 00045 plotTitle = 'Serial Data Log'; % plot title 00046 xLabel = 'Elapsed Time (s)'; % x-axis label 00047 yLabel = 'Data'; % y-axis label 00048 plotGrid = 'on'; % 'off' to turn off grid 00049 min = -1.5; % set y-min 00050 max = 6; % set y-max 00051 scrollWidth = 10; % display period in plot, plot entire data log if <= 0 00052 delay = 0.001; % make sure sample faster than resolution 00053 00054 %Define Function Variables 00055 time = 0; 00056 data = 0; 00057 count = 0; 00058 00059 %Set up Plot 00060 plotGraph = plot(time,data,'-mo',... 00061 'LineWidth',1,... 00062 'MarkerEdgeColor','k',... 00063 'MarkerFaceColor',[.49 1 .63],... 00064 'MarkerSize',2); 00065 00066 title(plotTitle,'FontSize',25); 00067 xlabel(xLabel,'FontSize',15); 00068 ylabel(yLabel,'FontSize',15); 00069 axis([0 10 min max]); 00070 grid(plotGrid); 00071 00072 %Open Serial COM Port 00073 s = serial(serialPort); 00074 set(s,'BaudRate',9600,'Terminator','CR','Parity','None'); 00075 disp('Close Plot to End Session'); 00076 fopen(s); 00077 tic 00078 00079 while ishandle(plotGraph) %Loop when Plot is Active 00080 dat = fscanf(s,'%f',10) %Read Data from Serial as Float 00081 % pause(0.1); 00082 if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct 00083 count = count + 1; 00084 time(count) = toc; %Extract Elapsed Time 00085 data(count) = dat(1); %Extract 1st Data Element 00086 testx(count)=time(count); 00087 testy(count)=data(count); 00088 %Set Axis according to Scroll Width 00089 if(scrollWidth > 0) 00090 set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data(time > time(count)-scrollWidth)); 00091 axis([time(count)-scrollWidth time(count) min max]); 00092 00093 else 00094 set(plotGraph,'XData',time,'YData',data); 00095 axis([0 time(count) min max]); 00096 00097 end 00098 %Allow MATLAB to Update Plot 00099 pause(delay); 00100 00101 end 00102 end 00103 00104 %Close Serial COM Port and Delete useless Variables 00105 fclose(s); 00106 clear count dat delay max min plotGraph plotGrid plotTitle s ... 00107 scrollWidth serialPort xLabel yLabel data time; 00108 00109 00110 disp('Session Terminated...'); 00111 delete(instrfind({'Port'},{'COM8'})); %ajustar puerto serie! 00112 plot (testx,testy) 00113 grid on; 00114 */ 00115
Generated on Tue Jul 12 2022 22:57:26 by 1.7.2