This is only a preview of the January 2021 issue of Silicon Chip. You can view 42 of the 112 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Items relevant to "AM/FM/SW Single-Chip Digital Radio":
Items relevant to "MiniHeart: A Miniature Heartbeat Simulator":
Items relevant to "The Bass Block Subwoofer":
Items relevant to "Busy Loo Indicator":
Articles in this series:
Purchase a printed copy of this issue for $10.00. |
Installing and using
While Arduino software and hardware have made microcontroller projects
accessible, many advanced users prefer to use MPLAB X, especially for PIC
devices. Like the Arduino IDE, it is a free download. This article will step
you through the process of creating your first MPLAB X project.
T
hose who have been dabbling with microproces- AVR parts (as described starting on page 88 of this issue).
sors and microcontrollers for a long time may reMPLAB has evolved into MPLAB X, which is now Javamember a time when writing a program required based, and therefore runs on all major operating systems;
intimate knowledge of instruction sets and memory maps. Windows, Mac and Linux.
You had to hand-write assembly language or even machine
For a long time, we’ve used this software and PICkit procode which would then have to be loaded into an EPROM. grammers to program microcontrollers for our projects, and
Our early microprocessor projects, such as the 1989 Print- we see no reason to change that, especially with MPLAB
er Buffer (siliconchip.com.au/Article/7380) or LED Message X now supporting AVR parts.
Board (siliconchip.com.au/Series/255),
There may be some out there
also from 1989, used a Z80 microprofor whom microcontroller processor with separate RAM chips and
gramming remains a mystery;
an EPROM chip.
perhaps you’re happy just buyAbout the same time came the first
ing pre-programmed parts from
PIC microcontrollers, with integrated
the SILICON CHIP ONLINE SHOP, or
program EPROM and in the next decyou just don’t have the need. But
ade, flash memory. These were typicalyou may be interested in the proly programmed in assembly language,
cess nonetheless. Or you might
with the machine code created by an
like to jump into the world of
assembler program.
microcontrollers.
Flash-based devices such as the
Rest assured that the process
PIC16F84 meant it was finally possionly continues to get easier. In
ble to quickly and easily update code
this article, we’ll introduce and
without having to manually erase an
review the latest version of MiEEPROM under a UV lamp (or sunlight,
crochip’s MPLAB X IDE.
if you didn’t have a UV lamp).
Once you’ve read through it,
It was over ten years ago, in July
you might also like to read the
2010, that we last gave an in-depth
separate article on the new AVR
‘howto’ on programming PIC microDA family of microcontrollers
controllers (siliconchip.com.au/Article/208). That article which are supported by MPLAB X.
introduced the PICkit 3 programmer/debugger from MicroMPLAB X is not tied to that particular chip or developchip and their MPLAB software.
ment board, but can be used with many Microchip microconMPLAB is a complete IDE (integrated development en- trollers, including virtually all PICs and many AVR MCUs.
vironment). It’s integrated because it includes the ability
to write programs in a high-level language (typically C) Microchip Technology
and then compile, upload and even debug these programs.
Microchip Technology is the company that makes PIC
The PICkit 3 is still a handy device; we haven’t come microcontrollers. In 2016, they bought out Atmel, the makacross many PIC devices that it can’t program. But you will ers of 8-bit AVR microcontrollers (which are at the heart of
probably have to switch to a PICkit 4 or Snap
early Arduino boards such as the Uno). Atmel
programmer if you want to work with the latest By Tim Blythman also made a range of 32-bit ARM-based micro48
Silicon Chip
Australia’s electronics magazine
siliconchip.com.au
controllers (which are in some of the more recent Arduino
boards). In a sense, these compete with Microchip’s MIPSbased PIC32 series.
For a few years now, we have seen some crossover in
features between the 8-bit PIC and AVR families.
We described a then-new AVR part in January 2019, the
ATtiny816 (siliconchip.com.au/Article/11372), and we’re
following up with an article on the latest AVR DA parts
this month (see page 88).
Microchip Technology also produces the MPLAB X IDE
software. It is available for free download, although some
compiler optimisations (to produce smaller and faster code)
are optional extras that you have to pay for. That said, you
can get a lot done with the free version of the software.
MPLAB X IDE
MPLAB X IDE is an evolution of the earlier MPLAB IDE
which dates back to 2001. The PICkit 2 programmer was
introduced in 2005, and many people would have first
come across MPLAB bundled onto a CD-ROM with their
PICkit 2 purchase.
MPLAB X was introduced in 2011 with support for the
Mac and Linux platforms. Now, in 2020, the latest version
of MPLAB X (version 5.40) is the first to drop support for
32-bit host processors (although it can, of course, program
32-bit microcontrollers).
MPLAB X does not provide all features ‘out of the box’.
Instead, compilers and other features are downloaded
and added separately. In fact, it appears that in the future,
new device support will be added using ‘Device Family
Packs’ (DFPs – we speculated on the meaning of DFP in our
ATtiny816 article!).
What it does
The User Guide for MPLAB X notes that it includes the
following features:
• a text editor (which also offers syntax highlighting
and error checking)
• a project manager
• a software simulator
Screen1: the default install directory includes the version
number of MPLAB X, so it can be installed alongside
earlier and later versions. This means you can try a new
version before committing to it.
siliconchip.com.au
• a debugger engine offering breakpoints, single stepping and watch windows
The following items can be added separately to the IDE:
• compilers
• programming frameworks (eg, Microchip’s Harmony
series)
• other tools
As we noted earlier, programmers such as the PICkit devices are also needed to write firmware images to the microcontrollers. If you are experimenting with the AVR128DA48
Curiosity Nano board described in our article in this issue
on the AVR DA family (or one of the other Curiosity Nano
series), the programming function is built into the board,
and no extra hardware is needed, apart from a USB cable.
There is also the MPLAB Xpress Cloud-Based IDE, which
runs in a browser.
Installing MPLAB X
Let’s get started with a basic introduction to the MPLAB
X IDE. We’ll assume you’ve done some programming before, for example, using the Arduino IDE.
The most basic steps involve writing code, compiling
it and programming the resulting HEX file to the device.
Under the Arduino IDE, the last two steps are combined
in the function of the Upload button.
The MPLAB X IDE can be downloaded from www.
microchip.com/mplab/mplab-x-ide
As we mentioned, the latest version at the time of writing (5.40) only supports 64-bit operating systems, so if you
have a 32-bit processor, you may need to work with an older
version instead. Legacy versions can be downloaded from
www.microchip.com/development-tools/pic-and-dspicdownloads-archive
There are minor differences with older versions, but you
should be able to follow along; the big difference is that
5.40 is the first version to support the AVR128DA parts and
thus is needed to work with the Curiosity Nano AVR128DA.
Although MPLAB X supports other operating systems,
much of the other software we use is still tied to Windows,
so we will be using Windows 10 for our guide. But our
Screen2: These options are new for version 5.40. There are
separate options for the IDE (integrated development environment) and IPE (integrated programming environment).
Choose the latter if you only wish to use MPLAB X for
programming HEX files onto chips. The settings shown here
are what we use, with support for 8-bit and 32-bit parts.
Australia’s electronics magazine
January 2021 49
Screen3: MPLAB X also installs drivers for devices such
as programmers like the PICkit 4 and the programming
interface on the Curiosity Nano AVR128DA.
experience is that on Mac and Linux, it works in much
the same way.
The download for MPLAB X v5.40 is around 1GB and
once installed, can take up to around 11GB. Installation
is quite straightforward, and for the most part, the default
options are fine (Screen1). It takes between about 10 minutes and an hour, depending on how fast your computer is.
If you only wish to use MPLAB X for programming devices, then you might only want to install the IPE (integrated programming environment). There’s also the option
to select whether you want support for 8-bit, 16-bit or 32bit devices; you will need 8-bit support for the AVR DA
parts (see Screen2).
The installer will also ask for permission to install some
drivers (see Screen3). These are for devices such as programmers, so it’s a good idea to install them now too.
When the MPLAB X installation is finished, you will also
be prompted to install other items that you might typically
need, such as a compiler – see Screen4.
You will likely need to install at least one compiler, but
if you want to install anything but the latest version, you’ll
have to download them manually from siliconchip.com.
au/link/ab4v
The compilers are called XC8 (for 8-bit devices), XC16
and XC32 (for 32-bit devices like PIC32s).
We currently use XC32 version v2.10 for our PIC32 projects, although some older projects use version v1.33. Also,
the procedure for creating a “CFUNCTION” for a Micro-
Screen5: the Free License for the XC8 Compiler works
quite well. If you need the features of one of the Pro
licenses (for example, more aggressive code optimisation),
then it can be applied later.
50
Silicon Chip
Screen4: to make proper use of MPLAB X, you need a
compiler, so you should leave the top option checked. You
can install compilers separately later, if required.
mite only works with this older version. This is due to
changes in the way some of the peripheral libraries work
within the compiler.
For 8-bit microcontrollers, we have previously used XC8
version v2.00. You might want to install one of these if you
wish to modify some of our project code.
The process here applies to version v2.20 of XC8, but
other versions (and other compilers) should be fairly similar. If you want to build the code for the AVRDA family of
chips, then you will need to install at least v2.20 of XC8.
The first question you are asked when installing an XC
compiler is about the licence type, as shown in Screen5.
Initially, at least, the Free option is fine. A paid licence
can be applied later if you need compiler optimisations
(this means that, in general, your programs will be smaller and run faster).
Then move onto the installation path; the default option
is usually a good choice (Screen6), as the installer organises the different versions into folders, so it’s easy to check
what versions are installed.
The final step also relates to the licence. The Host ID (used
for node-locked licences) is shown (see Screen7). Again,
Screen6: the XC8 Compiler can be installed independently
from MPLAB X, and different versions of it can be installed
simultaneously. We sometimes use an older version of the
XC32 compiler (for PIC32 parts) as it has a different set of
libraries.
Australia’s electronics magazine
siliconchip.com.au
Screen7: if you want to try the Pro license for XC8, there is
a 60-day free trial. The easiest way to activate it is to rerun
the installer and click the option shown here.
for the free licence, you can simply click Next.
At this stage, we have enough software installed to start
compiling code, but let’s take a quick tour first.
MPLAB X
We’ll use the AVR128DA48 Curiosity Nano development board described in the accompanying article as an
example. If you have one of these, plug it in now so that
the software can recognise it.
Open MPLAB X and choose New Project… from the File
menu. The next step is to select a project type; we usually
select “Standalone Project” (see Screen8). The other options are generally used to import existing projects from
other programs. If you have installed the Harmony or MCC
frameworks, then options for these will also appear.
Harmony and the MPLAB Code Configurator (MCC) are
the programming frameworks noted earlier; it is not necessary to install these to work with the AVR128DA48 Curiosity Nano. However, they may come in handy if you are
working with some complex peripherals, especially USB.
The next step is to choose the target part. For example, for the AVR128DA48 Curiosity Nano, the part will be
AVR128DA48, because this board has the 48-pin variant.
It’s possible to change this setting later (mid-project). For
example, you may wish to port the code to a device from the
same family with more pins, or even to a different device.
Screen9: after creating a new project and adding a “main.c”
file, you are presented with panels full of information.
Project settings and properties can be found by right-clicking
the project name at the top left and selecting “Properties”.
siliconchip.com.au
Screen8: as well as creating a Standalone project, you
can also import Atmel Studio projects. If you have other
frameworks (such as Harmony or MCC) installed, they
appear as options here.
On this tab, it’s also possible to select the programming
tool. You should see the Curiosity Nano in the drop-down
list. If you don’t have one of these, you can select “Simulator” or “No Tool”.
The window will jump forward a few steps to allow a
compiler to be chosen. Your only choice for the AVR128DA
will be XC8 V2.20 or later (or its assembler, “pic-as”).
Finally, you can choose a project name and location. We
went with “AVR128DA48_blank”.
The project is now created, but will (at a minimum)
need to have at least one source code file. Right-click on
the “Source Files” and click New -> avr-main.c. A file will
appear in the main window and also at left. Your screen
should now look like Screen9.
Navigating the IDE
The small window at top left allows you to navigate between projects and also individual files within a project.
Below this, at the left, is the Dashboard. It shows important project information. Particularly handy are the Data
and Program memory space bar graphs, which allow you to
keep track of these resources as your code expands.
At top right is the editor. If you have multiple files open,
they will be shown by tabs along the top. The editor has
the expected features like find and replace, but also syntax
Screen10: the Project Properties window contains settings
that are only changed rarely after the project is created.
You might use the option at top right to modify a project to
use a different part, for example, if you need more I/O pins
and want to change to a larger member of the same family.
Australia’s electronics magazine
January 2021 51
highlighting and autocompletion.
At lower right is the output window. Various stages of
the development process are handled by different utilities (under the control of MPLAB X). For example, during
compilation and upload, progress and warnings/errors (if
any) are shown here.
Of course, the windows can be moved around as needed,
but we find that the defaults work quite well.
The button with the green arrow coming out of the chip
is for reading a device’s memory; typically this would be
used to export the contents of flash memory to a HEX file,
although this is usually not needed if you have compiled
your own code. It might be handy, though, if your code
writes data to flash (eg, its configuration) and you want to
see what changes it has made.
The button with the blue circular arrow is used to toggle the programmer’s device reset line, for example, if you
wish to reset or disable the attached microcontroller during testing and debugging.
The final button is used for hardware debugging. If you
click this, your code is compiled with options allowing
it to communicate with the programming tool, and is uploaded to the device. The MPLAB X IDE then switches to
debugging mode, and some more buttons appear to control this. We explain the debugging process in more detail
in the article on the AVR128DA, which starts on page 82.
Handy hints
MPLAB Xpress IDE
Screen11: these buttons along the top of the MPLAB X
window are for compiling and deploying your project to
microcontroller hardware. The rightmost button initiates a
debugging session.
We can’t possibly detail all the features of MPLAB X, but
we’ll briefly summarise those we use the most. It might pay
to come back to this section while you’re working with the
Curiosity Nano board.
The main project properties can be opened by rightclicking on the project name in the Projects window (see
Screen10). Many options from the initial project setup can
be changed here. In fact, multiple configurations can be
created, perhaps to target a variety of different processors
or to help to port the project between compiler versions.
The programmer can be changed too; it’s the first item
under “Conf: [default]”. The “PKOB nano” is the programmer built into the Curiosity Nano (PKOB is short for PICkit
On Board).
You may have more need to tweak these settings when
working with a standalone programmer.
MPLAB X also includes an easy way to make a copy of
a project. This can be handy if you don’t have some other
form of version control in use, or you wish to use one project as the basis for another different project.
Simply right-click on the project name in the Projects
window and select “Copy...” Then supply a new name
and click “Copy”.
Just below the main menu items are commonly used
tools, as shown in Screen11.
The hammer icon builds (compiles) the project. Helpfully, the project name is shown, so you know which project you are building. This is handy if you have multiple
projects open.
The hammer and brush icon ‘cleans’ and builds. Usually, the build process only updates files that have changed
since the last build. In contrast, a ‘clean and build’ ensures
other changes like configuration settings changes are properly propagated through the compile process.
For small projects, either process takes only a few seconds, so we find that we simply do a ‘clean and build’
most of the time.
The next two buttons, labelled “Run” and “Make and
Program Device”, perform the same function in most cases. The Run button can also be used to start the simulator
for those devices that support it. Currently, this is only
some PIC devices, so the AVR128DA is not supported by
the simulator.
52
Silicon Chip
It’s also worth noting that there is a cloud-based version
of MPLAB X, called MPLAB Xpress IDE. It is not a replacement for the full IDE, but is a quick and easy way to have
a look around at the platform’s features – see Screen 12.
You can access it at the following link: https://www.
microchip.com/mplab/mplab-xpress
You can create a project, build it and even download a
hex file. There’s also an option to export an MPLAB X project to work with the full IDE.
Conclusion
We’ve used MPLAB X for many years now, and it has
improved over time. It’s especially handy that MPLAB X
can now work with AVR parts as well as PICs, especially
with in-circuit debugging (ICD).
The latest version of the IDE doesn’t bring much in the
way of new features for those that are familiar with using
it to work with PIC microcontrollers. Indeed, many of the
older versions are entirely adequate for coding, debugging
and programming older PICs.
But it is evolving to work with more microcontrollers, and
SC
these new AVR parts spread its reach even further.
Screen12: the MPLAB Xpress IDE is an online IDE which
allows you to export projects and compile HEX files. So
it’s a good way to test out some of the features of MPLAB X
without having to install it first.
Australia’s electronics magazine
siliconchip.com.au
|