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

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us: