Sunteți pe pagina 1din 5

Home Sign Up!

Explore Community Submit

MATLAB to PIC serial interface


by leevonk on March 29, 2006

Table of Contents
intro: MATLAB to PIC serial interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

step 1: Code with comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

File Downloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Customized Instructable T-shirts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

http://www.instructables.com/id/MATLAB-to-PIC-serial-interface/

intro: MATLAB to PIC serial interface


some code to let MATLAB talk to a PIC16f877 via the serial port. This isn't that hard nor is it much code, but I spent a decent amount of time figuring it all out (starting
from scratch), so I figured it would be helpful. Enjoy.

step 1: Code with comments


below is some code I pasted in. Download the .txt file if you want the code. See the pictures for help witht he serial interface. One shows the 22kohm resistor method of
connecting your serial port to a PIC. The other shows the max232 method of connecting your serial port to a PIC.
With serial connections the most common problem is not setting the computer and the PIC to the same Baud Rate, so make sure you do that (as it's done in my code).
Also, make sure you've connected all of the GNDs together (PIC, MAX232, and Computer GNDs all connected).
Also, make sure that you're using the right capacitor value for the max232 (max232 vs. max232A), see diagram in the instructable for the proper values.
Also, make sure that your computer is sending the values you think it is sending. To do this, stick wires into your DB9 cable to make a serial loopback. This will make the
computer get back exactly what it has just sent. Here's a diagram of how to wire this up:
http://electrosofts.com/serial/loopback.jpg
So make your matlab code do a serial read right after the serial write.
You can also use free serial port monitoring software to makesure serial communications are occuring properly (here's one example
http://www.batchconverter.com/FreeSerialPortMonitor-download-20643.shtml).
Anyway, here's the code:
%##########################################################################
%########################--MATLAB CODE--###################################
%##########################################################################
%This code assumes that you have MATLAB hooked up to a PIC microcontroller via the PC's serial port through a
%MAX232 IC _OR_ if you don't use a max232 all you need is a 22 kohm limiting resistor on
%the serial line which connects the computer to the PIC (aka Pin 3 and/or Pin 2, see below).
%Pin 3 or the DB-9 port is the TX line, aka the line that carries serial data from the PC to the PIC.
%Pin 2 carries serial data the other way (PIC to PC) if you want to do that.
%Pin 5 is the DB-9 ground pin which you should use to connect the PC ground with the PIC ground.
%This code also assumes that you're using COM3 for serial communication with the PIC.
SerPIC = serial('COM3'); %<--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')
%-- 003 = 00000011 in binary, so first two LEDs of PIC will light
fclose(SerPIC) %--close the serial port when done
delete(SerPIC)
clear SerPIC
%NOTE 1:
%if MATLAB ever gives a serial error, it will most likely say 'unable to open serial port' next time you
%run the program, so you have to restart MATLAB
%NOTE 2:
%the number must always be sent as a three digit number because for some reason I couldn't get my PIC to receive
%the number when it wasn't of a specified length (see PIC code: DEC3)
%==========================================================================
%==========================================================================
'##########################################################################
'#############################--PIC16F877 CODE--###########################
'##########################################################################
'This code assumes you have the PIC16F877 set up with one LED on each line of Port D (8 lines).
'This way the PIC will light up a different pattern of LEDs according to the number send via serial from MATLAB.
'Each LED should be in series with a 470 ohm resistor (see pic below)

http://www.instructables.com/id/MATLAB-to-PIC-serial-interface/

'_
''
' '------LED---\ 470 ohm
'PIC' >------/\/\/\------GND
' '------LED---/
'---'
'
'This code also assumes that you have the TX line of the DB-9 serial port connected to pin 0 of port C (PORTC.0)
Include "bs2defs.bas" 'has some useful stuff in it
'DEFINE OSC 4 'Oscillator speed in MHz, this isn't needed I guess
SerI var PORTC.0 'make an easy name to refer to the serial pin
TRISD = %00000000 'set PortD as an output port
PortD = %00000000 'set LED port to all zeros
GetGhost:
Serin2 SerI, 84, (DEC3 B0) 'get a three digit number from serial pin and put it into B0 variabl
PortD = B0 'set the lines of PortD according to this three digit number
GOTO GetGhost
'===========================================================================
'===========================================================================

File Downloads
Lee_PIC_To_MATLAB_serial_communication.txt (3 KB)
[NOTE: When saving, if you see .tmp as the file ext, rename it to 'Lee_PIC_To_MATLAB_serial_communication.txt']

Related Instructables

PICBasic serial
interrupts with
PIC16F877 by
leevonk

Eagle-ize
Leevonk's PIC
Intro to PIC
Microcontrollers protoboard by
westfw
by leevonk

Put your R/C car LCS-1M - A Fullunder computer Featured, Lowcontrol by prank Cost Hobby
Oscilloscope by
womai

http://www.instructables.com/id/MATLAB-to-PIC-serial-interface/

PIC
development/testing
board by leevonk RF Serial Data
Link {through
USB} by barmak

How to choose
a
MicroController
by westfw

Advertisements
Customized Instructable T-shirts

Comments
13 comments Add Comment

crapper says:

Sep 6, 2008. 10:33 AM REPLY


thanks for the instructable. but i have problem. my laptop doesnt have a serial port, only usb. how can i connect to my laptop? i dont think i can use a usbserial converter since its +5v 0. helpppp

duyngdang says:

Jan 21, 2008. 12:43 AM REPLY

Help me with COM in Matlab!


In Matlab command window,I type:
set(COM1,'BaudRate',9600,'DataBit',8,'Parity','none','StopBit',1,'Flowcontrol','none');
fopen(COM1)
then error:
??? Undefined function or variable 'COM1'.
Some body can help me about that! I'm very new with Matlab and conputer interface!
Thanks!

morteza.jalalat says:

May 6, 2008. 6:03 AM REPLY

my dear,you must type as below for creating a serial port object :


s=('COM1','BaudRate',9600,'DataBit',8,'Parity','none','StopBit',1,'Flowcontrol','none');
fopen(s)
where s is your object !!

duyngdang says:

Jan 21, 2008. 7:59 PM REPLY

Now I can open port with Matlab.


Now I have to put an hex file to Pic16F88? Would you please share for me the the hex file to put in the Pic16F877?
I have programmer kit and soft for pic but I don't have any software to program and convert to hext files
After that, I have to put some led to pins of pic16F877 for this command: fprintf(SerPIC, '%s', '003'); ?
Would you please send me the schematic for that?
I'm a mechanic guy,so I'm trying to know about computer interface. Please help me for that!
Thanks!

leevonk says:

Jan 21, 2008. 7:07 AM REPLY

uhm, how bout using my code that's staring you right in the face on the next page:
SerPIC = serial('COM3'); %<--change this appropriately
set(SerPIC,'BaudRate', 9600, 'DataBits', 8, 'Parity', 'none','StopBits', 1, 'FlowControl', 'none');
!!!!!!!!
note the very important SerPIC = serial('COM3');
!!!!!!!!

BlessedtoknowHim says:

Apr 3, 2008. 9:43 PM REPLY


I want to continuously send a x and y coordinate from Matlab via a USB-RS232 cable to a pan-tilt servo assembly controlled by an ooPIC-R. Does the
ooPIC-R convert the RS232 +-10V to TTL +-5V? Should the concept of your code work for this application? I can send one value from Matlab and get a
servo to react but the system acts up when I try to send values continuously. Sometimes I get a "timed out" error in Matlab.

http://www.instructables.com/id/MATLAB-to-PIC-serial-interface/

behrouz2007 says:

Apr 2, 2008. 1:47 AM REPLY


I want use 9bit USART (9th bit set) for sending command to PIC and (9th bit zero) for sending data to PIC. So i want use Parity bit of serial port as 9th bit.
But when i execute herein commands in MATLAB 6:
>> e=serial('COM1')
>> fopen(e)
>> set(e,'Parity','mark')
This error occured:
??? Error using ==> serial/set
Parity could not be set to the specified value.
Please help me. Why i can not change parity to 'mark' and 'space'.

duyngdang says:

Jan 22, 2008. 6:11 PM REPLY

Now I can open port with Matlab.


Now I have to put an hex file to Pic16F88? Would you please share for me the the hex file to put in the Pic16F877?
I have programmer kit and soft for pic but I don't have any software to program and convert to hext files
After that, I have to put some led to pins of pic16F877 for this command: fprintf(SerPIC, '%s', '003'); ?
Would you please send me the schematic for that?
I'm a mechanic guy,so I'm trying to know about computer interface. Please help me for that!
Thanks!

jnk101 says:

Nov 23, 2006. 9:04 PM REPLY

gr8 begining
chk out :
http://www.dspaceinc.com/ww/en/inc/home/products/systems/controld.cfm
have u come across diy / open source for this stuff
really keen on this stuff

finly says:

Jun 1, 2007. 1:59 AM REPLY


Hello, that's a great tutorial.
Do you have any tutorial about how to display signal generated by function generator and A/D converted by PIC ? The PIC will be connected to the PC
using serial.
Thank you.

ppsh04 says:

Apr 20, 2007. 4:56 PM REPLY

Thank you for the experience you have shared here. This post is really effective for my program.
:D Hope you can contribute more. Good Luck!

ferasmetib says:

Mar 29, 2007. 2:06 AM REPLY

would you please add this file


bs2defs.bas
and in what software enviromint is the pic code :
mplap ide, picbasic,....?

leevonk says:

Mar 29, 2007. 7:46 AM REPLY


I used MPLAB with PICBasic set as the compiler. If you have the PICBasic compiler you'll have the bs2defs.bas file, if you don't have PICBasic the file is
useless so I won't post it (I'm not sure if it's legal to post it).
It's not really an important file anyway, If you delete its include from my code, all you have to change is to declare your own variable in place of B0 (B0 is
a predefined variable when using bs2defs.bas).

http://www.instructables.com/id/MATLAB-to-PIC-serial-interface/