PIC microcontroller programming: The basics for every designer

Microchip Technology’s PIC® line of microcontrollers are popular with design engineers for several reasons, including price, performance and extensive design support. There is also a wide-ranging selection of PIC microcontrollers (MCUs) that have been carefully architected to match the requirements of diverse applications.

Getting started with PICs is similar to getting started with virtually any MCU: At the most basic level, the designer must know – in detail – the logic that will be implemented in hardware on the MCU. Many designs encounter problems because the designer has not thought through the logic completely.

In addition to building solid foundation of circuit logic, a detailed knowledge of the application provides answers to mission-critical questions such as the number of inputs and outputs required; level of performance; cost and development time budgets that must be met.

There are also important questions beyond straightforward electronic design that must be handled by the MCU: Where the system-level device will be used (environmental conditions and geographic location)? What industry standards must it conform to and other products it must interoperate with? What industry and government regulations regarding health, safety and environmental requirements must be met?

Figure 1 shows a block diagram of Microchip Technology’s PIC16F84 with I/O and some peripherals. Introduced in the early 1990s, it performs basic functions. Today’s 8-bit MCUs typically integrate multiple peripherals, which create more software complexity for the application developer.

Block-diagram-of-Microchip-Technology-PIC16F84-with-I-O-and-some-peripherals

Figure 1. The PIC16F84 is among the earliest MCUs to feature a serial programming algorithm and EEPROM memory. Source: Microchip Technology Inc.

PIC microcontroller hardware essentials

To program a PIC microcontroller, you need to know the specific device’s basic building blocks such as configuration registers, buses, and memory types. Understanding the PIC programming language is very useful – if not essential. Choosing the right set of software development tools also makes ramp up easier and faster.

The underlying memory architecture of many PICs is the Harvard architecture, which was first widely deployed in Intel’s 8051 MCU. This architecture has one memory space for storing program commands and allocates another space for data. Having separate memory spaces – which can be on-chip Flash memory, ROM, RAM or an external chip – typically results in faster program execution. (The term “Harvard architecture” is frequently used loosely. Many MCUs referred to as “Harvard” have been modified by mixing the Harvard and von Neumann memory architectures.)

Before we can go much further, we have to be fairly specific about the PIC MCU that will be chosen for the design. An 8-bit PIC is a good choice because they are generally lower in cost than 16- and 32-bit MCUs, have an adequate set of peripherals (such as A/D converters), and offer good performance.

Microchip offers several 8-bit PIC families. The PIC10F and PIC12F are the least expensive but provide less memory, lower pin counts and fewer peripherals. Choosing the lowest cost PIC chip runs the risk of discovering halfway through the design that the MCU does not fully satisfy the needs of the application. The PIC18F series is at the other end of the performance spectrum of Microchip’s 8-bit products. It is more sophisticated, offers highly specialized on-chip peripherals, and is also the most expensive family. For a first design, PIC18F series would probably be overkill.

Design with PIC microcontrollers

A designer just getting started with the PIC microcontroller architecture is probably best served by a chip in the PIC16F series, which has been around for more than a decade and is still popular. Consequently, the family has the largest code library available on the Internet, which means a lot of the basic code has been written and can simply be downloaded.

This trove of resources is invaluable. There is, in particular, a great wealth of code and schematics for PIC16F877, which has been used in many “starter” projects by online and classroom teachers as well as by a legion of professional designers. It is an old MCU design however, and Microchip does not recommend it for new projects. Fortunately, just about all of the PIC16F877 design support is still usable with the PIC16F887, which has pretty much the same options for pin layout and peripherals.

The PIC16F887 runs the same code with – at most – minor modifications. It also uses less expensive software development tools.

The PIC16F887 features 256 bytes of EEPROM data memory and a set of peripherals that make it suitable for A/D applications in automotive, industrial, appliances or consumer applications. Its datasheet can be found here.

How to program a PIC microcontroller

To program a PIC microcontroller (or any MCU), the following software tools are typically required:

  • An IDE (Integrated Development Environment), which converts the symbolic logic written for the application into program commands
  • A compiler, which converts the program into MCU assembly language commonly known by design engineers as HEX files
  • An IPE (Integrated Programming Environment) that it ultimately used to transfer hex file into the PIC after some program tuning
  • An in-circuit debugger/programmer

Microchip’s software development tools are highly regarded for their performance and ease of use. The company’s MPLAB®X IDE, MPLAB®XC Compilers and MPLAB® IPE to relieve designers of the burden of comparing and purchasing third-party tools. Microchip’s basic tools are provided free. An overview of all Microchip development tools can be accessed here.

Figure 2 shows the Microchip products available in five categories of the company MPLABX Integrated Development Environment (IDE).

Microchip-products-available-in-five-categories-of-the-company-MPLABX-Integrated-Development-Environment-IDE

Figure 2. The MPLAB X IDE covers a wide range of ancillary MCU programming functions. Source: Microchip Technology Inc.

For first-time 8-bit PIC users, Microchip has made it even easier to get started by launching MPLAB® Xpress, a cloud-based IDE that eliminates the tedium of downloading, installing, configuring, and periodically updating the tools. MPLAB Xpress includes MPLAB Code Configurator, which enables users to automatically generate initialization and application C code for 8-bit (and 16-bit) PIC MCUs using a graphical interface and pin map.

As with any MCU, the first step is to set up the configuration registers. The bits programmed into these registers specify fundamental device operation, such as the oscillator mode, watchdog timer, programming mode and code protection. These bits must be set correctly for the code to run successfully.

Once the configuration bits have been set, the remainder of the program-creation process depends upon the application. And once the code complete, the next steps involve moving the logic-based entity into the real world of bits.

The Integrated Programming Environment (IPE) comes into play for a variety of functions including transferring the program created on your PC or workstation into the MCU. As previously mentioned, Microchip’s environments combine these functions, but three important concepts are simulation, debugging and programming the PIC. A full description of the software tools and how they are used is beyond the scope of this article.

The PICkit™ 3 In-Circuit Debugger/Programmer is worth mentioning, however, because it is additional hardware and therefore conceptually goes a step beyond the software configuration of the other tools. It is used after the application program is transferred to the MCU. PICkit 3 is controlled by a PC running MPLABX IDE software and is an integral part of the tool suite.

The legend in Figure 3 shows the primary connections between the MCU being programmed or debugged and the PC that runs Microchip Technology’s MPLABX IDE.

Legend-showing-the-primary-connections-between-the-MCU-being-programmed-or-debugged-Microchip-PICkit-3-In-Circuit-Debugger-Programmer

Figure 3. Microchip’s PICkit 3 provides the hardware to debug and/or program a wide range of MCUs. Source: Microchip Technology

Advantages of PIC microcontroller

Getting started with a PIC MCU involves a thorough understanding of the application, the basic hardware architecture of the PIC to be used, and the software tool chain. Microchip has made the actual design process very accessible to engineers who are familiar with other MCUs and true novice designers as well. Free software tools make the set-up cost reasonable and there is a wealth of code and advice on the web both inside Microchip’s design ecosystem and on the web generally. All that is required from the would-be PIC designer is the desire to get started and the persistence to see the project through to completion.


Are you on the list?
Sign up to receive exclusive offers, product announcements, and the latest industry news.

Related news articles

Latest News

Sorry, your filter selection returned no results.

We've updated our privacy policy. Please take a moment to review these changes. By clicking I Agree to Arrow Electronics Terms Of Use  and have read and understand the Privacy Policy and Cookie Policy.

Our website places cookies on your device to improve your experience and to improve our site. Read more about the cookies we use and how to disable them here. Cookies and tracking technologies may be used for marketing purposes.
By clicking “Accept”, you are consenting to placement of cookies on your device and to our use of tracking technologies. Click “Read More” below for more information and instructions on how to disable cookies and tracking technologies. While acceptance of cookies and tracking technologies is voluntary, disabling them may result in the website not working properly, and certain advertisements may be less relevant to you.
We respect your privacy. Read our privacy policy here