Silicon ChipPICn’Mix - September 2020 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Subscriptions: PE Subscription
  4. Subscriptions: PicoLog Cloud
  5. Back Issues: PICOLOG
  6. Publisher's Letter
  7. Feature: The Fox Report by Barry Fox
  8. Feature: Techno Talk by Mark Nelson
  9. Feature: Net Work by Alan Winstanley
  10. Project: ULTRABRITE LED PUSHBIKE LIGHT by Daniel Doyle Nicholas Vinen
  11. Back Issues: LFSR Random Number Generator Using Logic ICs by Tim Blythman
  12. Project: The Micromite Explore-28 by Geoff Graham
  13. Project: Three Stepper Motor Drivers by Jim Rowe
  14. Feature: Cheap and easy compact speaker enclosures by Julian Edgar
  15. Feature: Circuit Surgery by Ian Bell
  16. Feature: Make it with Micromite by Phil Boyce
  17. Feature: Max’s Cool Beans by Max the Magnificent
  18. Feature: PICn’Mix by Mike Hibbett
  19. Feature: AUDIO OUT by Jake Rothman
  20. PCB Order Form
  21. Advertising Index

This is only a preview of the September 2020 issue of Practical Electronics.

You can view 0 of the 72 pages in the full issue.

Articles in this series:
  • Techno Talk (September 2020)
  • Techno Talk (October 2020)
  • (November 2020)
  • Techno Talk (December 2020)
  • Techno Talk (January 2021)
  • Techno Talk (February 2021)
  • Techno Talk (March 2021)
  • Techno Talk (April 2021)
  • Techno Talk (May 2021)
  • Techno Talk (June 2021)
  • Techno Talk (July 2021)
  • Techno Talk (August 2021)
  • Techno Talk (September 2021)
  • Techno Talk (October 2021)
  • Techno Talk (November 2021)
  • Techno Talk (December 2021)
  • Communing with nature (January 2022)
  • Should we be worried? (February 2022)
  • How resilient is your lifeline? (March 2022)
  • Go eco, get ethical! (April 2022)
  • From nano to bio (May 2022)
  • Positivity follows the gloom (June 2022)
  • Mixed menu (July 2022)
  • Time for a total rethink? (August 2022)
  • What’s in a name? (September 2022)
  • Forget leaves on the line! (October 2022)
  • Giant Boost for Batteries (December 2022)
  • Raudive Voices Revisited (January 2023)
  • A thousand words (February 2023)
  • It’s handover time (March 2023)
  • AI, Robots, Horticulture and Agriculture (April 2023)
  • Prophecy can be perplexing (May 2023)
  • Technology comes in different shapes and sizes (June 2023)
  • AI and robots – what could possibly go wrong? (July 2023)
  • How long until we’re all out of work? (August 2023)
  • We both have truths, are mine the same as yours? (September 2023)
  • Holy Spheres, Batman! (October 2023)
  • Where’s my pneumatic car? (November 2023)
  • Good grief! (December 2023)
  • Cheeky chiplets (January 2024)
  • Cheeky chiplets (February 2024)
  • The Wibbly-Wobbly World of Quantum (March 2024)
  • Techno Talk - Wait! What? Really? (April 2024)
  • Techno Talk - One step closer to a dystopian abyss? (May 2024)
  • Techno Talk - Program that! (June 2024)
  • Techno Talk (July 2024)
  • Techno Talk - That makes so much sense! (August 2024)
  • Techno Talk - I don’t want to be a Norbert... (September 2024)
  • Techno Talk - Sticking the landing (October 2024)
  • Techno Talk (November 2024)
  • Techno Talk (December 2024)
  • Techno Talk (January 2025)
  • Techno Talk (February 2025)
  • Techno Talk (March 2025)
  • Techno Talk (April 2025)
  • Techno Talk (May 2025)
  • Techno Talk (June 2025)
PIC n’Mix Mike Hibbett’s column for PIC project enlightenment and related topics Part 2: Software tools for the PIC18F S eptember 2020 marks the 15th anniversary of my start as a writer for the magazine. Back in 2005, articles were submitted by paper copy months in advance, followed by many iterations, all requiring that alterations still fitted within the same layout. ‘If you take five letters out, add five letters in’ was standard practice. 15 years later we rework articles through email but there are still many iterations to create our published output. In 15 years, editorial processes have changed, but I still recognise and remember the magazine I read as a child in the 1970s, and the authors who I was fortunate enough to work with since becoming an author. It’s with great fondness I remember John Becker, my former technical editor, who was both my professional liaison with the magazine and my hero from the 1970s. Tools for the job So, onto this month’s article, where we look at the software tools available for developing programs on Microchip PIC processors. In the July issue we covered how we selected the PIC18F47K42 (in a 40-pin DIL package) to form the basis of our PIC18 ‘Experimenter’ board. We chose the PIC18F47K42 through a long selection process – there are around 17,000 Microchip processors to choose from – because it offers a broad range of peripherals, in an easy-to-work-with package. It has not been used in projects within the magazine before, which makes it an interesting candidate. There has been no particular reason why it has not been used before – authors tend to stick with older processors they are familiar with – but the PIC18F47K42 offers a wide range of peripherals and lots of memory, which will make it adaptable for many different projects. Before we delve into the hardware aspects of any designs based on this processor, we need to get the software development environment installed, and a level of familiarity established. This is true whether you are a hobbyist or a professional Fig.1. Microchip debuggers (l-r): PICit 4, Snap, PICkit 3. We strongly recommend the PICkit 4, but the PICkit 3 is also a good choice. The Snap, however, is too limited for this project series. Practical Electronics | September | 2020 developer; having a complete development environment and being comfortable using it can make a huge difference to how easy it is to build a project, quickly. PIC vs Arduino Developing software for the PIC processor is very different to platforms such as the Arduino, which comes with over a hundred libraries and example applications. Microchip focus on providing a few key low-level driver libraries, and just a handful of example applications. Their low-level documentation for the processor is excellent however, so writing any new driver libraries is possible, and allows you to fine tune your software to match your application’s needs, and the capabilities of the processor. It does, however, require that you understand more about what is going inside the processor. That’s fine – it’s the reason why we experiment with PIC processors, to learn more about what is going on. If this learning opportunity does not interest you and you only want to get a project up and running as quickly as possible, then an Arduino development system would be more appropriate. The benefit of learning more about the processor itself is that it will provide you with many more options to solving a design challenge, and with more complex designs, enable you to create a solution that would not be possible on a simple Arduino platform. MPLAB X IDE Developing software requires a number of different tools. At a minimum, you need the following:  Text editor  Compiler  Driver libraries  Programmer/debugger  Programming interface To pull all these requirements together Microchip have created an Integrated Development Environment (IDE) called MPLAB X, which combines the code editor, programmer and debugger capabilities into a single application. The IDE is really a framework application that allows other PC programs to be configured, run and the results viewed all within the same application. It’s possible to run the compiler, see a list of error messages from that complier in an IDE window, that when you click the mouse on them will cause the offending line of code to come up in the editor window. Using MPLAB X is not a hard-and-fast requirement; you could edit your programs using your favourite editor, then compile the program in a command shell. However, Microchip’s IDE simply makes the process easier and quicker to perform. In addition to MPLAB X we will also need a compiler. This is a standalone application that will convert our programs, written in text files in the ‘C’ programming language, into the 57 compiler performs to reduce code size. This would be important if you were building a large number of products and would like to use smaller, cheaper versions of a microcontroller. For hobbyists, we can simply purchase the device with larger memory that costs perhaps 50p more. To reiterate, you are not missing out on features by avoiding an expensive purchase. Programming interface Fig.2. Uninstalling MPLAB components. binary format that the processor understands. Microchip provide three different compilers for the different PIC families:  MPLAB XC8: all 8-bit PIC devices, from the PIC10F to the largest PIC18F  MPLAB XC16: all 16-bit PIC devices, PIC24, dsPIC33  MPLAB XC32: all 32-bit PIC devices, such as the PIC32 These have to be download separately to MPLAB X but are very easy to install. The programmer and debugger capabilities are pre-built into MPLAB X and do not require a separate download. With the above components you have the minimum software necessary to start developing programs. However, one further download provides a set of goodies – example drivers and sample applications – that will make our life easier, especially if we are using UART, I2C or SPI communications. Confusingly, Microchip provide three different software packs to fulfil this, called MLA, Harmony and MCC. MLA is the old and now deprecated ‘Microchip Libraries for Applications’, which has been replaced by MCC, ‘Microchip Code Configurator’. MCC is used with 8-bit devices, such as the PIC18F family. Harmony is a similar pack of libraries, but targeted to 32-bit devices, so of no interest to us today. We will be installing MCC and will be making frequent use of it. In summary, our installation process will be MPLAB X, the XC8 compiler, followed by MCC. If you plan to use a 16-bit or 32-bit processor in the future, feel free to install those two compilers too, as they can co-exist with no issue. MPLAB X is smart enough to choose the correct compiler for your project, as we will show later. Last, but not least, Microchip have helpfully made these tools available not only for Windows, but also Linux and macOS PCs. …and the cost? The great news is that the MPLAB X IDE and the MCC libraries are free – however, the situation is a little more complicated with the compiler programs. They are provided for free, but you can pay to unlock commercial/professional features. The professional versions provide additional optimisations in the translation to a code binary, and this can result in a 20-30% improvement in code space use compared to the ‘free’ version of the compiler. There may also be some reductions in the amount of RAM used. In our opinion, for hobbyists, these improvements do not justify a fairly expensive purchase of a professional compiler licence. The ‘free’ version is not crippled in any way; all compiler features are available – the difference is in the optimisations the 58 Microchip processors require a small piece of hardware, called a ‘debugger’, to transfer programs to a device. The tool of choice is the PICkit 4 from Microchip, which costs around £55. The previous generation debugger, the PICkit 3, can also support the processor we are using and is cheap secondhand on eBay (from £12). Clones of the PICkit 3 can be purchased on eBay for as little as £15, but we strongly recommend using the official device. We will focus on using the PICkit 4 in this and future articles. It’s a solid product that should last for years – we have never had a Microchip debugger fail on us in 25 years, the only issue is that every decade or so new processors become available that require a programmer update, but you will be required to update your programmer only if you want to use those new ICs. There is a lower-cost debugger available from Microchip called the ‘Snap’, which costs around £18. It can program the PIC18F47K42 but has limited features, comes as just a bare PCB and in our opinion is just too basic for our plans. You can see these three debuggers in Fig.1. On a historical note, our first emulator for the PIC16 series of processors cost around £900 back in 1995! The debugger is connected to the PC via a USB cable (supplied with the debugger.) Five signals connect the debugger to your target PCB via an 8 pin 0.1-inch pitch header socket. Hook-up wires can be used to make the connection, but we prefer to fit an 8-pin header strip to our boards and plug the debugger in directly – we will provide an 8-pin header strip on our experimenter board. Installation preparation In the following sections we describe the setup for PCs running the Windows OS, as this is the most common setup. The installation process for other operating systems will be similar – check online for exact details if you are using Linux or macOS operating systems. Before we install the latest versions of the software (Microchip update their software tools frequently) we will want to remove any previous versions. Your creations will not be deleted by these steps, just the very large IDE and compiler install files, which can add up to many gigabytes of disk space. If you do not have Microchip tools installed you can skip to the ‘Installation’ section below. To remove earlier versions, first click on the ‘Start’ button, then type ‘Add’, and select ‘Add or Remove programs’. From the drop-down list that appears, as shown in Fig.2, type ‘mplab’ in the search box to filter just the Microchip software components. Now remove all ‘Microchip’ installed features. You will have to do these one at a time. Left click on an item in the list, then click the ‘Uninstall’ button. Follow the instructions, and when done, repeat for all the other entries. We had about a dozen entries for multiple old versions of the toolchain, so this bit of spring-cleaning freed up over 10GB of disk space. Installation We start by installing the latest version of MPLAB X IDE, which at the time of writing is version v5.40. In your browser, go to: https://www.microchip.com/mplab/mplab-x-ide and scroll down to the ‘Downloads’ tab. Click on the text ‘MPLAB X IDE v5.40’ to start the download. Don’t worry if the version has been updated, changes are likely to be minimal. Microchip make regular updates to their software tools (typically when new processors Practical Electronics | September | 2020 Adding MCC Fig.3. The MPLAB X main window. Our final installation step is to bring in MCC – the drivers that we can add to our projects. MCC is not just a set of source files, it’s a complete GUI (graphical user interface) that helps simplify the process. Confusingly, there are several ways to install MCC; we will do it within MPLAB X itself, as the IDE has a ‘plug-in’ tool already built in. Click on the ‘Tools’ menu option, followed by ‘Plugins’ (not ‘Plugins Download). Click on ‘Available Plugins’ and scroll down until you see ‘MPLAB Code Configurator’. Click the check-box, then click on the ‘Install’ icon at the bottom of the window, as shown in Fig.4. Click ‘Next’, then accept the licence agreement, and click ‘Next’ to start the installation process. MPLAB X will now download MCC and install it; so this may take a minute or two. At the end, MPLAB X will need to restart, so just click ‘Finish’. MPLAB X will restart automatically after a few seconds and complete the installation process. We are now ready to create our first project. If you have a PICkit 4 debugger, go ahead and plug that in now. The front of the device should illuminate blue once connected. In MPLAB X IDE, click on the ‘File’ menu option, followed by ‘New Project...’. In the ‘Categories’ section of the pop-up window leave ‘Microchip Embedded’ highlighted and click on ‘Standalone Project’ followed by ‘Next’. In the highlighted ‘Device:’ field, type in the name of our processor, ‘PIC18F47K42’. If you have a PICkit 4 plugged in then select it from the ‘Tool:’ drop-down list. Click ‘Next’. We can select the compiler we will be using; XC8. This should be shown in this list, so click on that and then click ‘Next’. Finally, we have to give our project a name (type ‘test’) and specify where this project will live on your PC’s disk. This directory will hold your project settings and all the source files you create. For now, it is an empty project and we shall leave MPLAB X at this point and return to it once we have completed our hardware design. are added, but also for bug fixes and improvements.) MPLAB has been in development and maintained for many years. Once downloaded, double click the file to start the installation process. Other than clicking to accept the software licence terms, accept all defaults. We recommend that you do not change the installation folder as this will make installing the compiler and MCC libraries difficult. Close to the end of installation a Windows Security pop-up will be displayed. Click ‘Install’ to install the hardware drivers for the debugger tool. At the end of installation, a Setup pop-up appears; untick the three checkboxes – we will perform these steps manually. Click ‘Finish’ to complete the installation. Now for the compiler; in your browser, go to: https://www. microchip.com/mplab/compilers. and scroll down until you see the ‘Compiler Downloads’ tab. Click on that, and then click on ‘MPLAB® XC8 Compiler v2.20’. Again, this version may have been updated by the time you get to it, so it may be v2.21 or similar. This is fine; compiler updates rarely impact Coming up next users of the tool, and just provide minor bug fixes. Again, once In the next article we will start the design of our experimentdownloaded, double-click on the file to start installation. Other er PCB. Our intent is to make clear the thought process going than clicking to accept the software licence terms, accept all into the design of the board and explore the capabilities of the defaults. The installation process will update MPLAB so that processor. We will be focusing on using the processor rather it ‘knows’ a compiler has been installed. than diving into the details of internal processor peripherals. We can now start MPLAB X IDE by double-clicking on the We are going to cover: GPS location reporting, digital compass, newly created desktop icon, or by clicking on the Windows various display technologies, speech output, PC connection via ‘Start’ icon and typing ‘mplab x’. Be careful at this point – the USB, Wi-Fi communication, interfacing with Amazon Alexa, MPLAB X IDE installation process installs another useful appliServo motor control, analogue input. cation, a programmer utility, that is amusingly called MPLAB Some are more ambitious than others, but it will be a fun X IPE. Given this differs by only a few pixels on the screen, journey and we hope you will join in. The PCB will be availit’s easy to click on the wrong one. We ended up deleting the able from our usual suppliers, and full PCB Gerber and design desktop icon for IPE by right-clicking on the desktop icon and files available should you wish to etch your own, although as selecting ‘Delete’. This removes the short-cut icon from the a double-sided design this might be challenging. desktop, but leaves the program installed and ready to launch from the Windows Start menu. If you’ve previously installed MPLAB you will be presented with a pop-up box asking whether you would like to import old settings from the previous installation. We recommend you do not do this – we want to start with a clean sheet. So click ‘No’ if presented with that message. You may get a warning from your virus protection software that mplab_ide64.exe is attempting to access the network. Go ahead and click ‘Allow access’, as we trust Microchip, and this feature enables MPLAB to check for updates to software automatically. You should now see the main IDE window, as shown in Fig.3. Two windows are opened on start-up, ‘Start Page’ and ‘MPLAB X Store’. Go ahead and close these windows by clicking the ‘x’ on the right-hand side of the tabs. Fig.4. MCC installation via MPLAB X. Practical Electronics | September | 2020 59