Introduction


L.Chung: It is not easy to write another PIC tutorial as there are several really good ones around, like 16F84 Tutorial by Mike Tracey and the practical hands on approach tutorial by Nigel Goodwin. It is pointless to just repeat what was already said and mentioned so I tried to include material that are less well known. I got these material from searching the Internet, reading other's problems and digging into datasheets. It is my intention that it will help those starting up in PICs from scratch. This is especially true when english is not my first language. I hope you will enjoy my tutorial.

Are you sure?

So you have heard about microcontroller, PIC in particular. You have a problem about building a circuit which changes its behaviour upon different external conditions and you have asked for help in online forums. Experienced people in forums would tell you to use a Microcontroller(uC) as it results in very simple design, other than to use tens of TTL/CMOS ICs. What they said is surely true as using a uC is far far simpler and cheaper than using several ICs, not to mention the immense flexibility it offers. There are many types of uC one can choose from and this tutorial is focused on only one of them, the PIC. Quite unlike my own past experience in microprocessors like 6800 and Z80, all the PIC datasheets and software development tools are freely available from the internet with no cost involved. Many of the hardware tools and associated software are also free too in case one need to build them.

So, let's get started.

So what's a PIC

To most people a PIC is just a piece of plastic with pins sticking out. Some has 8-pins, while others has 18-pins, 28-pins, 40-pins or "a hell lots of pins" on four sides. Nothing will happen when you power it up. To make it do something useful, you have to understand what is involved.

In the past, we have several famous and well known microprocessors like Z80, 6800 and 6502. They are essential a central processing unit(CPU) with only data bus, address bus and interrupt connections. To make them do something useful, user had to provide extra peripheral like memory(program code & data), address decoder, external crystal oscillator, I/O ports, USART and timers. Finally, user has to write a program and store it in the program memory before powering up the whole CPU system.

Microchip, the sole PIC producer, has integrated as much as possible the above "peripheral" into a single integrated circuit, as have all other microcontroller manufacturers. Gradually, more and more functional modules were added to the PIC and these include modules like EEPROM(data storage in absence of power), A/D(analogue to digital conversion), Voltage Comparator, UART for serial communication, I²C, USB, PWM(pulse width modulation), Event timer and counter and Internal oscillator. So it is now possible not to use an external crystal clock and have all the pins on the PIC available for I/O purposes, except for the two power pins. On the 18-pin 16F628A, it is possible to have 15-1/2 I/O pins. The half means one pin can be input only and its the pin4 MCLR.

PIC Naming

The naming of PIC by Microchip is a little confusing to people starting up as PIC families are divided into "basic", "medium" and "high" range using 12-bit wide, 14-bit wide and 16-bit wide program memory word respectively. The width of the "memory word" is a very important consideration because the PIC's RISC(explained later) structure coding embedded the target jump address(for "GOTO" & "CALL") into the instruction coding itself. So a GOTO or CALL instruction, when assembled, has in itself the jump address. A longer instruction bit width would enable a larger range of the jump. The 12F675 and 16F628A are 14-bit core while there are other like 18F series which are 16-bit core. There are vitually hundreds of different PICs to choose from as one can select based on core width, program memory amount, EEPROM amount and other desirable features. Some of PICs are one time programmable(OTP)which user can program only once. The 16F series, however, is based on "flash" technology(the "F" word) and the program memory content can be erased and re-programmed upto 100,000 times.

PIC Internal

PIC uses the Harvard Bus structure which means separate buses for program code data and user data. This is similar to the 8051 with internal ROM and external RAM. The main difference is the width of the data bus and program code bus which are 8-bit/14-bit for the PIC. The program code, as mentioned earlier, is 14-bit wide while the data in the PIC registers, are still 8-bit wide. As a result, the return address of a CALL instruction can not be stored in user RAM because it is more than 8-bit wide. Since the largest program memory PIC in the 16F6x8A family at the current time is only 4K words for the 16F648A, Microchip's solution to this is to include a 13-bit hardware Stack(which can point to any of the 8K address) for the program counter, to a depth of eight levels.

While it is true that the ability of the 13-bit stack to store any address within 8K enable subroutines to return to any calling address, however, does not automatically mean user's code can just call anywhere in these 8K memory space. Remember I have just mentioned above that the jump address is embedded in the instruction itself, which itself is only 14-bit wide for the 16F family. As a matter of fact, only 11-bit is available for storing the address which enable us to call/jump anywhere within a 2K range. There is no problem whatsoever for 16F628A because it only has 2K total program memory but 11-bit can't cope with 4K memory address in the case of 16F648A. More of that later in the Subroutine section of the tutorial.

To recap, the bit width of 16F6x8A family are: 14-bit program code word, 13-bit program counter with 8-level stack, and finally 8-bit data/registers.

RISC

The power of a PIC is unbelievable and it can easily out perform all previous entire CPU systems built from Z80, 6800 and 6502. The reason for its power lies in the RISC(Reduced Instruction Set Computer) structure of the CPU. By reducing the number of instructions to just 35 on the 14-bit core, nearly most of the instructions takes only one instruction cycle to complete. The instruction cycle is FOUR crystal clock cycles. A PIC running on 4MHz crystal carries out one million instructions per second, not to mention five million instructions if 20MHz crystal is used instead.

The PIC also use a pre-fetch pipeline in executing instructions. While one instruction is being executed, the next instruction is fetch from program memory so in general nearly all instructions executes in single cycle. The only exception is branching which takes two cycles.

Which PIC to use

To someone new in the field of microcontroller, a PIC is a microcontroller which perform certain actions according to instructions stored inside its program memory by the user. These actions includes(but not limited to) checking a certain pin for high or low voltage or setting a certain pin to high or low voltage so that external circuit can use this to drive something. So the more pins one have on the PIC, the more choices one have. PICs come in sizes of DIL 6-Pin, 14-Pin, 18-Pin, 20-Pin, 28-Pin, 40-Pin and other packaging with a lot more pins. The DIP package with 0.1 inch pin spacing is good for use in hobby environment but other packages with their small overall size are starting to attract more and more attentions amomg hobbyist.

A new PIC you bought from a store will be blank and contains no instructions.

Coding the PIC

The instructions the PIC understand are in a form of binary codes. This is very inconvenience for users because we are used to more high level communication like those we use in our daily language. Examples like "check", "output", "reverse", "clear" etc...are some of these. Therefore a piece of software running on a computer is required to "translate" the high level instructions we use into the corresponding binary codes the PIC can understand. This is where the assembler comes in. Its job is to translate those high level instruction into a form that the PIC can understand.

HEX Files

However, binary codes is also difficult for handling within the personal computer so it is respresented using ASCII characters in HEX format. It is just a respresentation of binary codes for easy handling only. The assembler also take care of that.

Software Developement Tool

Unlike our past experience in Z80 or 6502, where assembler software costs user money to buy, the software(MPLAB) we use for the translation(assembler) is offered free from the PIC manufacturer Microchip and the software also includes editor and simulator. So it is a must for beginners. We write our instructions in a text file with .asm extension and then translate(assemble) it into a HEX file suitable for the PIC to understand and follow.

Some people think assembly language is still a bit difficult to use as they are used to more high level languages like C or BASIC. For these people, there are Compilers available but the good ones costs a lot of money. e.g. US$250 for a C compiler. It is also a general understanding that one cannot be efficient at programming the PIC using C or other high level languages unless one is familiar with the assembly language programming. Therefore the recommendation is to start at assembly and moves up when one is feeling at ease using assembly codes.

Programming HEX code into PIC

Next comes the question of how to get these binary codes into the memory of the PIC so that each time we power up the PIC, it would automatically do what we wanted. Remembers that the HEX file is still inside our computer at the moment. You'll need both a piece of hardware called "programmer" between the computer and PIC to transfer the data and an application software running on the PC. What the software does is getting the HEX data from the file on the harddisk and transfer it to the PC's port(serial, parallel or even USB) on the computer. The "PIC programmer", which is connected at the port, would then move this data into the PIC's program memory under the control of the computer.

Hardware programmer and associate software

Luckily, many of these controlling software and various design of the programmers are also free for download and build. The most stable one are those built using the parallel port to transfer data while there are also designs that connects to the serial port. A certain design called JDM even obtain its power from the serial port to program a PIC. Unfortunately, it relies on the serial port to be able to provide certain voltage and current before it can work. Many are unaware of this limitation and built one just to find out that it would not work properly.

What's next

Once the binary codes is inside the PIC's memory, the PIC will execute them on next power up or on a hardware reset, provided it is fitted with an external crystal or it is programmed to use its internal clock source.

In the next tutorial, we will look at the physical PIC itself. Goto Next Page

Links & URLs

One of the stores selling PICs in UK

Microchip Datasheet and MPLAB IDE download Link