As Arduino and other options have taken center stage, the perception on PICs as a viable alternative to embedded project development has become dim. The article is aimed at illustrating the few steps required to get the proverbial hello world greeting.
Introduction
Many can hardly remember the last time they programmed with a PIC. Asking about their PIC project history usually sends them into the back room to find that dusty box. While Arduino and Sketch have taken center stage in the hobbyist arena, much of what has been redone with an Arduino was done to perfection with a PIC.
How so? The PIC16F84, PIC16F628, the PIC16F887, the PIC16F877a, the PIC18F452, the PIC18F4550 all set the stage for hobbyist development projects ranging from the simple LED flashing to complicated, industry relevant applications.
- PIC16F84 (18PIN) - Basic applications, LCD Display, keypad, etc
- PIC16F628 (18PIN) - Basic applications, LCD Display, keypad, included UART
- PIC16F887 / PIC16F877a (40PIN) - Basic applications, LCD Display, keypad, included UART, I2C, SPI, more advanced applications
- PIC18F452 (40PIN) Basic applications, LCD Display, keypad, included UART, I2C, SPI, more advanced applications. higher speeds with more advanced RISC available
- PIC18F4550 (40PIN) Basic applications, LCD Display, keypad, included UART, I2C, SPI, more advanced applications. higher speeds with more advanced RISC available and native USB UART
Granted, Arduino became an easy and more funky one size fits all, however each device above found their niche in various uses. There is allot of discussion about which is better, PIC or Arduino, and my conclusion on that is each has its role. However, Arduino has made entry to the field easier. PICs, unfortunately can run up a tab before you have a flashing LED.
What you need
To get started all you need the following:
- Coding environment
- PIC Development Board
- Components
- 1 LED
- 1 Resistor
- 1 Bread Board
- Some jumpers
Coding Environment
We recommend MikroC, with 7.6 being the most recent release. The software is available for free download from their site here MikroC for PIC.
PIC Development Board
There are lots of options, from very professional from Microchip themselves, 3rd part or DIY. For an LED to flash, a minimalist PCB is required.
Components
The PIC drives an I/o Port at between 3 - 5 vdc, and a LED switches on an 1.8 V. The combination of a LED + resistor in the required setup is shown below. Your local electronics store will be able to assist with this.
Create the Project
Once you have downloaded and installed MikroC, it has the advantage of making things easy. There is a wizard the creates the project for you.
- Double click the desktop shortcut and open MikroC
- You will be greeted by their red load screen, and this can take a few minutes until the IDE loads
- Locate the "New Project" button, and click it
- When the "New Project Wizard" Loads, retain the "Standard Project" option and click next
- Project Settings
- Enter a project name, e.g. "Hello World"
- For the project folder, click browse and select your location
- For device, click on the drop-down and locate the device PIC18F45K22
- For the device clock, you can retain the current setting
- Click Next
- Add Files: This is more of an advanced topic, so just click next
Using the code
The code below is a sample provided by MikroC in the examples folder. You can copy and paste this into the code window. Alternatively, you can work it to what you want.
void main() {
TRISA = 0;
TRISB = 0;
TRISC = 0;
TRISD = 0;
TRISE = 0;
do {
LATA = 0x00;
LATB = 0x00;
LATC = 0x00;
LATD = 0x00;
LATE = 0x00;
Delay_ms(1000);
LATA = 0xFF;
LATB = 0xFF;
LATC = 0xFF;
LATD = 0xFF;
LATE = 0xFF;
Delay_ms(1000);
} while(1);
}
What it does . . .
- The first step is to always configure the target hardware. In the above example, all the TRIS or direction registers are set to 0 or output
- The loop, sets the PORT latch register for all ports OFF or Low first, waits a 1 second
- The loop then, sets PORT latch register for all ports ON or High and waits 1 second
The net effect is an LED flashing, and hopefully the observing crowd will go wild and cheer you on to do more.
Thinking of Interest
While this has been very brief and it will most likely generate as many questions and smoke, the PIC remains a highly capable device. While only being an 8BIT device is often used to slate it, the ATMEGA328 used in the Arduino is also 8BIT. Do enough searching on the topic of PIC Vs Arduino comparisons, and you will find that all of head to head testing on comparable devices has revealed that the PIC holds its own and at times does better.
History
8 September, 2024 - Revision 1, initial release.