Discover the PIC
For the ultimate in low-cost embedded applications, the PIC microcontroller takes some beating. Mike Bedford shows you how to program it.
For the ultimate in low-cost embedded applications, the PIC microcontroller takes some beating. Mike Bedford shows you how to program it.
Wthe 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 40p, and this drops to as little as 25p in high volumes.
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 like memory 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-, 16- and 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 £23 (try Farnell, https://uk.farnell.com), 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 nonith
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 non-volatile 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/ mplab-xpress and clicking ‘Get Started NOW!’. As you’ll discover, however, although part of the suggested benefit of the cloud-based 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 nonvolatile 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 nonvolatile program memory is being written to. Some SBCS that are used with an IDE on a PC run their code automatically as soon as it’s been downloaded. With the Curiosity, though, you’ll have to unplug it from your PC’S USB port and reconnect it. This, of course, is a perfect analogy of what happens in real embedded applications – the code runs as soon as power is applied.
This demonstration code has several so-called labs, which are selected by pressing and releasing the pushbutton labelled S1, which is near the bottom left of the board, to the left of the mtouch button. To start, none of the labs will be running so you’ll need to press and release S1 to select Lab 1, and you’ll subsequently do the same to move from each lab to the next, cycling back to Lab 1 after Lab 10. You’ll be able to see the output of each lab by looking at the four red LEDS (D4, D5, D6 and D7) to the left of the potentiometer, and the action of Labs 1, 2 and 3 will be obvious.
Lab 4 will only be exercised properly if you try adjusting the potentiometer and you’ll then notice that the LEDS display a binary number related to the position of the potentiometer. In Lab 5 the LEDS rotate at a speed depending on the potentiometer position, and in Lab 6 the brightness of the left-most LED is controlled by the potentiometer. We’re not going to get embroiled in the other labs because the descriptions are a bit more involved. However, if you do want to read up on them, click EXAMPLES again and on the line for the
Curiosity - PICF16F1619 example, click the red title.
Programming Curiosity
The primary language for programming PIC chips – certainly the more powerful ones – is C, but it isn’t our intention here to deviate from our topic of PIC programming by providing any instructions on coding in C. This being the case, if you’re not sufficiently au fait with this language, you’ll need to get up to speed, although at least the first of our suggested programming exercises are so trivial that not being proficient in C is no deterrent.
To help get a better feel for the workings of MPLAB Xpress, we’ll make an extremely simple change to
Curiosity - PIC16F1619 so that, in Lab 2, the LED flashes at a different speed. First of all, though, let’s just take a look at what’s displayed on screen with the sample project loaded.
At the top left is an area labelled Project that shows, hierarchically, all the source and headers files associated with the project. Navigating around this is intuitive. You’ll also notice, in the largest area of the screen, that the source file main.c is already open for you to view and edit. If you do take a quick look at it, though, you’ll see that its function is nothing more than to increment the lab number as pushbutton S1 is pressed, and to call the appropriate function for the selected lab.
You’ll also notice that, since we want to alter the speed of flashing in Lab 2, the function we need is one called Blink. You’ll find the code for this under Curiosity PIC16F1619> Source Files>labs>lab02_blink in the tree structure of the project, so click Blink.c to open it in a new tab in the main area of the screen. A one-line change is all we need to make. That line is numbered 58 and is a macro definition for a constant called FLAG_ COUNTER_MAX , and which is defined as 3 to give a flash rate of 1.5 seconds. Try changing it to 1 to give a faster half-second flash rate, or to 20 to change the rate to 10 seconds. If you want more fine-tuning over the speed you’ll need to delve elsewhere, and we leave
that as an exercise for you when you’ve finished working through our other examples.
Next up we’ll suggest another way in which you can alter the code and, while it’s still simple, at least it involves adding a few instructions instead of just changing a single line. Our change is going to be to Lab 2 again, and we’ll still see LEDS flashing so, if you’ve changed FLAG_COUNTER_MAX to a large value, we suggest, first of all, that you change it back to 2 or 3 so you don’t have to wait too long to see things change. Next, go down to line 85 where you’ll find a function call to toggle LED D4 . What we’re suggesting is to add three lines after the one containing Led_d4_toggle(); with the instructions Led_d5_toggle(); , LED_D6_ Toggle(); and Led_d7_toggle(); . It’s fairly obvious what should happen, but go ahead anyway and try it out. Hopefully all four LEDS will now flash instead of just the one.
Rather than jump straight in at this point by creating your own project from scratch, we recommend that you continue in this incremental approach of amending the
Curiosity - PIC16F1619 project in ever more ambitious ways. First of all continue modifying the labs to change their operation in various ways. Then, before moving on to a completely new project, how about adding a brand new lab, Lab 11, that performs totally new function. We’re sure you’ll have ideas aplenty.
Before moving on to our next topic of assembly programming, we should tell you some news about MPLA Xpress that Microchip was keen to tell us. There’s an improved version of MPLAB Xpress coming soon. Microchip is providing a Github interface for its community of users to store, collaborate and share their projects, and are moving the cloud services to scale for larger bandwidth. Apparently it’s already started to release examples there and we’ll see more of that in the time to come. It’s possible, therefore, that some of the ways of working with the new IDE will differ slightly from the one we describe here, although we don’t anticipate major changes and, after all, our primary aim has been to provide you with an overview of programming PIC chips, not a detailed tutorial on MPLAB Xpress.
PIC assembly
Assembly programming might sound like something from the pages of a history book but this isn’t necessarily true in the world of PICS. C code is rarely going to be as compact or efficient as assembly code, although this is often considered a price worth paying for convenience of high-level language programming with more powerful processors. However, if you’ve writing code for a chip with just a few hundred bytes of program memory, you might feel the need to bite the bullet and abandon C in favour of assembly code for smaller and faster code. We say a few words about the PIC 16 series instruction set in the box (above), but here we’ll take a quick look at how to program in assembly in the MPLAB Xpress IDE.
To use the PIC assembler instead of C, go to Project Properties in the File menu and then, under Compiler Toolchain, select mpasm. Now, whenever you create a new file it’ll be given a suitable extension and it’ll be passed over to the assembler, rather than the C compiler, when you come to build the project and program the PIC chip.
You might also be interested to learn that the XC8 compiler, which is MPLAB Xpress’ default C compiler for eight-bit PICS, enables you to use inline assembly code in a C program. This can be achieved by placing your block of assembly code between a #asm and a #endasm . However, for compatibility with the latest standards, this is now discouraged and instead, you’re advised to use the asm statement. Unfortunately, however, this makes the code look more untidy, and it’s more laborious to write, with each line of assembler looking rather like this example: asm( “DECFSZ COUNT” ); . Use inline assembler with caution, though, because altering the contents of a register could cause the surrounding C code to fail.