THE B.A.E.C. NEWSLETTER

No. 140, August 2001.

How to get started in PIC Programming

By: Eric Edwards GW8LJJ
[Jun06] Email: eric@eddy11.fsnet.co.uk.

Several years ago a colleague suggested that I should be using PICs for my hobby, which then was amateur television. I spent many hours developing and experimenting with video mixers, faders and effects (FX) units, and all this was done sing analogue and digital circuits (TTL gates) that took quite a while to get the desired results. I did not know at that time what he was talking about. What were these PIC things? What could they do for me, and my hobby? How was I going to be able to use them anyway? I felt that I was getting on OK with my method of designing and building circuits using conventional integrated circuit and transistors.

It was a little later when I was told that I could replace a very complicated video switching circuit (manual and sequential switching) using eight TTL type gates and monostables and many other components with just one PIC chip. It could do a lot more than the printed circuit board full of 'standard' integrated circuits too! How was all this possible with just one chip, and how was I able to "tell it" to do anything. I has no experience of microprocessors and programming. I couldn't even turn a computer on, and when I eventually did, I couldn't do anything with it, other than stare at the 'blinking' cursor (pun intended)! My background is radio and television and during those years whilst others were increasing their knowledge and programming skills with the early personal computers, I was experimenting with video and television circuitry. I had neither the need, nor the interest for programming of any description. My way of thinking about programming and using micro chips soon altered however, when I gained the information how to program and use a PIC to do exactly what I wanted it to do. All my project building is now done with a PIC as the main component. Well, it's so easy!

You too, are about to learn, quite easily, how to use these PIC chips, what they are anyway, and what you need to get started. The PIC I shall be using is the cheap but powerful PIC16F84. This is very reasonably priced (about £2 from Crownhill Associates Ltd), and it is this chip that I recently gained experience with an it is what I am using today.

You will learn the jargon and terminology that goes with these devices and you will also be able to understand the data tables (only the stuff you need to know), and any other words and phrases that we come across. I will be showing you as well as telling you what you need to know. Firstly, let's find out a little bit about these devices then, throughout the pages of this Newsletter I shall be explaining in simple terms all about programming PICs in general and introducing you to the various software and hardware requirements. After a small amount of study, you will be wondering how you managed without them.

What is Programming

This is a list of commands and is the instructions that are placed inside a chip that will eventually do what you have told it to do. The programming starts with the written word, and this will vary depending on what language the source code will be written in. OK, let's look at that again...

Programming languages are the types or names of various software source codes. This starts with the earliest called BASIC (an abbreviation for Begineers All-purpose Symbolic Instruction Code, which is a programming language that was developed in the mid-1960s at Dartmouth College by professors John Kemeny and Thomas Kurtz). BASIC has English-like commands that make it easy to learn. It was supplied by the Microsoft Corporation for each new computer in the late 1970's and continued to do so with up-rated versions. Eventually it was not incorporated in the newer machines. There were variations of this programming language such as GWBASIC, QBASIC, QUICKBASIC and others. Another variation of this language is used in PIC programming.

First things first, let's find out what these chips are what they can do.

What is a PIC

PIC 18-DIL Package

A PIC is, well, an abbreviation for "Programmable Interface Controller." What that means is, it is a micro controller integrated circuit manufactured by Arizona Microchip of America, which is a microprocessor containing its own programme code, command code memory along with data storage memory, bi-directional (input/output) ports and its own clock oscillator. There are other PIC chips that do a great deal more too! The main advantage, I suppose, of this device is that it can perform many functions that other devices would need extra circuitry for. There are quite a few different varieties of PIC chips with some offering very powerful services. As well as being a`controller' for other add-on devices, it is a stand-alone product in itself, in as much as it can drive LEDs (Light Emitting Diodes) and transistors directly and produces some interesting projects once it has been programmed. It needs to be programmed, and you will be able to do that yourself after reading more of this article, as it cannot do anything until then. It is just a piece of plastic to you until it has the necessary binary noughts and ones placed inside by the means of programming software and hardware.

The PIC 16F84

This is a 16F84 PIC and is available as a DIP (Dual In Line Package) as shown, and as surface-mount (SO) chips measuring about half the length. There are other types that are much larger having 40 pins and even more. In this article we are only going to use the 16F84, as it is the cheapest and probably the easiest to use. It is also re- programmable by simply writing over it with new data. Don't worry, all will be revealed soon so that you will not be left in the dark!

To show the advantages of using a PIC I am going to compare two circuits of the same project. Don't worry at this stage how the PIC can do all these complicated functions and replace a lot of `standard' components, I shall be telling you all about it soon and you will be able to understand it all!

Let's build a circuit using conventional integrated circuits. Counting Lights, Using Conventional Components

This is a circuit using a standard counter integrated circuit and an NE555 timer to turn LEDs on in a sequence and counting up in binary.

Plugboard

A Binary Counter Circuit layout

This circuit layout on breadboard is of a binary counter using an NE555 timer as the pulse generator and a SN7493 as the binary counter. A better device as the binary counter however, is the CMOS (Complimentary metal oxide semiconductor) 40240B. This is a 7-stage ripple-carry binary counter/divider and is the circuit I have decided to use as my example of a counter using 'standard' integrated circuits. The timer is wired as an astable multivibrator producing a pulse (square-wave) at about a second interval. This pulse is connected to the input of the counter chip and there are 7 LEDs indicating a binary count from 0 to 127.

Osciloscope

Square-waves Produced By The NE555

Binary counters Resistor+LED soldered together

A Binary Counter using `Standard' integrated circuits

In the above circuit diagram, an NE555 timer is used as a pulse generator producing pulses at about one-second duration. This is governed by the timing components connected to pins 2, 6, and 7. If the 100µF capacitor connected on pins 2 and 6 were reduced to 47µF, then the counting pulses will be about 1/2 second each. The output of the pulse generator at pin 3 is connected to pin 1 of the binary counter 4042B. The actual device is a 4042 but are usually advertised as CD4042BE, CD 4042BM, CD 4042BC and so on, but as far as we are concerned in this application they are all the same and will give the same results.

When the circuit is connected to a power supply of 5-volts, The LEDs will start counting in binary notation. The first LED connected on pin 12 will flash on then off at about a second duration with the timing components used in the circuit diagram. When this LED goes out, LED number two comes on for one second then goes off and number one LED comes back on again with LED two. After a second, both these LEDs go out and LED number three comes on. This has now produced a count, in binary, of three. It carries on counting until all the LEDs are lit and then they all turn off and the whole sequence is started all over again. Let's have a look at this binary counting method just in case you are not familiar with it or maybe you have forgotten how it works...

Counting In Binary

We count (on our fingers) in DECIMAL and as the term suggests, uses ten digits, 0 to 9. When we started counting many years ago we used our fingers, and as we have ten of them (including thumbs) it is convenient to count using digits 0 to 9. I wonder at what it would have been called if, in the beginning, we had used our toes as well? (Actually, that's called vigesimal). Binary, on the other hand, or is it foot (?), uses only two digits, 0 and 1 and are called BITs, an abbreviation for BInary digiT.

Let's look at a table drawn to show the relationship between Binary and Decimal...

BINARYDECIMAL
0000000100
0000001001
0000001102
0000010003
0000010104
0000011005
0000011106
0000100007
0000100108
0000101009

The Decimal numbers count 0 to 9 (units) then the left-hand column changes to 1 (tens) and the count starts again but this time with a '1' in front of the digits 0 to 9, whereas the Binary numbers only use 0 and 1, and starting from the right, this increments in steps of one as the decimal increases 0 to 9. Reaching 10 in decimal the count starts again in the right-hand side (the units)... The binary step in two's starting from the right, 1, 2, 4, 8, 16 and so on, so 00000001 is 1 in decimal and the next 1 to the left of the first, is 2 (00000010). If the binary is shown as 00000011, then the decimal is 1 plus 2 = 3.

BINARYDECIMAL
0000101111
0000110012
0000110113
0000111014
0000111115
0001000016
0001000117
0001001018
0001001119
0001010020

You can see from the table how the binary number is built up from the decimal equivalent.

Let's have a look now at a circuit diagram that uses a PIC to do the same function...

PIC_with_Output_LEDs

A PIC Circuit Diagram To Count In Binary

That's it a PIC, two ¼ watt resistors and a capacitor. This is absolutely useless of course until it has a program written into it. Well, let's do that...

The Program Source Code

Dirs = 255
 Run: For B0 = 0 to 255
        Pins = B0
         Pause 200
      Next B0
Goto Run
That's it! No more to type. When this code is placed in the PIC BASIC compiler (PBC) it will create a HEX (Hexadecimal) code that will be suitable to be used with a wide variety of PIC programmers, some of which, are available as free downloads from the WEB.

Let's have a look at the code in more detail to see how it works...

Dirs = 255
This is setting up one of the PIC registers and is "telling" the PIC to set the PortB (there are two ports on the PIC 16F84, of which more later) as outputs to illuminate the LEDs.
 Run: For B0 = 0 to 255
The main program starts here with a routine called RUN. This can be called almost anything, and it is convenient to call it something that represents what the program is going to do. I could have called it LOOP, as it is looping around. RUN, or WALK OR BIN (Binary) or whatever, within reason. It is starting to count from 0 then it goes to 1 (this is RB0) and then counts up to 255 (all the pins, Binary 11111111).

        Pins = B0
A PICBASIC command that places whatever value is in the variable B0 (in our case any number from 0 to 255) on the PortB port and illuminating the appropriate LED.
        Pause 200
With a PIC running at 4MHz, this is a delay of about 200 mSecs. Without any delay, the program would run so fast that you will not be able to see the binary count, as it will appear if all the LEDs have turned on all at the same time.
     Next B0
Part of the FOR / NEXT routine and is used to count from 0 to 255. After it has completed its count, it then goes to the next line of the program...
Goto Run
Which will take it back to the start, and will count all over again. Without the RUN / GOTO RUN commands, it would have counted 0 to 255 and ended.

Sorry: The PIC circuit is shown with seven LEDs there should be another LED placed on pin 13 to bring the number to eight LED's. That's one more than the current using the conventional circuits.

The advantage of using PIC programming for building circuits is that the circuit function can be altered without or with little modification, as all the changes are done in software.

Look at this modified code...

Dirs = 255
 Run: For B0 = 0 to 8
        High BO: Pause 200
        Low B0: Pause 200
      Next B0
Goto Run
This program when programmed into the PIC will turn on and off at a period of about 200 mSecs, the same LEDs connected in exactly the same way on PortB, one at a time from pin 0 to 7 (The pins are labelled 0 to 7 and not 1 to 8 on the PIC). All that has been done is to change the software.