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 to pic serial communication

Status
Not open for further replies.

mankmanky1

New Member
Hi there

I am new to serial communication and I am trying to do a project where I can interface matlab with my pic18f4431 micro. I am having a problem setting up my pic to receive data serially as it fails to load properly via my bootloader program...

The code is below:

#include <htc.h>
#include<stdio.h>

void main(void)
{
ANSEL0=0b00000000;

TRISA = 0b00000000;
RCSTA=0b10010000;
TXSTA=0b00100000;
SPBRG=0x0F;

wait:
if(PIR1bits.RCIF==0)
{
goto wait;
}

PORTA=RCREG;
}

I was hoping this would program the pic to receive data serially...

My matlab on the other hand I think is ok. I have programmed matlab to send information serially via matlab to my pic using a vector that contains the bytes to be sent...
Do I have to send data via matlab in a vector or is there another way?

The matlab code is below:

b=[7 4];
s = serial('COM2');
set(s,'BaudRate',9600,'DataBits',8,'Parity','none','StopBits',1,'FlowControl','none','InputBufferSize',102400)
fopen(s);
s.Status

if strcmp(s.TransferStatus, 'idle') || strcmp(s.TransferStatus,'read')
fwrite(s,b,'uint8','async');

else

end

stopasync(s)
fclose(s)
delete(s)
clear s
display('finished')

Please can someone help me as I really need some direction

Thanks
 
hi man, first of all a question from my side and a very very important one:

'Did you make this code by converting a matlab/simulink file to PIc C or you only want to transfer it via serail com using matlab'

Now for your answer the only website that i came across having the theme 'Matlab with pic' uses the serial port via matlab. its matpic.com, its spanish so you might need to use google to translate it to english unless you can read spanish.

About the question. Matlabs/Simulinks Real time workshop does provide pic18 conversion. am working on it now but if you have done it then i want to ask some questions if you are happy to answer them for me?
 
  • Like
Reactions: 3v0
Hey thanks very much, will try and see if i can translate the website...
In terms of the question you asked, I made the matlab code simply wanting to transfer it via the serial com to the pic. The pic code was typed in mplab in the picc18 compiler.

I have this idea that if i can program the pic correctly to receive serial data then i can simply send data via the matlab code serially and the pic will pick up the data and understand that it is receiving bytes of information. Hence creating the interface.

If you see a problem with my code or know of a correction, please could you send it to me....

In terms of the pic18 conversion, I am not sure i understand your final question. Why would you need to do pic18 conversion in matlab? I would assume that if data is transmitted serially as bytes as can be done n matlab using vector form, then it doesnt actually need to be converted...
 
Hey thanks very much, will try and see if i can translate the website...
In terms of the question you asked, I made the matlab code simply wanting to transfer it via the serial com to the pic. The pic code was typed in mplab in the picc18 compiler.

I have this idea that if i can program the pic correctly to receive serial data then i can simply send data via the matlab code serially and the pic will pick up the data and understand that it is receiving bytes of information. Hence creating the interface.

If you see a problem with my code or know of a correction, please could you send it to me....

In terms of the pic18 conversion, I am not sure i understand your final question. Why would you need to do pic18 conversion in matlab? I would assume that if data is transmitted serially as bytes as can be done n matlab using vector form, then it doesnt actually need to be converted...

yeh u didnt understand the question correctly. What i intend to do( and thought that you had achieved) was to convert a simulink simulation directly to PIC C using the Real Time Workshop Encoder.As for the reason: Direct working of Matlab with PICs making it easy to design things like say a PID controller! i do have a book telling how to make pid controllers with PIC c.
 
I see, yeah i havent actually done that, i think that would be overkill for my project... i think there must be a simple way to create interaction with my pic and matlab.. my main problem is writing code for my pic18f4431 specifically that will tell it to receive data serially...
 
Hi there

I am new to serial communication and I am trying to do a project where I can interface matlab with my pic18f4431 micro. I am having a problem setting up my pic to receive data serially as it fails to load properly via my bootloader program...

The code is below:

#include <htc.h>
#include<stdio.h>

void main(void)
{
ANSEL0=0b00000000;

TRISA = 0b00000000;
RCSTA=0b10010000;
TXSTA=0b00100000;
SPBRG=0x0F;

wait:
if(PIR1bits.RCIF==0)
{
goto wait;
}

PORTA=RCREG;
}

I was hoping this would program the pic to receive data serially...

My matlab on the other hand I think is ok. I have programmed matlab to send information serially via matlab to my pic using a vector that contains the bytes to be sent...
Do I have to send data via matlab in a vector or is there another way?

The matlab code is below:

b=[7 4];
s = serial('COM2');
set(s,'BaudRate',9600,'DataBits',8,'Parity','none','StopBits',1,'FlowControl','none','InputBufferSize',102400)
fopen(s);
s.Status

if strcmp(s.TransferStatus, 'idle') || strcmp(s.TransferStatus,'read')
fwrite(s,b,'uint8','async');

else

end

stopasync(s)
fclose(s)
delete(s)
clear s
display('finished')

Please can someone help me as I really need some direction

Thanks
Hi manky manky.

I have to say, that i already did that experiment. And succesfully send some data to a pic 18f452 from matlab, and when the pic received the data put it on the LCD display.

I have the MPLAB code in C18 and the matlab code for send data.

Ideed, i wrote a code for send data from pic18f452 to my PC, and receive the data in Matlab, also i have the .m file and the mplab file.

I use C18 so it was really easy to made the pic->Pc and Pc->pic interface, and the read and write of data.

Well, answering your question, the first thing to do, is verify that your computer is sending the serial packages of information, you can do this with a scope, or if is a hobby project at your home you can verify that the data is sending with a LED ( just put the LED in the transmission line), and send data too many times with a for or while cycle in matlab.

Once this have been verified, the next step is check that the pic is receiving the data. How?. Well there is an option where you can program an interruption when the pic is receiving data, so you need to program that interruption and in the interruption routine send one bit to one free PORT (A , B or C) , and light up a LED when the pic enter to the interruption routine.

Remember to put in ON, the Global Interruption Enable Flag, AND the Peripheral Interrupt Enable bit. Maybe in the next week i'll make some videos for youtube where i show how this things function.

Sorry about my english, i'm a not a native english speaker.

Au Revoir!
 
Hey

Thanks very much for your reply, and your english is really impressive by the way! :) Yeah I actually already got the interface working. I had to do the write routine from matlab to the PIC and then underneath that code, I managed to read data from the PIC into matlab... quite strange that it only seemed to work in that specific order...

But thanks again for your help and a video would be really helpful, I actually am using hitech c18 to program... when i try use c18 i get a PIC INFO- CHIP error or something like that when i build the program... I dunno what the problem is exactly...

Thanks again

Mankmanky1 :)
 
Hi there!

Don't worry im glad to help you, and thanks for your comment about my english, you're very kind.

Im glad to hear, that you already had working the interface, good job!

I can imagine a sort of things about why, you only can write and then read data, and not viceversa.

When you use the functions fwrite or fprintf , and then fread or other read function, i think it works fine, because the write function send the data and end the function, and then the read function if asyncronous, wait for data until a data arrive or until the Timeout has reached or the InputBuffer in matlab is complete.

You can see the properties of your serial object created in matlab using ( once created ) the object get(nameofobject). And you can see the Timeout and InputBufferSize.

Fo be clearly, the Timeout is the waiting time to complete a read or write operation ( If a time-out occurs, the read or write operation aborts), and the InputBufferSize is the amout of data that you are going to read ( in bytes, A read operation is terminated if the amount of data stored in the input buffer equals the InputBufferSize value or a time-out occur. ), if you are going to read only one byte put 1, if you don't know how many byte you are going to receive, put a large one or make some calculations.

So maybe when you first do a read operation and no data is coming, or the data arrive after the timeout you will have an error (so you need more time in the read operation, the default timeout are 10 seconds, but you can change it), and tou can't continue with the script.

Well i hope this can help you.
If i was not very clearly plz let me know, to explain it better.

These are my hypothesis, about your problem in the matlab side. In the Pic side, there are too many posibilities, if you can give me more clues maybe we can solve that.

Au Revoir! MankManky

Have a nice day.
I leave you a picture where you can see a serial packages sending to matlab at rising and falling edges.

S5002953 on Flickr - Photo Sharing!
S5002951 on Flickr - Photo Sharing!
S5002926 on Flickr - Photo Sharing!
 
Hi.
its my first time to post a question in this forum !
I want to send some pseudorandom data pattern to the serial port and then receive it back (loopback) through my transceiver prototype using matlab and then calculate the
bit-error-rate(BER) by compairing the sent and received patterns.
please help me !
thanks in advance
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top