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.

Neue Beiträge

Leider ergab Ihre Suche kein Ergebnis

Aktuelles über Elektronikkomponenten­

Wir haben unsere Datenschutzbestimmungen aktualisiert. Bitte nehmen Sie sich einen Moment Zeit, diese Änderungen zu überprüfen. Mit einem Klick auf "Ich stimme zu", stimmen Sie den Datenschutz- und Nutzungsbedingungen von Arrow Electronics zu.

Wir verwenden Cookies, um den Anwendernutzen zu vergrößern und unsere Webseite zu optimieren. Mehr über Cookies und wie man sie abschaltet finden Sie hier. Cookies und tracking Technologien können für Marketingzwecke verwendet werden.
Durch Klicken von „RICHTLINIEN AKZEPTIEREN“ stimmen Sie der Verwendung von Cookies auf Ihrem Endgerät und der Verwendung von tracking Technologien zu. Klicken Sie auf „MEHR INFORMATIONEN“ unten für mehr Informationen und Anleitungen wie man Cookies und tracking Technologien abschaltet. Das Akzeptieren von Cookies und tracking Technologien ist zwar freiwillig, das Blockieren kann aber eine korrekte Ausführung unserer Website verhindern, und bestimmte Werbung könnte für Sie weniger relevant sein.
Ihr Datenschutz ist uns wichtig. Lesen Sie mehr über unsere Datenschutzrichtlinien hier.