Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Getting Started with a Modern Variety PIC Microcontroller

0.00/5 (No votes)
10 Oct 2024 1  
Step by step intro on setting a modern variety PIC for your project
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.

Image 1

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.

  1. Double click the desktop shortcut and open MikroC
  2. You will be greeted by their red load screen, and this can take a few minutes until the IDE loads
  3. Locate the "New Project" button, and click itImage 2
  4. When the "New Project Wizard" Loads, retain the "Standard Project" option and click next
  5. Project Settings
    Image 3
    1. Enter a project name, e.g. "Hello World"
    2. For the project folder, click browse and select your location
    3. For device, click on the drop-down and locate the device PIC18F45K22
    4. For the device clock, you can retain the current setting
    5. Click Next
  6. Add Files: This is more of an advanced topic, so just click next
    Image 4

 

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.

/*
 * Project name:
     LED_Blinking (Simple 'Hello World' project)
 * Copyright:
     (c) Mikroelektronika, 2011.
 * Revision History:
     20110929:
       - initial release (FJ);
 * Description:
     This is a simple 'Hello World' project. It turns on/off LEDs connected to
     PORTA, PORTB, PORTC, PORTD and PORTE. 
 * Test configuration:
     MCU:             PIC18F45K22
                      http://ww1.microchip.com/downloads/en/DeviceDoc/40001412G.pdf
     Dev.Board:       EasyPIC7 - ac:LEDs
                      http://www.mikroe.com/easypic/
     Oscillator:      HS-PLL 32.0000 MHz, 8.0000 MHz Crystal
     Ext. Modules:    None.
     SW:              mikroC PRO for PIC
                      http://www.mikroe.com/mikroc/pic/
 * NOTES:
     - Turn ON the PORT LEDs at SW3.
 */

void main() {

  TRISA = 0;           // set direction to be output
  TRISB = 0;           // set direction to be output
  TRISC = 0;           // set direction to be output
  TRISD = 0;           // set direction to be output
  TRISE = 0;           // set direction to be output

  do {
    LATA = 0x00;       // Turn OFF LEDs on PORTA
    LATB = 0x00;       // Turn OFF LEDs on PORTB
    LATC = 0x00;       // Turn OFF LEDs on PORTC
    LATD = 0x00;       // Turn OFF LEDs on PORTD
    LATE = 0x00;       // Turn OFF LEDs on PORTE
    Delay_ms(1000);    // 1 second delay

    LATA = 0xFF;       // Turn ON LEDs on PORTA
    LATB = 0xFF;       // Turn ON LEDs on PORTB
    LATC = 0xFF;       // Turn ON LEDs on PORTC
    LATD = 0xFF;       // Turn ON LEDs on PORTD
    LATE = 0xFF;       // Turn ON LEDs on PORTE
    Delay_ms(1000);    // 1 second delay
  } while(1);          // Endless loop
}

 

 

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here