Archive for August 2002

PIC programming tools

There are several tools needed to get started in programming the PICs. I’ll briefly cover some software to develop in and hardware to load the chip.

Software

I use the MPLab package by Microchip. It’s an excellent IDE and is available free for download at their site.

As I go through examples I will introduce specific features of MPLab, I find this the easiest way to do it, rather than listing them out and assuming you’ll get em right away…

Hardware

KIT96 Programmer

KIT96 Programmer

On the hardware end is the actual device to send the data to the chip! For this I use kit 96 from www.kitsrus.com. It is a P16PRO compatible programmer, is quite easy to build, and looks pretty nice once it’s put together, a quality product. There is software available for it at www.picallw.com, and costs a mild registration fee to fully program anything beyond a 16F84(a).

There are other programmers but I must say this is by far the nicest programmer you’ll find short of the professional versions. And even some pro versions leave quite a bit to be desired.

The programmer operates on a wall-wart power adapter and a PC parallel port (yet programs the chips serially). I have added a 40-pin wide ZIF socket on mine to facilitate bigger PICs, and make it WAY easier to put chips in and take them out…

Number bases and logic

Embedded programming requires at the very minimum a casual understanding of binary, hexadecimal, and logical operations. Here is a brief overview of these operations and a few sample uses.

Binary

Binary is all computers understand. It is a numbering system quite different than our “base-10″ system. In “base-10″ or decimal numbers, every digit has 10 possible values, 1,2,3,4… (hopefully you know this!). In binary, there are 2, only 2. These are typically represented by 1 and 0, but it can be a bird and a dog, up/down, left/right, front/back, black/white, ANYTHING, as long as it’s only two.

Quick Info on Binary:

  • a binary digit is known as a ‘bit’
  • 4-bits are called a nibble (or nybble)
  • 8-bits are called a byte
  • binary numbers are typically grouped into nybbles for readability and ease of conversion to HEX

So here’s a binary number: 0110 1000. An 8-bit number. Now what IS the number? Well it’s 104 obviously!

Here’s how we translate binary to decimal. First we need to disect our 8-bit number:

bit #
7
6
5
4
3
2
1
0
104
0
1
1
0
1
0
0
0

Bit 0 is known as the LEAST SIGNIFICANT BIT, and bit 7, the MOST SIGNIFICANT BIT. Bit numbers are increasing LSB to MSB. You get the value 104 by adding 2^(bit #) of all bits that are 1. So for this number it’s: (2^6 + 2^5 + 2^3) = 64+32+8 = 104.

The maximum value of an X-bit number is (2^X)-1. And the maximum number of different values that can be represented by an X-bit number is 2^X. So an 8-bit number can have 256 different values, and a max value of 255 (ranging 0-255 gives us 256 nums).

I guarantee you there are better tutorials on binary out on the net, search around, I’m going for quick-n-dirty here! :)

Hexadecimal

Hex is binary’s big brother in a way… It’s on the other side of the spectrum from binary to decimal, in hex, each digit can be 16 different values (0,1..8,9,A,B..E,F). So our 104 from before becomes: 0x68. The notation “0x” is typical to denote a hex number.

Lets try another number in more detail; 190. In binary that is: 1011 1110. So in hex we have 0xBE.

An easy way to do bin->hex is when the binary number is broken into nybbles, you can picture each nybble as 1 hex digit. The max value of a 4-bit number is 15, F in hex, so the conversion is pretty easy!

0110 1010 1101 1111 0010 1011 -> 0x6ADF2B

Logic

AND

 
0
1
0
0
0
1
0
1

 

OR

 
0
1
0
0
1
1
1
1

 

NOT

0
1
1
0
XOR

 
0
1
0
0
1
1
1
0

With blue being the input, or numbers being operated on, and green being the result.

  • AND – The AND (&) op. results in a 1 if and only if both inputs are 1.
  • OR – The OR (|) op. results in a 1 if either input is 1. (inclusive or)
  • XOR – The XOR (^) op. results in a 1 if strictly ONE input is 1, and the other is 0.
  • NOT – The NOT (!) op. flips the bit, if the input is 1, the result is 0. (a.k.a. compliment)

This is BARELY scratching the surface of logic, but it’s enough to get us by. Here are typical uses for these operations:

AND (&)

AND is good for “masking out” bits. If you wanted to get rid of a few bits in a number, while leaving the others unchanged, AND is what you’d use. Example: I have a 8-bit number, but I only want the upper 4-bits. My number is 0101 1010 = 0x9A. To mask these out, I create the “bitmask” 1111 0000 = 0xF0. The 1’s are the bits I want to keep, the 0’s what I want to get rid of.

0x9A & 0xF0 = 0x90 = 0101 0000 (lower bits have been removed)

OR (|)

OR is just the opposite. It is used for “setting” bits, while leaving the rest unchanged. If you have a byte, and need to set a couple bits in that byte, you “OR them in”. Example: I have a byte, and I want to set bits 0,3,6. My number is 1011 0110 = 0xB6. To OR them in, I make a “bitmask” of 0100 1001 = 0x49. The 1’s are the bits I want to SET, the 0’s the bits I want to leave alone.

0xB6 | 0x49 = 0xFF = 1111 1111 (the bits have been set)

XOR (^)

Exclusive or is a toggler, it’s used to flip bits based on their current value. Example: I have a byte, and I need to toggle the upper 4-bits of it. The number is 1100 0101 = 0xC5. To toggle them I make a “bitmask” of 1111 0000 = 0xF0, the 1’s being the bits I want to toggle, the 0’s being the bits I want to keep the same.

0xC5 ^ 0xF0 = 0011 0101 = 0x35

NOT (!)

Not simply flips the bits, inverts them, toggles, compliments, whatever you want to call it.

!0x0F = 0xF0

Introduction to PIC Microcontrollers

PIC 16F628

PIC 16F628

PICs are great little chips, useful in all kinds of projects. Here is a run-down of some of the features contained in the chips, as well as potential uses.

How to start… Well, first off, what is a microcontroller? Well, the difference between a microcontroller (heretoin referred to as MCU or μC) and, say, a computer CPU is pretty big… Here’s a little chart of some of the typical differences:

  CPU MCU
Onboard instruction memory   +
Complex math routines +  
Super-high speed +  
General purpose   +
All-in-one   +
Built-in I/O ports   +
Low power consumption   +

Microcontrollers are intended for small, specific purposes, they have their own I/O ports, own memory, nearly everything needed is built in. You’ll find them in your microwave, refrigerator, car, camera, tv, all kinds of things…

The microcontroller I’ll tend to use in the beginning at least, is the PIC 16F628. It has 2048 words of instruction memory, plenty of data RAM, lots of EEPROM storage, a serial module, PWM, Input Capture/Compare, Comparators, etc… Lots of features. These may seem complex but ignore it, we’ll start out with super simple stuff and not use any of it til later (if ever!)

This chip runs at 20MHz, which is PLENTY for the purposes we’ll be putting it to, heck it’s an insane amount faster than needed. You can get them for about $4 at Digi-Key.

Schematic

Each of the pins you see there have a specific purpose, which is explained on the following page. A quick look @ the datasheet reveals this:

Legend:

  • <–> denotes a bi-directional data line
  • –> going INTO the chip denotes input-only
  • <– going OUT of the chip denotes output-only
  • the pins may have various labels, depending on how you decide to use them in your design
  • if a label has a BAR above it’s name (i.e. MCLR) it is active LOW, meaning the pin is “enabled” @ 0V instead of @ +5V

The 16F628 has 2 – 8bit data ports. They are the RA0..7 and RB0..7 you see on the diagram. Vss = Ground, and Vdd = +5V typically, and in my examples, always. CMPx are comparators, TX/RX are serial guys, etc, they are all described in the datasheet. For the first few tutorials, I’ll only be using them as straight digital outputs, no funky stuff.