Raspberry Pi
How to program the $1 chip
With the entry-level boards in Raspberry Pi family costing less than $5, we’re fully conversant with the low cost of computing power. Yet if you’ve not encountered them before, the price of chips in the PIC microcontroller range from Microchip Technology might still be surprising. The fact is that an entry-level PIC chip can be bought for about 80 cents. Remarkable!
Needless to say, these chips don’t offer a lot of processing power – in fact many of them wouldn’t even be able to support an operating system. Yet in many embedded applications – that’s where the processor works invisibly in the background – the requirement for number crunching is minimal, and an operating system is unnecessary. What’s more, as we’re about to see, those cheap chips contain a lot of circuitry that you might expect to be external to the processor.
Here we introduce you to PIC microcontroller chips and show you how to program them using the MPLAB Xpress IDE and a Curiosity development board. Although real-world applications of PIC devices involve using just the chip, which you’d incorporate into your own circuit board, using a ready-made board for learning to develop code offers a simple, ready-to-go solution at low-cost.
A key thing to bear in mind about PIC chips is that they’re microcontrollers, not microprocessors. This means that they’re effectively microprocessors with the addition of on-chip RAM, flash storage, non-volatile program memory, and input/output circuitry for interfacing to real-world devices. For this reason they can be used in embedded applications with a minimum of external circuitry.
Daddy or chips?
According to manufacturer Microchip Technology, there are several official families of PIC, but to cut a long story short, eight-, 16and 32-bit architectures are available. Within each of these broad categories, specific products differ mostly in the amount of each type of on-chip memory and the number and type of I/O pins.
Some top-end chips, which are intended for signal processing applications, have the addition of DSP instructions. However, at the other end of the spectrum are chips that are aimed at applications where low cost and a small size are the most important considerations. For example, the PIC10F200-I/OT is an eight-bit microcontroller that runs at a clock speed of 4MHz. It has 256 bytes of program memory, 16 bytes of RAM, and four digital I/O pins. The chip is tiny, having just six or eight pins and, depending on the package type, measures as little as 2.8x2.9mm.
Development kit
If you’re using a PIC chip in a real application, the most common way of using it would be to incorporate it into your own custom circuit board, as opposed to using an off-the-shelf SBC. This way you can minimise space and cost. However, so you don’t have to carry out any electronic design or construction at this early stage – we’re basing this tutorial on a development kit from Microchip Technology.
Called DM164137 Curiosity, and costing about $40 (try digikey. com.au), it has a socket into which you can plug any eight-bit PIC with eight, 14 or 20 pins, although it’s supplied with a PIC16F1619 already fitted. However, if you do want to use a different PIC chip, code is portable between eight-bit devices, often with no change or minimal alteration. The PIC16F1619 is an eight-bit chip which is clocked at 32MHz, it has 8K of 14-bit program memory, 1K of RAM, 128 bytes of non-volatile user memory and several timers. Turning to interfacing options, it has 18 I/O pins which, in addition to standard digital input and output, support I²C, LINbus, SPI and UART/ USART interfaces, there are two PWMs (Pulse Width Modulators), there’s a 12-channel 10-bit ADC for analogue inputs, and an 8-bit DAC for analogue output.
There are several advantages of using the development kit. First it has a USB port so you can connect it to a PC. When it’s used with one of the microchip integrated development environments, you can program the on-board PIC by downloading your code into the chips’ non-volatile program memory, and it also provides debugging facilities.
Second, it has various devices connected to the PIC’s I/O pins. In particular, it has a mechanical push button (actually there are two, but one is specifically to reset the board and can’t be read by software), a capacitive “mTouch” touch-sensitive switch, a potentiometer that feeds a user-controlled variable voltage to the PIC, and four LEDs (in addition to a couple of dedicated ones such as a power indicator). In addition, there are also headers that provide a means of connecting to any of the PIC’s pins, thereby enabling other I/O options to be used. There are also two so-called footprints, which are groups of holes in the board to which the user can solder modules or connectors. One of these supports a Bluetooth module, while the other, the mikroBUS footprint, allows one of a variety of Click Board sensors to be used. A huge selection of these boards is available from Mikroe (www. mikroe.com).
Feeling curious?
Now you know what Curiosity does, it’s time to try it out for yourself. We’ll be referring to where on the board you can find various LEDs, pushbuttons and so on. When we do that, our instructions will only make sense if you’re holding the board with
the side containing the majority of the components facing you, and with the words ‘Microchip’ and ‘Curiosity’ the right way up.
Curiosity is powered via its mini USB port, for which you’ll need a USB A to mini USB B cable. Note that the cable isn’t supplied with the board, and because the on-board socket is different from the micro USB socket on most Android phones, you can’t use a common USB to micro USB cable. The board is supplied with a demonstration program already loaded into the PIC chip’s nonvolatile program memory, and this will run as soon as you connect the board to your PC and hence supply it with power. So, having plugged it in, check that it’s working by adjusting the potentiometer, POT1 – that’s the rotary control near the bottom right corner of the board – and, all being well, it’ll control the brightness of one of the LEDs to the left of the potentiometer.
Try also putting your finger on the touch-sensitive m-Touch button, which is near the bottom left. This should cause another of the LEDs near the potentiometer to illuminate. Once you know that your Curiosity is working, it’s time to get to grips with the development environment.
Microchip Technology’s MPLAB X integrated development environment is a fully featured tool which is freely available for Linux. However, for first-time users, Microchip suggests using its MPLAB Xpress IDE, which is also free, cloud-based, and simplified compared to MPLAB X. In accordance with this recommendation, this is the route we took, although much of our advice will apply if you chose to use the locally-installed MPLAB X IDE.
Get started by going to www. microchip.com/mplab/mplabxpress and clicking ‘Get Started NOW!’. As you’ll discover, however, although part of the suggested benefit of the cloudbased IDE is that it runs in a browser and therefore doesn’t need installation, this is something of a simplification. In reality, a Java utility called the USB Bridge has to be downloaded and executed – and MPLAB Xpress will prompt you to do that at the appropriate time.
You might also need to install the latest version of the Java Runtime Environment, though, and because the USB Bridge is actually a .jnlp (Java Network
Launching Protocol) file, you’ll also need to install IcedTea. And finally, before we really get started, it’s a good idea to register with Microchip and login to MPLAB Xpress. This will enable you to save your projects.
As your first exercise, we suggest that you reprogram the PIC16F1619 on your Curiosity Board with a different ready-made program. To do that, click EXAMPLES in the toolbar and you’ll be taken to a page on the Microchip website containing MPLAB Xpress examples. Under Board select Curiosity and search for PIC16F1619 under Device so you’ll only see examples that are appropriate for your hardware.
One you’ll see, and which we recommend, is called Curiosity - PICF16F1619 and it has a
Microchip logo against it, indicating that is was provided by Microchip, as opposed to the user community. Click IDE to the right of that line and you’ll be transported back to MPLAB Xpress, but now with that example code loaded into the IDE. Now, click the Make and Program Device icon in the toolbar, which looks like a PC screen with a green arrow pointing down to a chip. This causes the code to be compiled and loaded into the PIC chip’s non-volatile program memory.
You can follow progress in the Debugger Console window and you might also notice that one of the red LEDs to the left of the potentiometer on the board flashing briefly – this indicates that the on-board non-volatile