Projects   /   Communication & Interfacing   /   PIC16F877A

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.

Published

At a glance

CategoryCommunication & Interfacing
PIC modelPIC16F877A
IncludesCircuit diagram
Reading time~3 min
Jump to a section
  1. MATLAB to PIC serial interface
  2. Code with comments
  3. Quick Solutions to Questions related to MATLAB to PIC Serial Interface:
  4. Related projects

Summary of MATLAB to PIC serial interface using PIC16F877 microcontroller

This article guides users in establishing a serial communication link between MATLAB and a PIC16F877 microcontroller. It highlights critical setup steps, such as matching baud rates and ensuring common ground connections. The text details two wiring methods: using a 22kΩ resistor or a MAX232 chip with specific capacitors. Troubleshooting tips include verifying data transmission via serial loopback cables and utilizing free monitoring software to confirm proper signal flow.

Parts used in the MATLAB to PIC Serial Interface:

  • PIC16F877 microcontroller
  • Computer with MATLAB installed
  • Serial port (DB9 cable)
  • 22kohm resistor
  • MAX232 chip
  • CAPACITORS (specific values for MAX232 vs MAX232A)
  • Ground wires

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:
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).
For more detail: MATLAB to PIC serial interface using PIC16F877 microcontroller
  • What is the most common problem when setting up serial connections?
    The most common problem is not setting the computer and the PIC to the same Baud Rate.
  • How should the ground connections be handled?
    You must connect all GNDs together, including those of the PIC, MAX232, and Computer.
  • Which components are required for the MAX232 method?
    The MAX232 chip requires specific capacitor values depending on whether you use the MAX232 or MAX232A version.
  • How can I verify if my computer is sending the correct values?
    You can stick wires into your DB9 cable to create a serial loopback that returns exactly what was sent.
  • When should I perform a serial read after writing?
    You should make your MATLAB code do a serial read right after the serial write during testing.
  • Can I use external software to monitor the connection?
    Yes, you can use free serial port monitoring software to ensure communications are occurring properly.
  • Is there an alternative to the MAX232 for connecting the serial port?
    Yes, you can use the 22kohm resistor method of connecting your serial port to a PIC.
  • Where can I find a diagram for the serial loopback wiring?
    A diagram is available at http://electrosofts.com/serial/loopback.jpg.
Beginner Tutorials · PIC16F877A

PIC16F877 Driving LCD in 4-Bit Mode using PIC16F877 with Proteus Simulation

Introduction This microcontroller project shows how a PIC16F877 can drive an LCD display using 4-bit mode inside a Proteus simulation.The project demonstrates...

Programmers & ICSP · PIC16F877A

EPE PIC/LCD Demo using PIC16F877 with Proteus Simulation

IntroductionThe EPE PIC/LCD Demo is a classic microcontroller project that demonstrates how a PIC16F877 can fully control a PG12864F graphical LCD.Designed as...

Motors & Robotics · PIC16F877A

Efficient Sun-Following Robot: A Solar Tracker with Microcontroller Control

Abstract The impending global challenge of dwindling non-renewable fuel resources underscores the urgency of transitioning towards renewable energy alternatives like solar power....

Communication & Interfacing · PIC18F4520

CAN FD Node based on a PIC18 Microcontroller

The introduction sets the stage for an embedded systems project that involves developing a Embedded C CAN FD project using the MCP2517FD...

Communication & Interfacing

LAN9662 Gigabit Switch: Optimized for Industrial Ethernet

Introduction It should come as little surprise therefore that one element of what is now more commonly referred to in some circles...

Communication & Interfacing

PIC Microcontroller: Fundamentals & Applications for Students

Introduction Microcontrollers are the key components in the embedded systems market. A variety of embedded systems was formed and developed after the...

Ibrar Ayyub

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