jman
jman
Branch Unspecified
03 Dec 2008

matlab to pic serial communication

HI there, I am new to serial communication and i am trying to send data via matlab to my pic18f4431 microcontroller via a serial port. I am programming my pic using picc18 compiler in mplab and then loadng that program into a bootloader for my pic.

The problem is i dont think that my coding is correct and so here it is:

Pic code:

#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;
}

Matlab Code:

b=[7 4];
s = serial('COM2');
set(s,'BaudRate',9600,'DataBits',8,'Parity','none' ,'StopBits',1,'FlowControl','none','InputBufferSiz e',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')

If anyone knows what the problem is or knows of the correct way to create the communication between the pic and matlab, please let me know...

Thanks
durga ch

durga ch

Communications
10 years ago
Hi
Tried doing something.
after taking a brush up from The serial port session in ' help' section of matlab i tried modifying your above code accordingly

%Create a serial port object — Create a serial port object for a specific serial port using the serial creation function.

%Configure properties during object creation if necessary. In particular, you might want to configure properties associated with serial port communications such as the baud rate, the number of data bits, and so on.

%Connect to the device — Connect the serial port object to the device using the fopen function.

%After the object is connected, alter the necessary device settings by configuring property values, read data, and write data.


%Configure properties — To establish the desired serial port object behavior, assign values to properties using the set function or dot notation.

%In practice, you can configure many of the properties at any time including during, or just after, object creation. Conversely, depending on your device settings and the requirements of your serial port application, you might be able to accept the default property values and skip this step.


%Write and read data — Write data to the device using the fprintf or fwrite function, and read data from the device using the fgetl, fgets, fread, fscanf, or readasync function.

%The serial port object behaves according to the previously configured or default property values.


%Disconnect and clean up — When you no longer need the serial port object, disconnect it from the device using the fclose function, remove it from memory using the delete function, and remove it from the MATLAB® workspace using the clear command.





disp('Start');
s = serial('COM3'); %port creation

set(s,'BaudRate',9600,'DataBits',8,'Parity','none' ,'StopBits',1,'FlowControl','none','InputBufferSize',102400) % serial port properties
fopen(s); % connecting to a device using fopen
s.ReadAsynMode='Continuous'; % mode of reading
readasync(s1);
l=[hex2dec('AB1F'); % input data
l=char(l);
fwrite(s,l); %sending data to micro controller
w=fread(s,1,'unchar') %reading from the port a value at a atime

stopasync(s)
fclose(s)
delete(s)
clear s
disp('end');


it was executed but with error "??? Error using ==> serial.fopen at 71
Port: COM3 is not available. No ports are available.
Use INSTRFIND to determine if other instrument objects are connected to the requested device."



Will look more and try to come up with something more working!! 😀 meanwhile if you have nay updates do post on!!!
jman

jman

Branch Unspecified
10 years ago
Hey Durga,

I finally got it working! I wasnt initialising the PIC properly to receive data.

Here is the code that works:

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


void USART_Init() { 

TRISC=0b10000000; // RX 
//SPBRG= 0x0F;
//SPBRGH = 0x04;    // 2400 Baud.      0x1386 =2400;
TXSTA = 0x24; // TX enable BRGH=1 
RCSTA = 0x90; // continuous RX 
//BAUDCTL = 0x01; // BRG16 = 1 
} 


void main(void)
{
USART_Init();

TRISA=0b00000000;


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

PORTA=RCREG;
}
}//END MAIN
Now my problem is reading data from the PIC into matlab, if you have any suggestions as to how to initialise the pic correctly please let me know. Thanks

Dan 😀
durga ch

durga ch

Communications
10 years ago
hmmm!

will work on this week end... its exicting to learn new stuff 😁

Share this content on your social channels -

Only logged in users can reply.