Home‎ > ‎Sergey's Blog‎ > ‎

Microchip PIC Microcontrollers - Part 1

posted Nov 16, 2013, 12:02 AM by Sergey Kiselev
In the next couple of posts I'd like to deviate from my previous Galileo topic and talk a bit about much simpler microcontrollers that IMHO get frequently overlooked with all the Arduino craze. Yet these microcontollers are pretty simple to program, and much cheaper than Arduino (just a few $$ vs. tens of $$). More specifically I am going to talk about my favourite mid-range devices like PIC12F629, PIC12F675, and PIC16F88. Other popular with hobbyists PIC controllers include PIC16F84A (mostly because it is a direct replacement of venerable PIC16C84), PIC16F627 and PIC16F628. All these microcontrollers can be replaced by PIC16F88.

Introduction

Microchip PIC microcontrollers have been around for quite some time, at least 20 years in their current form - PIC16C84 was released in 1993. When released PICs were among first microcontrollers to use electrically erasable memory. Early devices (like PIC16C84 mentioned above) employed EEPROM for both instruction and data memory, later devices (all these with 'F' in between numbers in the part name, like PIC16F88) use flash for instruction memory and EEPROM for data memory (because it is byte-programmable).
From computer architecture perspective PIC microcontrollers are Harvard type RISC processors. This means that they have separate program and data memories, and relatively small instruction set with fixed instruction size. Most mid range devices have only 35 instructions. Another interesting fact that the instruction word size in these devices is 14-bit and not a multiply of 8-bit (a byte).

Specifications

The specifications vary between different microcontrollers in Microchip PIC family. So I will give some highlights. Hopefully it will help in picking up the right microcontroller for your project.
  • Multifunctional I/O ports with 25 mA sink/source current (more than enough to drive a LED, or a small speaker or buzzer).
    • 8-pin PIC12F629, PIC12F675 have 6 I/O pins.
    • 18-pin PIC16F88 have 16 I/O pins.
    • Analog comparator or two, with inputs and outputs accessible through I/O pins.
    • 10-bit analog to digital converter on PIC12F675 and PIC16F88. Input to this ADC can be multiplexed between several I/O pins. So it is possible to measure signals on all of these inputs (not at the same time, but in very close intervals).
    • PWM and RS-232 compatible UART, SPI, and I2C serial interfaces on PIC16F88
  • Wide operating voltage. Generally from 2 V to 5.5 V. Some devices, for example PIC16F88 require 4V to 5.5V, but they usually have lower voltage equivalents, e.g. PIC16LF88.
  • Built in oscillator. This will save you a quartz resonator in applications where high timing precision is not required. Of course you can use an external resonator, but that would take two I/O pins.
  • Processor clock frequency is up to 20 MHz. But it depends on the voltage (higher frequency requires higher voltage).
  • 2 or 3 timers.
  • Flash based instruction memory:
    • PIC12F629, PIC12F675 - 1024 words (14-bit instructions)
    • PIC16F88 - 4096 words
  • SRAM data memory:
    • PIC12F629, PIC12F675 - 64 bytes
    • PIC16F88 - 368 bytes
  • EEPROM based data memory:
    • PIC12F629, PIC12F675 -128 bytes
    • PIC16F88 - 256 bytes
  • Programmable interrupts on timer overflow, input pin change, and so on.
  • Various microcontroller goodies (power on reset, brown-out detect, watchdog timer)

Programming PIC Microcontroller

First of all I would recommend getting familiar with PIC hardware capabilities. Read a datasheet specific for the PIC you're planning to use. You don't have to read everything, but at least get familiar with I/O ports functionality and other features you want to use.

Important Stuff

I'll just mention a few things to pay attention to. First is the configuration word it is programmed into special location in microcontroller's memory and used to configure basic things like what type of oscillator to use, whether enable watch dog timer, copy protection and so on. Normally you can define the configuration word when you write your program. Some programmer software also allow changing configuration word when programming the PIC.

Next is the internal oscillator calibration value. Microchip calibrates internal oscillator during manufacturing and writes the calibration value into the last word of program memory (address 0x3FF in case of PIC12F629 and PIC12F675). Programmer software normally should preserve this value (but I've seen some that erased it). You might want to read that value and write it somewhere (e.g. on the chip itself). Also as one of the first steps in initialization sequence you might want to calibrate the oscillator by doing:

        bsf STATUS,5    ; bank 1 (RP0 = 1)
        call 0x3ff      ; get the OSCCAL value
        movwf OSCCAL    ; calibrate
        bcf STATUS,5    ; bank 0 (RP0 = 0)

Other important steps of initialization sequence is configuring I/O ports and interrupts. Here are some registers you might want to setup here:

  • CMCON - comparator configuration
  • ANSEL - analog to digital converter configuration (if ADC is present)
  • TRISIO - I/O pin direction - input or output
  • WPU - weak pull up enable / disable. Note that not all pins have weak pull ups.
  • OPTION_REG - various options: global weak pull up enable/disable, timer configuration, and so on
  • INTCON - interrupts control register

Programming Languages

As far as programming languages there are several possibilities here:

Assembler

PIC assembly language is pretty simple (did I mention - only 35 instructions). But it is significantly different from x86 assembler we all (or at least I) used to. So it takes a little exercise to get familiar with it. As far as assembler software I personally like open source GPUTILS suite.

C Language

There are several C language compilers for PIC, most of them commercial ones. SDCC is notable free and open source compiler.

Other

Commercial BASIC and Pascal compilers are available. Another thing that worth mentioning is Flowcode development environment.

Uploading Programs to PICs (Programming Instruction Flash Memory)

This is probably a more tricky part for Arduino users and it requires a one time investment - buying or building a programmer. In old days a popular solution was building a serial port based JDM programmer, which contains only a few components. This still might be a viable solution if you have an older computer with serial port. For newer computers you'll have to find a USB based programers. There are several such programmers available. The official Microchip programmer is PICKit, which is a bit expensive to my taste. eBay is full of K150 programmers, that IMHO is a simple JDM programmer connected to a FT232 USB to serial converter IC. The scary part about them is software, which might be or might be not compatible with your OS. Usbpicprog seems to be a good choice here. It is an open source design. And it is based on a PIC chip itself. So you'll need another programmer to program the programmer's PIC if you'll be building it. Alternatively you can buy the programmer from project's web site (20 euro + shipping). Another positive side is that usbpicprog project supplies programmer software for all popular OSes (Linux, Windows, Mac OSX).


Comments