Getting Started with Embedded C Programming - Part 1
Hi there! It’s been a long time since we’ve finished the Microcontroller Basics tutorials which were devoted to programming the PIC10F200 MCU using the Assembly language. This series became quite popular, and people asked if we were going to continue with PIC microcontrollers. And now the time has come to give a positive answer.
Now we will switch to the C programming language as it’s simpler to write more complex code on it. Also, we will use a much more advanced MCU than the PIC10F200. But the concept of the tutorials will remain the same - to show how things really work inside the MCU. So we will often refer to the microcontroller data sheet to check the registers and bits names, and to figure out what they do. I’m also planning to show you how to use the visual code configurator which simplifies our life a lot and hides the operations with the registers inside the API functions. I think it will be interesting to compare the Microchip code configurator with a similar Renesas tool which we will consider in the RA MCUs series.
Let’s start with the hardware part first. I know there are tough times now from the parts prices and availability perspective. So I spent a lot of time finding the proper MCU which has an advanced periphery, a decent price, and is (at the time of writing this) available and in stock. My choice finally fell on the PIC18F14K50 microcontroller (PIC18F14K50 Page). For now it’s available directly at the Microchip site and in local electronic parts shops. It’s available in PDIP-20, SOIC-20, SSOP-20 or QFN-20 packages. As we’re going to use the breadboard for assembling the schematics, it makes sense to purchase the MCU in the PDIP package. If it’s not available, don’t be upset, you can use these SOIC/SSOP to DIP converter PCBs (Fig. 1)
These boards are available on Aliexpress or ebay and cost about $1 for 5 pieces. I also have the PIC18F14K50 MCU only in the SOIC package so I’m going to use this converter.
Let’s consider the main features of the selected MCU.
- 15 input-output pins, one of which is input only (the same as with the PIC10F200);
- 16 kB of flash memory and 768 bytes of the RAM;
- Up to 48 MHz clock frequency;
- One 8-bit timer and three 16-bit timers;
- 10-bit analog-to-digital converter with internal reference voltage source;
- Dual rail-to-rail comparator;
- Enhanced Capture/Compare/PWM (ECCP) module;
- Master Synchronous Serial Port (MSSP) module which can work in SPI and I2C modes;
- Enhanced Universal Synchronous Asynchronous Receiver Transmitter (EUSART) module;
- Full-speed and high-speed USB device module.
Maybe not all of these modules are clear for now but I’ll explain them all later in the corresponding tutorials. For now you can just believe me, this MCU is much more advanced than the PIC10F200.
If you can’t find exactly this MCU, you can purchase PIC18F13K50 which has 8kB of flash memory and 512 bytes of RAM, everything else is the same. Also, you can buy PIC18LF13K50 or PIC18LF14K50 which have a narrower supply voltage range of 1.8 to 3.6 V, so if you decide to use this option, please make sure you don’t apply 5 V to your chip, as this can damage it.
The rest of the required hardware is the same as for the PIC10F200 series (Equipment for our Simple Microcontroller tutorials (PIC10F200) - Part 2):
- PICKit debugger. I have both v.3 and v.4 but I like the first one more so I’ll use it.
- Resistors of different values;
- Tactile switches;
- Wires with the Dupont connectors;
When we move forward, you also will need other parts and modules, but I’ll talk about them later.
Now let’s switch to the required software. Microchip offers the free MPLAB IDE to work with their microcontrollers. As MPLAB 8.76 is deprecated, we will use the MPLAB X IDE as the only option. For now the latest available version is 6.00 and can be downloaded from the Microchip site: MPLAB® X Integrated Development Environment (IDE).
The last time we used it and didn’t need any additional software as MPLAB comes with the built-in PIC Assembler. But now, as we agreed to use the C language, we need to also install the C compiler. Microchip provides free compilers for all PIC architectures called XC. There are XC8, XC16, and XC32 for 8-bit, 16-bit and 32-bit MCUs correspondingly. As PIC18F14K50 is an 8-bit MCU we need to install the XC8 compiler which the latest version (2.36 for now) can be found here: MPLAB XC8 C Compiler.
If you still don’t have MPLAB X installed, I’ll make a short guide about it. During the installation process after some “Next” clicks you will see the following screen (Fig. 2)
Here you need to select which applications and devices you need. MPLAB X IDE is the main application in which we will work so it is mandatory. The MPLAB IPE is a standalone programming tool. I’ve actually never used it, so I’ll skip its installation. Below you can select the devices’ support installation. As we will use the 8-bit MCU, we need to select this option. The other ones are optional. Then you can click “Next” twice and wait while MPLAB X is being installed.
After installation is complete, you will be prompted to choose some actions (Fig. 3).
Here you can select any check. If you haven’t downloaded the XC8 installer, you can select the third one. We will also install the MCC but directly from the IDE, so no need to select the last check.
Let’s now install the XC8 compiler. On the third screen after accepting the license agreement, you will need to select the license type (Fig. 4)
But actually this is a choice without a choice, as all other options except for “Free” are not available, so we just click “Next”, select the installation directory, click “Next” again and see the following screen (Fig. 5)
Well, these options are up to you. If you are going to use the compiler alone on your PC, then deselect the first check. Also you can add the xc8 to the PATH, it will not hurt. Then click “Next” twice and wait while installation is complete. In the end you will be prompted to buy a license (Fig. 6)
You can skip this step by clicking “Next” as the XC8 compiler also has a free version which doesn’t need any license. In the next screen click “Finish” to finalize the installation.
Now we’re good to launch the MPLAB X IDE. Find its icon in the Start menu or on your desktop and run it. You will see the following application (Fig. 7)
We will not create a new project now but we need to install the MPLAB Code Configurator as well. To do this, you click on the “Tools” in the main menu, then select “Plugins”. In the opened window you select the second tab “Available plugins” (Fig. 8)
In the list you need to find and select ‘MPLAB Code Configurator’ then click the “Install” button. You can also select any other plugins you want. The description of each one is in the right part of the window, but so far no other plugins are needed. After installation of the Code Configurator you will be prompted to restart the IDE. You should agree to make sure that everything went well.
After restart you will see that the icon MCC has appeared in the toolbar (Fig. 9)
The icon is inactive now as no project was created yet.
And that’s it for now. Next time we will create and run our first project for the PIC18F14K50 microcontroller using C language. As we already discussed the inner workings of MCU’s in general, and PIC’s in particular, we can jump right into the hands-on tutorials. If, at any time, you’re confused about what’s going on internally with the MCU, go check out the first tutorials of the PIC10F200 Assembly-based tutorials.
As homework I suggest you purchase the required MCU (this can be a challenge, believe me). Also, if you don’t have the MPLAB X IDE installed yet, please get it.
- 212 Tutorials
- 9 Textbooks
- 12 Study Guides
- 31 Tools
- 97 EE FAQs
- 295 Equations Library
- 200 Reference Materials
- 91 Glossary of Terms
Friends of CircuitBread
Get the latest tools and tutorials, fresh from the toaster.