This is only a preview of the July 2020 issue of Practical Electronics. You can view 0 of the 72 pages in the full issue. Articles in this series:
|
PIC n’Mix
Mike Hibbett’s column for PIC project enlightenment and related topics
Part 1: Introducing the PIC18 family
I
n this month’s article we will
introduce a new series that dives into
the PIC18 microcontroller family, looking at the device’s capabilities, software
development tools and building up our
own development board. We will explore
how designing a project based around
Microchip’s PIC processors complements the use of an Arduino platform,
and how there are ‘for’ and ‘against’ arguments related to each development
approach. These articles will be aimed
at people already familiar with basic
54
microcontroller development, on platforms like the Arduino. While we will
attach a variety of peripherals to our
processor board, the key focus is going
to be on using the PIC itself.
Before we dive in, let’s look at the range
of processors available from Microchip.
Although you rarely find PIC processors
in hobbyist development boards, they do
find their way into some mission-critical
applications. We’ve seen them in automotive systems and data-centre power
supplies, where high reliability and long
availability of parts is essential. Microchip
keep their parts available for purchase so
long as there is customer demand – the
original PIC16C63, which we used back
in the early 1990s, is still available – in
stock with Digikey!
The Microchip PIC families
Microchip have seven families of processors, not including those acquired
from Atmel: PIC10, PIC12, PIC16, PIC18,
PIC24, PIC33 and PIC32. These families
are grouped by the type of processor
Practical Electronics | July | 2020
Fig.1. PIC processors come in a
variety of form factors – including, in
the past, UV erasable.
core, with the first four families being
8-bit processors, PIC24 and PIC33 are 16bit; and the PIC32 is a 32-bit processor.
These are slightly confusing names, so
from the get-go, do not make the understandable mistake of thinking that PIC18
(or especially PIC16) PICs are anything
other than 8-bit-based designs.
Each family of processors has a wide
range of on-chip peripheral options,
memory sizes and package types. You
can see some examples of the package
varieties in Fig.1, taken from the
PIC n’ Mix lab stock. That little
collection includes a windowed
UV-erasable PIC16C63, which was
purchased in 1996!
The PIC10 and PIC12 families are
low pin count (6 and 8 pins respectively) and are designed for very
simple applications. Despite their
minimal capabilities – a few bytes
of RAM, and a few hundred bytes
of code memory – these devices can
still be programmed in a high-level language such as ‘C’. The author
used one in an industrial application recently.
The PIC16 family are more capable and have a much wider range
of peripheral options and packages.
Readers of the magazine will be familiar with the PIC16F877, a 40-pin
device that has been a popular choice
of many authors over the years.
The PIC18 family represents the
peak of 8-bit processor performance
and variety, with their key differentiator being larger memory availability,
both SRAM and Flash. For commercial applications the PIC16 family
is still relevant when low cost or
simply continuity of an old design
is relevant, but the PIC18 is our go-to
family these days, especially when
designing easy-to-assemble projects.
Fig.2 (left) PIC family features
(Source: Microchip Technologies).
Practical Electronics | July | 2020
The PIC24 and PIC33 devices are 16-bit
processors and intended for applications
requiring DSP (digital signal processing) capabilities, such as motor or power
supply control. They support more complex peripheral interfaces but are really
better suited to specific use-cases; they
are not an ideal choice for a general-purpose development platform.
We spoke to Microchip recently to
enquire about the number of processor
variants they had. Our local sales manager
came back, saying he gave up counting
after noting 17,000 variants. There are a
lot to choose from!
Fig.2 shows a selection table for the
8-bit processor families. It’s interesting to
see the distribution of available features
across the range of devices; you wonder
whether the decision of which peripherals to include is sometimes driven by
large-volume customer requests.
For the purpose of this series of articles we wanted to introduce a highly
flexible processor that could be used
in many different projects, while still
being easy to use. Perhaps not for absolute beginners, but for hobbyists keen to
move on from Arduino-based projects,
to learn more about the processor itself.
With that in mind, we decided to use
a PIC18 processor. Within this family
there are lots of hobbyist-friendly parts
(ie, in easy-to-use packages) which offer
plenty of memory and a good selection
of interesting peripherals. These parts
are easy to source from the usual electronics distributors.
Meet the family
We liked the look of the PIC18(L)FxxK42
‘sub-family’ – 128KB of Flash, 8KB of
RAM and a great range of peripherals.
It also has the CLC (configurable logic
cell) peripheral, which is a peripheral
we have not yet played with, so this article series will give us the opportunity
to explore it. The PIC18 family are optimised for use with the ‘C’ language,
without being complex processors to
set up. (The PIC32 for example is a
very complicated device and requires
a lot of engineering experience to get to
grips with.) Writing software in assembly language is still possible, for those
who really enjoy a challenge.
There are ten devices available within
the PIC18(L)FxxK42 sub-family to choose
from. We used the Microchip selection
tool (https://bit.ly/pe-jul20-pnm) to compare the features, as shown in Fig.3.
This is an abridged table; there are over
70 parameters to choose from when refining your search. With just ten parts
available we quickly homed in on our
preferred part, the PIC18F47K42 in a
40-pin DIP package. This device’s feature list is impressive:
55
Fig.3 PIC18(L)FxxK42 family selection.
PIC18 F47 K 42
VP P / MCL R / R E 3
1
40
R B 7 / ICSPDA T
R A 0
2
39
R B 6 / ICSPCL K
R A 1
3
38
R B 5
R A 2
4
37
R B 4
R A 3
5
36
R B 3
R A 4
6
35
R B 2
R A 5
7
34
R B 1
R E 0
8
33
R B 0
R E 1
9
32
VD D
R E 2
10
31
VSS
VD D
11
30
R D7
VSS
12
2 9
R D6
R A 7
13
2 8
R D5
R A 6
14
2 7
R D4
R C0
2 5
2 6
R C7
R C1
16
2 5
R C6
R C2
17
2 4
R C5
R C3
18
2 3
R C4
R D0
19
2 2
R D3
R D1
2 0
2 1
R D2
Fig.4 The PIC18F47K42 pinout.
128KB Flash
8KB SRAM
1KB EEPROM
35-channel 12-bit ADC
2 analogue comparators
DAC (which can be connected to the
comparators, that’s useful)
4 PWMs
2 UARTs
2 I2C buses
SPI bus
Multiple timers
36 GPIO pins.
And that is just the key peripherals, there
are a dozen other interesting features too
– all that, for the price of a bottle of Coke.
56
There are some peripherals
missing on this device that we
would have liked to play with,
such as I2S for high-quality audio
output and a USB peripheral
(although we will add USB interfacing on our development
board) but we will pick those up
in future articles. The pin-out
of the IC can be seen in Fig.4,
and the block diagram of the
internals in Fig.5.
The device has two internal
oscillators, a high-speed one
to provide the 64MHz system
clock, and a 31kHz low-frequency clock designed to provide
operation of the device under
very-low-current consumption.
The high-frequency oscillator is
factory calibrated and is accurate
enough to provide high-speed
UART communications – removing the need for any external
crystals. The block diagram also
shows the code access and data
accesses are on different buses
(the thick grey lines) – this processor is a Harvard Architecture
CPU. Having the two buses separated enables the CPU to be
more efficient, allowing, for example, the DMA peripheral to
move data from, say, the UART
into memory while the CPU is
executing other tasks. It’s like
having a second processor in
the chip – nice!
The processor also has a feature called PPS (Peripheral Pin
Select) that allows most of the
peripherals to be mapped to a
GPIO pin of your choice – which
can significantly simplify your
PCB designs.
Take your PIC
So why would you want to build
a microcontroller project rather
than using something simple and
off-the-shelf, like an Arduino or
even a Raspberry Pi? Arduinos come in
all shape and sizes and are very cheap.
We are certainly not ‘knocking’ these
platforms, they are very useful and very
simple to use, but there are some really
good reasons to go with a bottom-up
custom design:
1. You will learn more about microcontrollers. The Arduino platform is
excellent, but it abstracts the low-level
details away, leaving you with more
of a ‘Lego assembly’ process, so you
learn less.
2. You may have size constraints. If you
want to build something small or light,
you can build exactly what is required,
and no more.
3. You may have power constraints. Microcontrollers can operate down to a
few microamps or less; an Arduino
platform comes with extra features
you do not need in the final product,
like USB interfaces and power LEDs,
which can increase this minimum current consumption a thousand fold.
4. Your design may be complicated. Arduino platforms do not have great
debugging capabilities, which will
make tracking down hard-to-reproduce software issues very difficult.
5. You may be designing an actual product.
Microchip microcontrollers will always
be available, even decades from now. If
you design with an unusual Arduino
development board, you may well find
the manufacturer obsoleting the board
with no notice – this is not uncommon.
Our development board is going to
provide several on-board features for
experimentation:
A PC serial communications interface,
using an on-board UART-to-USB converter chip, the MCP2221A.
A header connector for the ESP-01
Wi-Fi interface.
A header for an SD-Media card module.
Three-pin headers for servomotor drive.
A header for a colour touchscreen LCD.
FET power switches for external
device control.
A PICkit 4 header for programming
and debugging.
Interfaces for I2C and SPI bus devices.
Two configurable op-amps.
Loads of analogue input and digital
I/O headers.
One of the key questions we will have
to answer along the way is whether we
will support 5V or 3.3V external devices,
or both. The design shall be, however,
optimised for low-power operation, if
desired. Our board will use through-hole
parts as much as possible to minimise
the soldering complexity.
PIC Internals
Let’s go back to the processor itself and
look at the key building blocks within
the device.
CPU
The CPU is the key feature that distinguishes the different product families, and
the PIC18 family is an 8-bit processor but
has a 16-bit instruction size, allowing for
many more instructions compared with
the PIC16 or smaller devices. It can process
most instructions in 62.5ns, allowing for
up to 16 million instructions per second.
There is a hardware multiply instruction
too, along with low-power modes that can
bring the processor down to an active current consumption of just a few microamps.
Practical Electronics | July | 2020
CPU
12 8 K B
Flas h
8 K B R A M
Ports
1K B E E PR OM
Perip h erals
GPIO
OSC1
Internal
osci llator b lock
OSC2
L FINTOSC
osci llator
Osci llator
st art-up timer
6 4MH z
osci llator
Pow er-on
R ese t
SOSCI
SOSCO
MCL R
communication over long distances. We will use one of these
UARTs to communicate with a PC over a USB interface converter, and the other to communicate with the plug-in Wi-Fi
module. The UARTs can, however, be re-mapped to other
GPIO pins under software control.
36 GPIO (general-purpose input/output) pins are available on
the 40-pin package we have selected. However, not all of these
pins are free for general-purpose use because some GPIO pins
are required for any of the other peripheral features we use.
These are digital signals and will output 0V or 3.3V under
program control.
Pow er-up
timer
W W DT
Sing le-su p p ly
p rog ramming
B row n-out
R ese t
In-circuit
d eb ug g ing
Fail-sa fe
clock momitor
Precis ion
b and -g ap
reference
Fig.5. PIC18F47K42 block diagram.
Flash
128KB of non-volatile memory is available on chip. This
memory is normally used for program storage, but it can also
be used for fixed data or constants that needs to be preserved
when power is removed. The memory is typically programmed
through the debug interface using a PICKit 4 debugger/programmer, but the CPU can write to this memory too, allowing
for software updates to be performed and controlled by the
processor itself. With the addition of a Wi-Fi module, this
means we can support remote firmware update – if we write
the software to do it!
SRAM
8KB of SRAM is available on chip. This is typically used for
program variables, and the data will be lost when power is removed. For a small, embedded project, 8KB is a huge amount
of memory.
EEPROM
1KB of non-volatile EEPROM is available, again within the
processor itself. EEPROM memory is more easily writeable
than Flash memory, and is used for unique device configuration data, such as Wi-Fi credentials, or calibration data. This
memory can be written via the debug interface or by the processor itself.
ADC
35 of the GPIO pins can also be configured as analogue input
signals to the single ADC converter present on the chip. The
ADC has a resolution of 12 bits, which means it can resolve
input signal voltage changes down to 1mV, at speeds of up to
1MHz sample rates. We will make a number of the ADC channels available via headers and connect some configurable op
amps to allow easy connection of microphones or other audio
sources. ADCs are used for a variety of uses (hence, the large
number of input channels offered.) We will use one channel to
measure the input power supply voltage, to enable the monitoring of battery level.
Next time
In our next article we will look at the software tools available
for working with the processor. These include MPLAB-X, an
integrated development environment, the XC8 ‘C’ language
compiler and the MCC Code Configurator tool, which provides ‘ready-to-go’ example code for the different peripherals
within the processor.
ESR Electronic Components Ltd
All of our stock is RoHS compliant and CE
approved. Visit our well stocked shop for
all of your requirements or order on-line.
We can help and advise with your enquiry,
from design to construction.
I2C
Two I2C buses are provided on the device. An I2C bus is a
two-wire communication interface intended to provide a communication path between two or more devices, either on the
same PCB or a short distance away, such as within an enclosure. Multiple ICs can be connected to a single bus, but the data
speeds are low, below 1MHz typically. ICs such as temperature
sensors, accelerometers and gyros typically use this interface.
3D Printing • Cable • CCTV • Connectors • Components •
Enclosures • Fans • Fuses • Hardware • Lamps • LED’s •
Leads • Loudspeakers • Panel Meters • PCB Production •
Power Supplies • Relays • Resistors • Semiconductors •
Soldering Irons • Switches • Test Equipment • Transformers
and so much more…
Monday to Friday 08:30 - 17.00, Saturday 08:30 - 15:30
SPI
A single SPI bus is provided on the device. The SPI bus is a
three-wire interface, with a fourth wire used as a ‘chip-select’ to
allow multiple devices to communicate on the same bus. The
SPI interface runs at up to 16MHz and is used for high-speed
interfaces, such as LCD displays or communication modules.
UART
Two UART interfaces are provided; the UART provides classic
serial communication capabilities and can be used, in conjunction with an RS232 or RS485 interface IC, to provide wired
Practical Electronics | July | 2020
Station Road
Cullercoats
North Shields
Tyne & Wear
NE30 4PQ
Tel: 0191 2514363 sales<at>esr.co.uk www.esr.co.uk
57
|