APC Australia

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 encountere­d them before, the price of chips in the PIC microcontr­oller 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 applicatio­ns – that’s where the processor works invisibly in the background – the requiremen­t for number crunching is minimal, and an operating system is unnecessar­y. 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 microcontr­oller chips and show you how to program them using the MPLAB Xpress IDE and a Curiosity developmen­t board. Although real-world applicatio­ns of PIC devices involve using just the chip, which you’d incorporat­e 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 microcontr­ollers, not microproce­ssors. This means that they’re effectivel­y microproce­ssors with the addition of on-chip RAM, flash storage, non-volatile program memory, and input/output circuitry for interfacin­g to real-world devices. For this reason they can be used in embedded applicatio­ns with a minimum of external circuitry.

Daddy or chips?

According to manufactur­er Microchip Technology, there are several official families of PIC, but to cut a long story short, eight-, 16and 32-bit architectu­res 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 applicatio­ns, have the addition of DSP instructio­ns. However, at the other end of the spectrum are chips that are aimed at applicatio­ns where low cost and a small size are the most important considerat­ions. For example, the PIC10F200-I/OT is an eight-bit microcontr­oller 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.

Developmen­t kit

If you’re using a PIC chip in a real applicatio­n, the most common way of using it would be to incorporat­e 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 constructi­on at this early stage – we’re basing this tutorial on a developmen­t 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 interfacin­g 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 developmen­t 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 developmen­t environmen­ts, you can program the on-board PIC by downloadin­g 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 specifical­ly to reset the board and can’t be read by software), a capacitive “mTouch” touch-sensitive switch, a potentiome­ter 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, pushbutton­s and so on. When we do that, our instructio­ns 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 demonstrat­ion program already loaded into the PIC chip’s nonvolatil­e 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 potentiome­ter, 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 potentiome­ter.

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 potentiome­ter to illuminate. Once you know that your Curiosity is working, it’s time to get to grips with the developmen­t environmen­t.

Microchip Technology’s MPLAB X integrated developmen­t environmen­t 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 recommenda­tion, 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/mplabxpres­s 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 installati­on, this is something of a simplifica­tion. 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 appropriat­e time.

You might also need to install the latest version of the Java Runtime Environmen­t, 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 appropriat­e for your hardware.

One you’ll see, and which we recommend, is called Curiosity - PICF16F161­9 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 transporte­d 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 potentiome­ter on the board flashing briefly – this indicates that the on-board non-volatile

 ??  ?? You might be more used to processors having hundreds of connection­s or more, but this tiny eight-pin PIC chip is a processor with on-chip memory and I/O.
You might be more used to processors having hundreds of connection­s or more, but this tiny eight-pin PIC chip is a processor with on-chip memory and I/O.
 ??  ?? The Curiosity developmen­t kit gives you all the informatio­n you need to learn about PIC chips and how to program them.
The Curiosity developmen­t kit gives you all the informatio­n you need to learn about PIC chips and how to program them.

Newspapers in English

Newspapers from Australia