I2C on PIC: Microchip MCC Boilerplate Is A Pretty Good Tutorial In Itself.

After completing the MCC tutorial, I knew I wanted to start learning about implementing I²C on the PIC micro controller. I planned to use PIC in conjunction with a Raspberry Pi and I²C is the protocol already used by existing Raspberry Pi peripherals. I wanted my PIC projects to play well with others in this ecosystem.

The Microchip tutorial covered serial communication with the EUSART module but not communication with the MSSP (Master Synchronous Serial Port) module that assists with implementing the I²C protocol.  I went hunting for such a tutorial elsewhere on the Microchip web site but didn’t find one.

At this point I was not terribly worried. The PIC16F18345 data sheet provided an overview of implementing I²C using the MSSP module.  Section 30.5 I2C Slave Mode Operation included a list of operations during normal I²C communication. This list was almost a pseudo-code outline of what the developer needs to do to implement I²C communication on the PIC.

I would be happier if there was a tutorial walking through the actual code of an I²C implementation, but the pseudo-code should be enough to help me take whatever boilerplate MCC (MPLAB Xpress Code Configurator) generated and filling in the missing parts.

I created a new PIC project and used MCC to enable the MSSP module for I²C communication. After auto generation was complete, I went into the code ready to get to work. And… hmm… what’s this?


The MCC boilerplate is more than just a bunch of function prototypes, it’s actually an implementation of an I²C slave acting like a EEPROM device. This was, for all practical purposes, the tutorial I had been looking for. Within an hour I was controlling the brightness of a single LED via commands sent over I²C bus and interpreted by my PIC.

So let this be an announcement to other PIC programming novices: sometimes the tutorial you’re looking for is right in the MCC boilerplate code.


Two Weeks with the MPLAB Xpress PIC16F18345 Evaluation Board

DM164141 boxIt’s been about two weeks since I started learning the MPLAB Xpress PIC16F18345 Evaluation Board in earnest. I went from having only a vague idea about PIC micro controllers to having a decent grasp on the strengths and weaknesses of Microchip’s family of 8-bit PIC micro controllers. From never putting my hands on a physical PIC to writing several simple programs and wiring the PIC up to physical components. This is my two-week report.

The short version: The PIC is solid. Everything around the PIC16F18345 feels like a good start of an idea, but the current state is very rough.

The longer version, starting by getting the bad news out-of-the-way first:

Missing Technical Information: I had made a first pass through the PIC16F18345 data sheet to learn about the chip. It had more information than I could possibly absorb on the first pass. But similar information is nowhere to be found for the evaluation board built around the chip. The only documentation on the product page is a quick start guide that boils down to “Login to MPLAB Xpress website”. There is no data sheet, no schematic.

First example: I stumbled across the USB serial port functionality by reading an example. I should have been able to learn about it from technical documentation.

Second example: There are two soldering pads on the board labelled “3V3” and “5V” implying it’s possible to switch voltage levels, but no details on how. Again, this should have been documented.

I hope this is the effect of a new product in a young product line, and it’ll just take some time for the documentation to show up. In the meantime, I remain in the dark about the specifics of this board.

MPLAB Xpress: I appreciate Microchip is making PIC development more accessible by moving software into the cloud, and building PIC programming hardware into a simple evaluation board. It is all very low-friction up until I found out the MPLAB Code Configurator (MCC) module requires Java to run. I hope they move MCC to be web-based as well. Once they do that, it’ll be possible to do PIC development from platforms like Chromebook and Raspberry Pi. Which will broaden their market for PIC development.

MPLAB X: Since I had installed Java on my Ubuntu anyway, I decided to investigate their full-featured IDE to compare against the web-based Xpress. I thought that I could compile the *.hex file in MPLAB X and copy it onto the evaluation board the same way as I do for MPLAB Xpress. Unfortunately this plan was foiled by how the two components interact.

When MPLAB X launches, the USB mass storage device on the evaluation board disappears. It doesn’t reappear until I quit MPLAB X and unplug/replug the board. I was hopeful this meant MPLAB X could talk to the evaluation board directly, bypassing the file copy stage, but I saw nothing of the sort. None of the MPLAB X programming functions seemed to know how to talk to the evaluation board. So in order to update the program on the chip, I have to quit MPLAB X and unplug/replug the board. This is an unacceptable workflow. I have to stick with MPLAB Xpress until I find a solution.

Serial communication is flaky: In addition to the USB mass storage device disappearing under MPLAB X, I also had problem with the USB serial port for the chip to talk to my PC running Ubuntu. It would frequently fall out of sync with the host PC and result in gibberish. I could work around the issue by making sure software flow control is activated, then quit/restart my instance of minicom. But this shouldn’t be necessary.

No hardware debugging: The USB mass storage based programming interface is pretty innovative. The downside is that there is no way to debug the code running on the hardware through this USB port.

OK, I’m done complaining. It’s time to recognize the good this product represents.

For the audience of people like me, interested in getting into PIC programming, it is fantastic. This product line has lowered the barrier of entry tremendously. Ignoring MCC, the software side is far more easily accessible than before. On the hardware side, the evaluation board has a retail price of $12, a fraction of the price of admission previously charged for PIC development.

Before this product, the least expensive PIC programmer device from Microchip (PICkit 3) has a MSRP of $60. And that money doesn’t even include any of the connectors, the circuit board, serial port breakout, power supply, or the PIC itself! All of those required pieces have been simplified and integrated into a single $12 evaluation board.

What I have in my hands today feels like a very rough first draft of a new and very promising product line. As a PIC development platform for beginners, we’re nowhere near perfect today. The features are rough and the documentation is thin. But I sincerely hope Microchip keeps refining this effort and filling in the gaps of this product line.

Know Thy Chip: Reading the PIC16F18345 Data Sheet

Now that I got a taste of PIC development with the tutorials, I have a much better idea of what a PIC can and can’t do. That survey gave me a rough outline with which I can use to keep myself oriented while diving deeper. So on the advice of a local longtime PIC tinkerer, I dove in to the authoritative source: the data sheet Microchip publishes for the PIC16F18345 chip.

I rarely deal with a PDF with so many pages. Take a deep breath, we’re going in!

This data “sheet” is actually a tome with almost 500 pages (as of rev. E) of dense technical information and it would be unrealistic to expect that I would understand all of it or even be able to remember very much of it. Fortunately that is not the goal. This first pass will let me know the kind of information that could be found in the data sheet. So that when I need to know a specific detail in the future, I know I can find it here.

I was surprised to find power management as a topic again and again, but in hindsight I shouldn’t be. PIC micro controllers are used in a lot of small battery-powered devices, catering to engineers building devices that run on a tiny battery for weeks, months, or years. People who build these solutions (which doesn’t include me… or at least not yet) need to know how the chip can be put into various low-power modes and what functionality does or does not work in those modes.

Another topic that received a lot of pages in the data sheet are the integrated “core-independent peripherals” that handle common tasks. The different sets of peripherals are a large part of how one PIC edition differs from another one in the same family. Each peripheral is briefly covered in the data sheet with basic description and a rough outline of how to use them on a PIC chip.

Some peripherals are fairly straightforward in function: the serial communication tutorial has already introduced me to the serial modules, and I paid special attention to the I²C communication peripheral since I intend to use that to communicate with a Raspberry Pi. I smiled at the Configurable Logic Cell peripheral because I recognized it as the modern replacement for the network of 7400-series chips that I last saw in college lab exercises.

Others peripherals are less obvious to me. I can read the description for modules like the Complementary Waveform Generator, but my brain has yet to make the connection between these tools and the problems I would solve with them. I hope that when the appropriate problem arises, I would recognize it and know to come back to the data sheet to learn how to design a solution.


Combining Tutorials for Serial Communication on PIC16F18345

As I read through the tutorials mentioned in the previous two posts, I tried to follow along on my tool with the long name “MPLAB Xpress PIC16F18345 Evaluation Board“. This was a good exercise because those tutorials were written for different demonstration boards. Following along required some modifications to run on my specific evaluation board, which meant I had to have enough comprehension to know what I needed to do. I couldn’t get away with just monkey-see monkey do.

Case in point: the serial communication project in the MCC tutorial. There were a few differences to reconcile before I got it up and running on my own hardware.

By this point in my PIC self-education, I knew how to resolve most of the differences. The PIC itself was different but not a hinderance. The tutorial’s PIC16F1709 has the same USART module as my PIC16F18345 and understood the same instructions. The tutorial called for a PICkit 3 programmer/debugger which is different from the programmer built into my evaluation board but I’ve learned how to program my board. And the tutorial used the full-featured MPLAB X IDE application whereas my board was designed for the web-based MPLAB Xpress. For the most part it was a matter of navigating different menu structures.

The serial communication lines, though, got a little trickier. The tutorial calls for a PICDEM Lab II development board equipped with a MCP2221 breakout module to allow serial communication back to the PC. I thought I was stuck until I found a serial communication example specific to my evaluation board. This is where I learned the board has a built-in serial communication module hard-wired to specific pins and routed back to the PC connected to the USB port.

USART diagram
This diagram in Microchip’s tutorial labelled chips with different numbers than what’s actually present in our setup, but we get the idea.

From there it was a matter of combining bits and pieces from both tutorials in order to assemble a project that implements the serial communication functionality. This project is also where I experimented with tracking MPLAB X project directories in Github, so I could share the results with the world.


Notes on “Get Started with 8-Bit PIC MCUs using MCC”

It looks like Microchip loves long titles for their tutorials. After getting an overview of the 8-bit PICs in the previous tutorial Get Started with the PIC16F1 Enhanced MCU Architecture , I chose to follow up with this tutorial Get Started with 8-Bit PIC MCUs using MCC. It seemed to be a good starting point because MCC (MPLAB Code Configurator) is their tool to alleviate the pain of setting up a new project. The prospect of avoiding tedious work is good motivation to learn the tool.

I had expected a tutorial talking about the design intent of MCC, maybe an overview of its feature areas, or similar pointers to help the novice orient themselves as they tried to use the tool. A little theory behind the practice. This turned out not to be the case.

MCC Tutorial OutlineThe tutorial jumps straight into the practice with 12 simple PIC projects, each demonstrating a basic PIC task. Each project starts with how to set up hardware bits (LEDs, resistors, etc) on a project breadboard. Then it moves to the software side by walking the reader through using MCC to create all the boilerplate code setting up the project. After MCC took care of all the repetitious tasks, the reader has to add a few lines of task-specific code to finish things up.

I like to learn by doing, so this approach worked pretty well to help me gain introductory familiarity with MCC feature areas. For the tasks covered by these projects, I’m now confident I know enough to get going on my own. And this knowledge should help me hunt around MCC for the features not covered by the tutorial.

And there are a lot of those! The PIC is capable of more than these 12 things, and it’s not clear Microchip has provided similar tutorials elsewhere. For example, this tutorial covers serial communication with the PIC USART module. My goal is to communicate with a Raspberry Pi over I²C, which is a different module. So I’ll have to look elsewhere to find a guide to help me speak in I²C, the protocol used by many Raspberry Pi accessories.

Notes on “Get Started With The PIC16F1 Enhanced MCU Architecture”

I was annoyed that Microchip didn’t make their tutorials easily accessible from their top-level site navigation bar. It took Google indexing from outside the site to point me in the right direction. Once I knew these things existed, though, it was time to get to work.

Like most of the Microchip site, the target audience is not the complete novice. This tutorial gives an overview of the features of their PIC16F1 series using vocabulary that would be understandable for people already well-versed in the field. For example, the first section explains these PIC chips are Harvard architecture designs. This was presented without explanation, footnote, or link to Wikipedia explaining what Harvard architecture was. (Hint: It has nothing to do with style of buildings on the Harvard University campus.) The target audience for this tutorial would already know what that meant.

Many years ago I dipped my toes in learning the Ubicom/Parallax SX line of micro controllers. They were competing with PIC micro controllers in some markets and share some design similarities including the Harvard architecture. At the time, I chose to experiment with the SX line because it was promoted by Parallax. Their big hit BASIC Stamp product line caters to absolute beginners and a few BASIC Stamp were created with a SX chip at its heart. Parallax offered the SX chip itself for the more advanced users. The product line was backed by documentation written by the same crew that knew how to write for novices.

Unfortunately the SX product line never found the same success PIC did. So it has since been discontinued. The little bits and pieces I still remember from the experience helps me understand PIC today. Even with that help, I’m not quite up to the level expected by the author of the Microchip PIC tutorial, which meant a lot of Google side trips to understand the various topics.

One place the SX differ significantly from PIC was the implementation intent. The SX put their focus on software: The chips were simple but designed to run at significantly higher clock speeds (75 MHz) compared to their contemporary PIC counterparts (typically 1-4 MHz). This allows the SX to implement a large range of functionality strictly in software, reducing the need for peripherals.

In contrast, PIC controllers – especially the ones today – is less focused on the code running on the core chip and more focus on having that code orchestrate peripherals to do the actual work. The deep product line offers PIC chips with varying set of peripherals to suit different goals. It can get bewildering but since I already have a specific PIC in hand, I can stay focused on the subset of peripherals on this PIC16F18345 chip.

Get Started PIC16

Microchip Hides Their Tutorials. Why?

The last time I tried to learn PIC programming, I was frustrated by their developer support web site. There’s a lot of detailed reference material for people who already know what they are doing and know what they are looking for. I didn’t see anything like a tutorial for novices.

This time around Google helped me find a tutorial titled “Get Started With The PIC16F1 Enhanced MCU Architecture“. Since the PIC on my evaluation board is the PIC16F18345 I thought that was a great place to start. When it came  up on my web browser, I looked on the navigation bar on the left and felt stupid. The tutorial is under the big bold “Tutorial” section on the global navigation bar. Why didn’t I see that before?

A Getting Started tutorial under the “Tutorial” section of the global navigation bar.

The answer? Because it was not there. If somebody just opens up the Microchip developer web site directly as I did before, without the URL taking me directly to a tutorial, the “Tutorial” section is absent from the navigation bar.


Most of the other sections are the same, but “Tutorial” is gone from below “Help” and instead there is “Store” above “Help”. I guess Microchip is more eager to take my money than to teach me why I might want to give them money.

Why does Microchip mask the existence of the tutorials section? I don’t know. I wish they hadn’t, because I would have been able to get started on PIC programming much earlier.

Maybe they’re ashamed of the quality of the tutorials? I hope not, because I’m about to dive in and I’d be sad if the tutorials sucked.

MPLAB Xpress Code Configurator Requires Java. Good Feeling’s Gone.

Getting started with the MPLAB Xpress development system, I’ve been impressed by what I saw. Microchip’s PIC platform has a lineage that goes all the way back to 1976 but the new web-based tools show they are making serious effort to move the development experience into 2017. A HTML-based code editor, cloud-based compilation, download to local computer, and USB storage device based hardware interface making it accessible to every modern computer with an internet connection.

I had compiled a few pre-built examples to try out the process and feeling pretty good about the whole thing. Until the time came for me to create a new project from scratch. The boilerplate code for a new project sets up all the parameters tailored to the features and peripherals of a specific PIC chip, and the tool to take care of all these details is the “MPLAB Xpress Code Configurator” (MCC).

So far so good, I clicked on the blue “MCC” icon, and got this.

MCC Requires Java


The sound in my head is of screeching tires punctuated by the crunching metal of a car crash. Maybe they have ambition to move PIC development into 2017, but today starting a new MPLAB Xpress project requires a trip back to 1995 when Java was king.

If MCC was a web-browser based Java applet, we would be completely out of luck as Firefox and Chrome both dropped support due to all the security nightmares the past few years, and those were the only two browsers I have installed on my Ubuntu development system.

Fortunately MCC is a client-side application launched with a JNLP file so there is still some hope. I tried the open-source Java implementation first, but icedtea-netx does not work at the moment. So off to Oracle I go to download the official closed-source binaries which got me past this blocker.

In hindsight I should have created a virtual machine (probably with VirtualBox) and installed Java only in that VM to contain any possible damage from future Java security vulnerabilities. I’ll do that on my next clean install of Ubuntu.

Back to MCC: it feels like a chunk of legacy code carried over to today’s MPLAB Xpress. Having to install Java is very much counter to their “nothing to setup or install” pitch so I expect they will migrate MCC functionality to be HTML-based as well.

Hopefully it’ll just be a matter of time.

MPLAB Xpress Examples Are More Helpful When I Actually Click On Link To Explanation.

PIC processors have been a popular tool for decades, catering to a wide range of usage scenarios. If you add up all the possible features and all the possible variants, it totally overwhelms the beginner trying to find a place to start. That was my problem last time: I didn’t know which chip I should start with, what tools to learn, and I didn’t even know how to navigate the documentation to inform these decisions.

But now that I have an evaluation board, I can focus on starting off with the chip on it (PIC16F18345) and use the tool (MPLAB Xpress web IDE) it is designed to work with. Microchip provides examples to help get people up to speed. This code repository is also open for people to upload and share. As of this writing, the examples total about 200, far short of something like Github. But it’s a start.

They even have a few examples for the specific board I have. Not just the chip, the full evaluation board. This spares me the headache of trying to translate code meant for a different device. I saw the big red “IDE” on the right hand side and clicked it.

Examples 345
Big red “IDE” on right are links. Big red titles on left are also links.

As expected, the project opened up in the development environment and…


Now what?

A lot of the code in the project appear to be auto-generated boilerplate. Most of the comments are legal disclaimers and the rest are gibberish to a beginner. The main() function has a while(1) infinite loop and no code inside that loop. What’s actually doing anything here? I wandered aimlessly looking for something to grab onto as my Rosetta Stone to get started.

Jumping straight into the code was not as  helpful as I had hoped.

After a while, I had to take a break and take care of other things. When I returned, my session had timed out and I had to restart. I retraced my steps and was just about to click on the big red “IDE” when I noticed… hey. The title on the left is also big and red. Could it be a link?

Not only was it a link, it was in fact a link to an explanation of the project and steps walking through how to re-create the example from scratch. I just got distracted by the big red “IDE” taking me on the wrong path.

Hardware Support for Web-Based PIC Microcontroller Development

MPLab Xpress IDE LogoNormally, micro controller programs are written on a PC and compiled into binary code that is loaded onto the chip via a device generically called “programmer“. Afterwards, the chip can be removed from the programmer device and start performing the tasks described in the binary code.

These programmer devices are hardware peripherals. So as a web-based tool, MPLAB Xpress would not be able to utilize them without installing additional software to bridge the gap. This is counter to the “Xpress” goal of offering zero-install, low barrier to entry into the world of PIC.

Microchip Technology’s solution to this challenge is to create “MPLAB Xpress” branded evaluation boards with programmer hardware already on board. This specific product line is designed to eliminate the need for specialized devices or driver software, because it talks to the connected computer as a USB flash storage device. As far as the PC knows, this evaluation board is just another USB flash drive, which the PC knows how to access it without special drivers.

Customers with these evaluation boards could write a PIC program in their web browser with MPLAB Xpress and build the program into a binary file that the browser downloads. The downloaded file can then be copied to the evaluation board just like copying to a USB flash drive. Then the user presses the reset button on the board and the PIC16F18345 starts running the program.

An interesting side note: how did Microchip implement a PIC programmer that accepts compiled binary code via USB flash storage? They did it with another PIC. The evaluation board has two PIC chips on board: One is a PIC16F1454 always running the USB storage and programming functions. The other is the PIC16F18345 chip we’re actually writing code for.

So this is why the name of the cloud-based tool is in the name of this piece of hardware. This line of evaluation boards have been specifically designed to support the cloud-based tool, making it possible to get started writing code for PIC chips without installing any software.

Parsing the Long Name: “Microchip MPLAB Xpress PIC16F18345 Evaluation Board”

Giveaway winner holding the prize in front of his Luggable PC, photo by [Shulie]
There was a giveaway at the Hackaday LA August meetup and I came home with a little box labelled “Microchip MPLAB Xpress PIC16F18345 Evaluation Board“. (Retail value: $12) While I had some vague ideas about PIC chips, I’ve never had the chance to sit down and play with one. Thanks to Hackaday LA, I now have my chance.

In my exploration of hardware projects, there have been occasional times when I thought: “Hmm, I wonder if this is the kind of problem I could solve with a PIC.” Now that I have one to learn with, I hope to reach a point where I can definitively recognize problems that I can (or can’t) solve with a PIC.

The first thing to figure out is what I actually have in my possession. The long name was more than a little intimidating but a little research parsed the name into the following components:

  • Microchip in this context is not the generic meaning but the proper name of the company Microchip Technology.
  • MPLAB is a product line of software tools they produce. Offered to engineers for developing products around their hardware such as the PIC chips.
    • MPLAB Xpress is a specific product in this line. It is a cloud-based variant of the full MPLAB software suite, scaled down to lower the barrier of entry into the world of PIC programming.
  • PIC16F18345 is a cryptic alphanumeric string but that also makes it the least ambiguous one to understand. It designates a specific chip from the extensive PIC product line.
  • Evaluation means this is for prototyping and not for companies to put into large volume products. I won’t understand why until later.
  • Board means all of the above are mounted on a single circuit board.

After decoding the individual words, my question was “Why is the name of their MPLAB Xpress software on this hardware device?”

The short answer: this evaluation board’s on-board programmer has been tailored to support MPLAB Xpress’ cloud-based microcontroller development model.

The longer answer is coming up as tomorrow’s post.