MATLAB to PIC serial interface using PIC16F877 microcontroller

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.
PIC serial interface

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).

Schematic PIC serial interface
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:
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

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.