Conect Matlab with MBED FRDM-KL25Z serial

Dependencies:   FastAnalogIn mbed

Fork of Matlab2013serialKL25Z by oscar rodriguez

Committer:
oscargrodri
Date:
Sun Mar 09 02:15:31 2014 +0000
Revision:
0:cdcf435b31f9
Child:
2:7942a82c8561
Connecting Matlab 2013 with MBED FRDM-KL25Z Serial Include script

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oscargrodri 0:cdcf435b31f9 1 #include "mbed.h"
oscargrodri 0:cdcf435b31f9 2 //FRDM-KL25Z CONECTED TO MATLAB 2013//
oscargrodri 0:cdcf435b31f9 3 // Copyright (c) 2014 Oscar Rodriguez-- Oscargrodri@hotmail.com
oscargrodri 0:cdcf435b31f9 4 /*
oscargrodri 0:cdcf435b31f9 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
oscargrodri 0:cdcf435b31f9 6 * of this software and associated documentation files (the "Software"), to deal
oscargrodri 0:cdcf435b31f9 7 * in the Software without restriction, including without limitation the rights
oscargrodri 0:cdcf435b31f9 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
oscargrodri 0:cdcf435b31f9 9 * copies of the Software, and to permit persons to whom the Software is
oscargrodri 0:cdcf435b31f9 10 * furnished to do so, subject to the following conditions:
oscargrodri 0:cdcf435b31f9 11 *
oscargrodri 0:cdcf435b31f9 12 * The above copyright notice and this permission notice shall be included in
oscargrodri 0:cdcf435b31f9 13 * all copies or substantial portions of the Software.
oscargrodri 0:cdcf435b31f9 14 *
oscargrodri 0:cdcf435b31f9 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
oscargrodri 0:cdcf435b31f9 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
oscargrodri 0:cdcf435b31f9 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
oscargrodri 0:cdcf435b31f9 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
oscargrodri 0:cdcf435b31f9 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
oscargrodri 0:cdcf435b31f9 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
oscargrodri 0:cdcf435b31f9 21 * THE SOFTWARE.
oscargrodri 0:cdcf435b31f9 22 */
oscargrodri 0:cdcf435b31f9 23
oscargrodri 0:cdcf435b31f9 24 Serial pc(USBTX, USBRX);
oscargrodri 0:cdcf435b31f9 25
oscargrodri 0:cdcf435b31f9 26 AnalogIn ain(PTB0);
oscargrodri 0:cdcf435b31f9 27
oscargrodri 0:cdcf435b31f9 28 int main()
oscargrodri 0:cdcf435b31f9 29 {
oscargrodri 0:cdcf435b31f9 30 while(true) {
oscargrodri 0:cdcf435b31f9 31 pc.printf(" %f ",ain.read()*5);
oscargrodri 0:cdcf435b31f9 32 wait(.1);
oscargrodri 0:cdcf435b31f9 33 }
oscargrodri 0:cdcf435b31f9 34 }
oscargrodri 0:cdcf435b31f9 35
oscargrodri 0:cdcf435b31f9 36 /* This is the Matlab Script just copy ir as is only change the Com number
oscargrodri 0:cdcf435b31f9 37
oscargrodri 0:cdcf435b31f9 38 delete(instrfind({'Port'},{'COM8'})); %ajustar puerto serie!
oscargrodri 0:cdcf435b31f9 39
oscargrodri 0:cdcf435b31f9 40 clear
oscargrodri 0:cdcf435b31f9 41 clc
oscargrodri 0:cdcf435b31f9 42
oscargrodri 0:cdcf435b31f9 43 %User Defined Properties
oscargrodri 0:cdcf435b31f9 44 serialPort = 'COM8'; % define COM port #
oscargrodri 0:cdcf435b31f9 45 plotTitle = 'Serial Data Log'; % plot title
oscargrodri 0:cdcf435b31f9 46 xLabel = 'Elapsed Time (s)'; % x-axis label
oscargrodri 0:cdcf435b31f9 47 yLabel = 'Data'; % y-axis label
oscargrodri 0:cdcf435b31f9 48 plotGrid = 'on'; % 'off' to turn off grid
oscargrodri 0:cdcf435b31f9 49 min = -1.5; % set y-min
oscargrodri 0:cdcf435b31f9 50 max = 6; % set y-max
oscargrodri 0:cdcf435b31f9 51 scrollWidth = 10; % display period in plot, plot entire data log if <= 0
oscargrodri 0:cdcf435b31f9 52 delay = 0.001; % make sure sample faster than resolution
oscargrodri 0:cdcf435b31f9 53
oscargrodri 0:cdcf435b31f9 54 %Define Function Variables
oscargrodri 0:cdcf435b31f9 55 time = 0;
oscargrodri 0:cdcf435b31f9 56 data = 0;
oscargrodri 0:cdcf435b31f9 57 count = 0;
oscargrodri 0:cdcf435b31f9 58
oscargrodri 0:cdcf435b31f9 59 %Set up Plot
oscargrodri 0:cdcf435b31f9 60 plotGraph = plot(time,data,'-mo',...
oscargrodri 0:cdcf435b31f9 61 'LineWidth',1,...
oscargrodri 0:cdcf435b31f9 62 'MarkerEdgeColor','k',...
oscargrodri 0:cdcf435b31f9 63 'MarkerFaceColor',[.49 1 .63],...
oscargrodri 0:cdcf435b31f9 64 'MarkerSize',2);
oscargrodri 0:cdcf435b31f9 65
oscargrodri 0:cdcf435b31f9 66 title(plotTitle,'FontSize',25);
oscargrodri 0:cdcf435b31f9 67 xlabel(xLabel,'FontSize',15);
oscargrodri 0:cdcf435b31f9 68 ylabel(yLabel,'FontSize',15);
oscargrodri 0:cdcf435b31f9 69 axis([0 10 min max]);
oscargrodri 0:cdcf435b31f9 70 grid(plotGrid);
oscargrodri 0:cdcf435b31f9 71
oscargrodri 0:cdcf435b31f9 72 %Open Serial COM Port
oscargrodri 0:cdcf435b31f9 73 s = serial(serialPort);
oscargrodri 0:cdcf435b31f9 74 set(s,'BaudRate',9600,'Terminator','CR','Parity','None');
oscargrodri 0:cdcf435b31f9 75 disp('Close Plot to End Session');
oscargrodri 0:cdcf435b31f9 76 fopen(s);
oscargrodri 0:cdcf435b31f9 77 tic
oscargrodri 0:cdcf435b31f9 78
oscargrodri 0:cdcf435b31f9 79 while ishandle(plotGraph) %Loop when Plot is Active
oscargrodri 0:cdcf435b31f9 80 dat = fscanf(s,'%f',10) %Read Data from Serial as Float
oscargrodri 0:cdcf435b31f9 81 % pause(0.1);
oscargrodri 0:cdcf435b31f9 82 if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct
oscargrodri 0:cdcf435b31f9 83 count = count + 1;
oscargrodri 0:cdcf435b31f9 84 time(count) = toc; %Extract Elapsed Time
oscargrodri 0:cdcf435b31f9 85 data(count) = dat(1); %Extract 1st Data Element
oscargrodri 0:cdcf435b31f9 86 testx(count)=time(count);
oscargrodri 0:cdcf435b31f9 87 testy(count)=data(count);
oscargrodri 0:cdcf435b31f9 88 %Set Axis according to Scroll Width
oscargrodri 0:cdcf435b31f9 89 if(scrollWidth > 0)
oscargrodri 0:cdcf435b31f9 90 set(plotGraph,'XData',time(time > time(count)-scrollWidth),'YData',data(time > time(count)-scrollWidth));
oscargrodri 0:cdcf435b31f9 91 axis([time(count)-scrollWidth time(count) min max]);
oscargrodri 0:cdcf435b31f9 92
oscargrodri 0:cdcf435b31f9 93 else
oscargrodri 0:cdcf435b31f9 94 set(plotGraph,'XData',time,'YData',data);
oscargrodri 0:cdcf435b31f9 95 axis([0 time(count) min max]);
oscargrodri 0:cdcf435b31f9 96
oscargrodri 0:cdcf435b31f9 97 end
oscargrodri 0:cdcf435b31f9 98 %Allow MATLAB to Update Plot
oscargrodri 0:cdcf435b31f9 99 pause(delay);
oscargrodri 0:cdcf435b31f9 100
oscargrodri 0:cdcf435b31f9 101 end
oscargrodri 0:cdcf435b31f9 102 end
oscargrodri 0:cdcf435b31f9 103
oscargrodri 0:cdcf435b31f9 104 %Close Serial COM Port and Delete useless Variables
oscargrodri 0:cdcf435b31f9 105 fclose(s);
oscargrodri 0:cdcf435b31f9 106 clear count dat delay max min plotGraph plotGrid plotTitle s ...
oscargrodri 0:cdcf435b31f9 107 scrollWidth serialPort xLabel yLabel data time;
oscargrodri 0:cdcf435b31f9 108
oscargrodri 0:cdcf435b31f9 109
oscargrodri 0:cdcf435b31f9 110 disp('Session Terminated...');
oscargrodri 0:cdcf435b31f9 111 delete(instrfind({'Port'},{'COM8'})); %ajustar puerto serie!
oscargrodri 0:cdcf435b31f9 112 plot (testx,testy)
oscargrodri 0:cdcf435b31f9 113 grid on;
oscargrodri 0:cdcf435b31f9 114 */
oscargrodri 0:cdcf435b31f9 115