Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

matlab and pic

Status
Not open for further replies.
I have been trying to get uart working with MATLAB, but I only get limited access to it, and no luck so far. How are you wanting to connect the pic to it?
A little more detail might help, size of array ETC. Otherwise it looks like your just being lazy, it sort of sounds like " can somebody pop over to my house and make this for me".
Sorry I seem to be ratty today
 
Code:
ser=serial('COMxx'); % Defining the specified COM Port to be used
fopen(ser); % starting serial Communication,opening serial port
fprintf(ser,'Hello world');
Does this work? Does something appear on COM port? this is a snippet from a fairly old code.
 
I dont have access for a couple of days but will try it thanks. I like matlab but its been very hard to learn! I would like to get my hands on the control tool box lol, but the laptop I borrow dosnt have that one on it :(.
Apparently there is a open source clone but I have no idea if it has the tool box's as well
 
well my baudrate is 9600;
i got many programs which writes data t o pic . but i need to take in an array of 1024 numbers continuously. well my aim is to obtain a real time plot of sampled signal
 
you dont say how large the numbers are but lets assume a BYTE 1024 bytes is 8192 bits, add stop and parity is 10,240 bits.
so you will sampling fairly slowly? allow for this and that around 1.5 sec per sample. can you up the baud rate? and what size are the numbers? 0-?
 
i just want to plot the graph. dropped the plan of doing it real time. but i cant get the array of data . just need the code fragment to read an array of data. i am just a beginner in matlab. dont even know the basics. this matlab program is just for error checking and testing
 
I dont know, but surely there is plenty info in the help file or on the matlab forum? Put a support ticket in
 
Yeah, matlab itself offer a lot of help. Look for demosa and on the main window type help and the keyword.
 
SerPIC = serial('COM1'); %<--change this appropriately
set(SerPIC,'BaudRate', 9600, 'DataBits', 8, 'Parity', 'none','StopBits', 1, 'FlowControl', 'none');
fopen(SerPIC); %--open the serial port to the PIC

% fprintf(SerPIC, '%s', '003');%--send a _three_ digit string to the PIC with no terminator ('%s')

int8y = fread (SerPIC,'003','int8'); %-- 003 = 00000011 in binary, so first two LEDs of PIC will light
t = 1:10;
plot (y);

fclose(SerPIC); %--close the serial port when done
delete(SerPIC);
clear SerPIC


this is the code i have entered . it keeps on returning the error, that fread should have a double value . but if i type
y = fread(SerPIC,'003')
y is not filled with any value - i checked it in the variable editor.
my actual problem i guess is to intake a 'double' array so that i can plot a graph . please help me with the input part . i guess i can do the plotting and other things
 
int8y = fread (SerPIC,'003','int8'); %-- 003 = 00000011 in binary, so first two LEDs of PIC will light

How are leds going to light if you read data from serial port?
Should the second argument be a string '003' or a decimal number 3? The documentation says that it should be integer. (number of int8 values you read)

The documentation also says that the returned output is always "double" by default.. so if you read int8 values, they are converted to double by default.
I think you want to do something like:

int8y = fread(SerPIC, 10, 'int8=>int8'); % Read 10 int8 values and return them as an array of int8 values

If I tried to help you further, I would just be copy-pasting what it says in the documentation, so go read it yourself.
https://www.mathworks.se/help/instrument/serial-port-interface.html

int8y = fread (SerPIC,'003','int8'); %-- 003 = 00000011 in binary, so first two LEDs of PIC will light
t = 1:10;
plot(y);

Are you trying to plot y against t? Where does this variable y come from? And should you plot it using:
plot(t,y);

Your code is a total mess. Start from scratch.. read the documentation and think what every line of code you write does.

Could you at least describe what kind of data are you trying to receive? Is it continuous data.. what format.. how many values etc.?
 
Last edited:
this is the code i have entered . it keeps on returning the error, that fread should have a double value . but if i type
y = fread(SerPIC,'003')
y is not filled with any value - i checked it in the variable editor.
my actual problem i guess is to intake a 'double' array so that i can plot a graph . please help me with the input part . i guess i can do the plotting and other things
 
What?
 
its continuous data . might be as blocks of 128 bytes .data to be received is a sampled time domain signal . sampling is done by adc of pic. sorry for the last thread.that was a mistake
 
Did the previous posts help you to modify your code? Any improvement?
 
well i left the whole thing to start fresh . my aim was to build a power spectrum analyser . now i'm doing the fft using pic itself . there are some errors but i think i can get it right if i spend some more time


thank you for helping me with the fft code . u replied to my other thread and i got a break . my project is approaching its deadline . so i decided to do it on some thing i know better and chose pic. but i will surely look into the matlab codes as soon as my project is finished . can u afford to help me on that
 
Status
Not open for further replies.

Latest threads

Back
Top