Arrow Electronic Components Online

PIC microcontroller programming: The basics for every designer

Microcontrollesr12 Feb 2024
A close-up view of a circuit board with a microcontroller socket, as a hand uses a small screwdriver to adjust or work on electronic components
View all articles

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

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

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

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 microcontrollers

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.

Article Tags

Microchip Technology
Consumer Electronics
Autonomous Machines
Automotive
Internet of Things (IoT)
Embedded Programming

Related Content