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
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
0