PIC Micro-controllers


New web site at magics-notebook.com


As with all images on this site, click on the thumbnail to see an enlarged picture.


A PIC device is basically a complete computer inside a small chip.  There is an extremely wide range of them available, with various amounts of RAM, ROM and special functions. PIC's require to be programmed using an assembler language, which I decided I didn't have either the time, brains or inclination to learn !. So I took the easy route and purchased a BASIC compiler. This software translates easy to understand basic commands into the assembler language. It also provides simple commands that will automatically generate the assembler code for complex functions, such as controlling Liquid Crystal displays, keyboards, precision timing, waveform generation, analogue to digital conversion, pulse width modulation etc..  All this means that it is very easy to create the programs that actually tell the PIC what to do. Before we go any further lets have a look at the basic PIC 'core' circuit for the very popular 16F84  and 16F628 PIC's.  In order to work the PIC must have certain connections, they are ...


  The 'core' circuit connections are on the left hand side of the diagram and require connection for all applications ( except when we use the internal clock ).  Five volts + is connected to pin 14 and pin 5 is connected to ground.  The timing xtal is connected between pins 15 and 16 and in addition both pins are decoupled to ground via 22pF capacitors, to complete the oscillator circuit. One function of pin 4 is MCLR or as a reset pin. This must normally be connected to the five volt positive line using a 4.7K resistor.       

The pins on the right side of the diagram are for the two ports A and B. Port A is a five bit port and Port B has eight bits. Each bit on these two Ports can be programmed  to provide various input and output functions. The clever stuff is in the programming !.

There are excellent tutorials already written by people who know what they are talking about so fortunately I do not have to show my ignorance on this score !. However having just gone through the process of learning some basics about PIC chips and programming, it may not be amiss to have a look at some of the requirements before you can produce your very first master piece !. 

What you need to start with PIC's

PIC chips.

First we need a PIC !.  Now as much as I love the PIC micro-controller, I am not quite so keen on Microchip who seem to produce a bewildering variety new versions, almost on a daily basis. Microchip also do not comply with a standard architecture across their range which makes software development cumbersome. The knock on effect is that having purchased an expensive piece of development software, such as an assembler or compiler, it is out of date as soon as you purchase it !. They also have a habit of swapping pin positions between types with interchangeable functions, to further confuse the issue.

So the first thing we need to do when choosing a chip is not only ensure it is capable of doing what we want, but we must also check to ensure that the chip chosen is supported by the assembler / compiler and also the hardware programmer.

Illustrated on the left is an 16F877 forty pin PIC. The chip in the center is an 18 pin 16F628 PIC.  The one pound coin is shown for size.

Some PIC types have become very popular and are widely supported by compilers and EPROM programmers. For example, in the past the 16F84 was very widely used in projects.  In practice the 16F84 has been replaced by the 16F628/9, a cheaper yet more versatile version. Another very popular PIC is the 16F877, one of the larger ones in the PIC range. One often finds the same chip in more than one version. These mainly differ in the amount of RAM / ROM that they contain. For example the 16F627 has 1K of flash memory for program storage while the 16F628 has 2K. Maplin's actually charge the same for either chip and in my mind it simply does not make sense to  buy the 16F627, when you can have twice the memory for the same price.. You will find a similar situation with other chips such as the 18F1220/1230. Developers tear their hair out at Microchip's proliferation of chips and would dearly love to see some rationalisation in their range, in addition to maintaining some sort of convention for pin function. In the example of the chips already mentioned a 18F1230 could be used to replace a 16F628 .... except that Microchip has changed the pin functions around !. 

Certainly during the early stages of your experiments I would recommend that you start with a 16F628 or if you want to use a bootloader an 18F1320.  It will be recognised by your compiler and programmer.  Both are cheap ....  and  accidents do happen !.

Assemblers and compilers.

It is software that makes computers 'clever'. There are three levels that we can program a computer, machine code, assembler and compiler. Machine code is no longer used by modern programmers as it consists of a totally incomprehensible string of nought's and ones. The next stage of development was to introduce a program that used fairly easily understood commands, which then converted or 'assembled' them into machine code.  Many people use assembler to write their programs for PIC micro-controllers. Most PIC's only use 35 commands and so it is reasonably easy to learn and can produce fast code. Another approach is to use a 'High Level' language to make the process of producing the required machine code, much easier. In many cases this produces slower code but much less work. Probably the two most popular are BASIC and "C".  If you are a mental masochist try "C".  If you want an easy learning curve and the ability to almost instantly write programs using the most complex functions ..... think about a BASIC compiler.  I chose the Crownhill Proton + BASIC compiler and it does everything that I want of it .... in a friendly manner. Upgrades for this compiler are free of charge for life, very important in an ever changing field. They also have a very active discussion group covering all aspects of PIC's, where one can find experts to solve almost any problem for you.

EPROM Programmer.

Once you have written a program for a PIC, there are two ways to get it into the chips flash program memory. One way is to download the program to two special pins on the chip ... from a PC using a 'serial boot loader' sub-program. Unfortunately this method will only work with chips that are capable of self modifying their own memory content.  The most common PIC used for the development of programs is the 16F877 and this is capable of using a boot loader. So is the 18F1230, but the 16F628 cannot use a boot loader.

In the case where a chip is not capable of self modifying it's own memory content, we have to use an EPROM programmer.  The programmer is normally a 'stand alone' box of tricks that is used to place a program into the flash memory of the PIC.  From an electronic point of view no matter how large the PIC is, only three pins of it are used in the programming process, plus the two supply connections.  This means that we do not have to remove the PIC from it's target application board to program it. Instead we can make up a simple adaptor cable that will connect the programmer ZIF socket to the remote PIC to be programmed. This is usually done by incorporating a small five pin socket on the target board for this purpose.  There are a few other important considerations so I advise you to read Microchip's application note on the subject before you attempt it. Once you have the adaptor it is as simple to program a chip this way as it is to use a boot loader, which may not be available for a recently introduced PIC type.  HOWEVER the programmer must be capable of programming the PIC chosen !. 

Also shown are two other ICSP adaptors I made for the programmer.

The above picture shows the PICPRO40 Programmer that I used to use with the Winpicprog freeware. This program covers the more common chips like the 16F877 and 16F628. 

When I moved to the 18F range of PIC's I found that Winpicprog did not cater for them, so I purchased a new JDM programmer that also came with free software, WinPic800, that appears to cover just about every type of PIC.  It cost about 7 on EBAY and has to be one of my better buys !.

Target Board.

The target board is the PCB belonging to your actual project that you are building, using the PIC. It could be a specially designed printed circuit board or a general purpose development board.  As I progress with PIC's I find myself liking the development board approach.  It means that a single development board can be used over and over again to develop .... and act as the heart ... of many projects.  As we have already seen apart from the 'core circuit connections, (MCLR, V+, V-, Xtal one and 2 connections) all the other pins can be programmed as inputs or outputs, with the alternative of programming specific pins to create special functions.  Since many of the connections are connected to 'off board' components, such as switches, plugs, sockets, pots, etc., it is very easy to design your own mini development and multi-purpose board. I use the word  'board' because this can either be a small PCB or even made of Vero board.


The photograph shows the simple 18 pin development board for the 16F84/627/628, 18F1220/1230 etc. connected up as a serial to parallel LCD converter leaving eight pins free for other purposes. During development work I cannot be bothered with batteries and tend to use 'wall mart' type plug in transformer PSU's. The problem is that I use loads of these in my workshop with different output voltages.  Some have the DC socket polarity one way and some the other. After blowing up a few PIC's by plugging in the  wrong transformer lead, I now automatically fit a full wave rectifier to the target board, that will work with either polarity type. Also on each target board I fit a simple five volt stabilised power supply. 

With the exception of the oscillator pins, most circuits will require only one connection to each PIC pin. However more than one supply connection may be required for 'off board' sensors etc..  I also like to have a reset button on a development board and an LED to indicate when the board is powered up.  Since the board is self contained I always fit a two pin jumper on the board, in line with the power supply output. This enables me to easy monitor current to the target board, which gives a good warning of potential disasters.  You will notice two spare pins on the top of each dual socket block. The left two are Ground and the right two +5 volts giving a total of four connections to both positive and negative supply rails.  All other PIC pins have two connection sockets, which should be more than , ample for most simple applications.

A Simple development board for the 16F877 etc.


Built using Vero board. The box contains my usual power supply mods, plus a serial input output socket for an external RS232 converter. Under the 16F877 PIC is a 24LC256 32K byte EEPROM. In the center are three LED's and also four switches with pull up resistors, that can be jumpered over to the PIC.  The reset switch is top center. In the picture it is running Zapper software.

My very first PIC project was an all singing and all dancing four processor board, with RS232 on all sockets. It was interesting because I was almost completely blind at the time and had to use a Vero board template to drill the holes by feel :-).  I dare not look at the soldering now !. When I re-gained my sight I decided to have another go !.  Since I didn't know anything about PIC's at that time I rather overdid it, with serial as well as parallel LCD displays etc.. However it has served me well. 


John Kent 2002.



Another Look at PIC's.

I think my big step forward since 2002 is that I finally mastered making PCB's !.  I found I actually enjoyed doing the layouts and seeing the final product, so it took me awhile to come to the conclusion that maybe I was wasting time laying out new board designs for every project, when I could use a standard multi-purpose board instead. Next came the idea of putting all the project specific circuit onto a separate PCB that just plugged into the PIC ,motherboard, or in other words ... adopt a modular approach.  

 In many of my projects I tend to use PORT.B for an LCD display.  This can be a 40 character x four line display, or a 40 x 2, 40 x 1, 20 x 4, 20x 2, 20 x 1, 16 x4, 16x 2, 16 x 1  etc, etc.. If you do not want to use an LCD display then you can use these eight bit for any other purpose. If you run short of pins then you can also consider using a serial display, which uses only one port bit.  This is often handy when you want a diagnostic display when developing software for an application and you can remove it from the finished product.

The term ICSP is often used to describe two completely different means of programming a PIC and this can cause confusion. Some PIC's are not capable of modifying their own memory, ie the 16F84, 16F628 etc.  For these we can only use an Eprom blower (programmer). Rather than have to keep swapping the chip between PCB and programmer during development, Microchip introduced a means of programming a PIC in circuit using an adaptor cable between a special socket on the target board and the programmer. However .... I prefer another method. PIC's such as the 16F877, 18F452, 18F1230 etc, are capable of modifying their own memory and this makes it possible to modify the installed software via a serial port on the target board. The software is being developed on a standard PC using a compiler ( Translates one programming language into assembler code that the PIC can use). In this case the compiler has provision to download the compiled software direct to the target PIC  via the PC's RS232 serial port ..... an RS to TTL adaptor (MAX 232) .... straight into two pins on the PIC. It may sound complicated but it works like a dream !. On the diagram of my multi-purpose PIC board I have included a special five pin socket for this type of serial programming. This socket also provides power to the external RS232 to TTL adaptor and a connection to a PIC reset button on the RS232 adaptor box. Let us now have a look at the board ....

Starting from the top middle. Many of my projects are for mobile battery powered applications.  For me it made sense to be able to permanently monitor the state of the external batteries. The AN.0 trimmer pot is there to adjust the battery voltage down to a voltage between 0 and +5 volts. Please note that some PIC's have other means of monitoring battery voltage. The rest of the bits on PORT.A  are available. Don't forget that PORTA.4 is open collector and requires a pull up resistor. 

The DS1307 real time clock is capable of providing seconds, minutes, hours, date, day and year information. It can be used for either 12 or 24 hour mode. A small backup battery keeps the clock running when the PCB is powered down.

The two 24LC512 eprom chips are used for long term data storage and are erasable if required. Recently the 24LS1024 became available and is pin for pin compatible with the 24LC512 chip. I have  included one megabit of eprom on board which will be far more than I can imagine using, but if the need arose I could simply remove one of the 24LS512 chips and plug in a small daughter board to give me a total of 4 mega bits using the 24LC1024 chip. 

The photo of the board shows a 20 mHz xtal fitted to the PIC, which is rated for a 20 mHz xtal..  Here we could introduce a socket so that any frequency xtal can be plugged in. The only justification for using an xtal of lower frequency, ie 4 mHz is that it would allow the PIC to output lower frequency HPWM waveforms. Typically a 20 mHz xtal will allow a lowest useable frequency of 1221 kHz but a 4 mHz xtal will permit a 145 Hz waveform. 


All facilities on PORT.A are made available on a ten pin SIL socket. Also available on this socket are the +5 volts line and ground. This allows the motherboard to power the project board or visa versa. 

Ground and one port bit is available on the  PWM socket as well as the two PWM channels.

Three port bits and ground are available  on the lower spare port socket and four port bits available on the upper spare port socket.

+5 volts can also be taken from the ICSP socket when it is not in use for programming.


Variations on  theme.


This one was for a Function generator and there is an R2R network on PORT B. The four pin socket on the right connects the sine waves and power to the active low pass filter and output board.

You will notice that some of the PIC's have a green sticker on them. This is a reminder to me that I have pre-programmed a 'bootloader' into the chip to allow serial in circuit programming direct from the PC.  Also note that hiding the jumpers, decoupling capacitors etc under the PIC, , makes for a 'clean' board layout.


Getting things done.

 To keep it simple, just think of a PIC as a complete computer in a single chip. Most pins can be programmed for more than one function, so they are very versatile components. Typically pins can be programmed as digital inputs or outputs, analogue inputs and by adding a single resistor and a couple of capacitors they can also give analogue outputs. Most have at least three timers and these enable timing and waveform generation. The smaller PIC's have at least one CCP channel which can generate precise square-wave pulse trains with selectable duty cycle. With the addition of a few passive components they can be made to generate pretty good sine waves  and complex waveforms. Many have other functions including comparators, half and full wave drivers etc.. , 


A design for a the 16F877 or 18F452 PIC board.

As an introduction to PIC's let us have a look at a simple design for a single board PIC computer suitable for incorporation into a metal detector. The 16F877, which is probably over the top for this application, but it does have two PWM output channels.  I decided to build the processor circuit on it's own PCB and utilise a second 'piggy back' PCB with the detector electronics on it. When laying out processor PCB's I like to retain as many of the chips functions as possible .... even though they may not all be required for a particular application. So Here is the PCB layout  and component overlay. You may have to adjust them to a 0.1" matrix, before printing ......



(Copyright John Kent. June 2007)

Remember that you are looking at the tracks ... through the PCB board !. Not all of the components are shown. In the center is the 40 pin 16F877, which I hope is going to do all the clever stuff. Right center is a 10 pin socket for an LCD display. The pinout is standard for most LCD's. For development purposes I will be using a two line x 40 character display, but for an actual detector a 16 x two line LCD would probably be more than sufficient. Bottom right is a spare two bit port with +/- 5 volts. Top left is a 5 volt two pin power supply socket. Just under that is a spare eight bit port with +/- 5 volts. This particular port we will be using for analogue inputs. Left center are the pads for a 4 mHz xtal and it's two decoupling capacitors.  Bottom left is a six bit port, again with +/- 5 volts. Pins 16 and 17 of this port are the CCP PWM channels we will be using for the transmitter and reference signals. Bottom center is a five pin socket that will be used to program the 16F877 PIC during development .... using a bootloader ... which has already been programmed into the PIC. The pinout for this socket looking at it from the component side of the board is ......

1.    Tx.                        PIC pin 25

2.    Rx.                       PIC pin 26

3     Ground.                 PIC pins 12 and 31

4.    +5volts.                PIC pins 11 and 32

5.    MCLR (reset).      PIC pin 1.


Here is the PCB, that I made up this afternoon......

It still has to be trimmed to size and the holes drilled.



I will be using the Proton+ compiler to program the PIC micro-processor. Micro-processors are normally programmed in a rather cryptic assembler language, that I do not have the time or inclination to learn. Most people know how to program in BASIC, and the purpose of the compiler is to take the easy to understand BASIC program and to convert it into assembler code, which has the advantage that it will run much faster than BASIC.

The compiler software resides in the users PC and the programs are written there. To transfer them from the PC to the PIC we use a serial output port and cable. This port conforms to the RS232 standard and requires converting to a 5 volt signal that is required to program the PIC. The converter is a single chip, that is powered from the programming socket on our PCB. In our case we will only be sending data to the PIC via the Rx pin. After sending a program correction to the PIC we need to reset it, before it becomes effective and that is the reason for the presence of the MCLR line on the programming socket. The converter chip, which is usually housed in a small plastic box, is powered from the +5 volts and ground pins on the programming socket. Below is a photo of the RS232 converter box ...

wpe1.gif (16366 bytes)

            Click on thumbnail to see circuit full size.




The serial port cable from the PC plugs into the left hand 9 pin socket. The black cable on the right, plugs into the PIC PCB programming socket. In my case I will have to use a short adaptor cable between this cable and the PIC PCB socket. The press switch on the top of the box is used to reset the PIC during programming.  In practice the whole thing works very well. After the PIC program has been finalised, one unplugs the programming cable and the device will work as a 'stand alone' unit. 



The programming set up.

The PIC board is now populated and it is time to wire together all the elements that we will be using to develop the  PIC program. 




The round green sticker on the PIC tells me it has been preloaded with a short bootloader program, which will allow in circuit program modification. The alternative would have been to re-program the PIC after every software change using an Eprom programmer. It is possible to buy PIC chips with a bootloader pre-programmed (Mechanique), otherwise you would need an Eprom programmer to preload your own chips as I have done.  I bought my Eprom programmer off EBAY for around 7 plus postage and packing ....


The JDM programmer. It comes complete with a serial cable and free software that will program just about any PIC. Considering the 40 way ZIF socket would cost about 10 in UK, it really was a gift at that price !.  The programming software to drive the JDM is WinPic800. You can find full details of all the PIC's it can program at ....


Where you can also download a free copy. WinPic800 can be used with many different types of programmer. To use the programmer we plug the serial cable from the PC into the JDM and insert the PIC into the Zero Insertion Force socket on the JDM . There is a small diagram on the bottom right corner that shows the correct orientation of the PIC.  There is also a jumper to be set on the JDM, On the left for 8,14,18 and 20 pin chips and to the right for 20 and forty pin chips. Again it is clearly marked. 

Next we access the WinPic800 program and load the program we intend to program the PIC with.  Usually  I only want to program into the PIC  a short bootloader program, that allows serial programming via the PC.  There is a bootloader for each xtal frequency and PIC type. On my computer the bootloader files can be found at .....

 C:\Program Files\ProtonIDE\MCLoader\LoaderHex

The files will look something like this, except there will be a lot more of them, to cover all PIC types and Xtal frequencies.


So now have a guess what the bootloader file name would be for a 18F1320 using a 20 mHz xtal. Easy isn't it ?

Select the one you want and it will load into WinPic800, which will them program it into the PIC sitting in the JDM programmer. Once this is done and the data verified by WinPic800, you can remove the PIC from the JDM and plug it into your target PCB board and live happily ever after !.. 

Please note the we BASIC types need not worry about the gibberish shown in the window. This will be the  Hex code for the small bootloader program. 

The next step is to remove the PC serial cable from the JDM and plug it into your RS232 converter box. You are now ready to download the PIC software program from the compiler on the PC .... via the RS232 converter box .... to the programming socket on your target PCB board.  To accomplish this the compiler will ask you to reset the PIC and you do this by simply pressing the red button on the RS232 box shown below.  This puts a momentary short to ground on the MCLR pin of the PIC and it resets, allowing the PIC program to be downloaded to the PIC. As soon as the program has downloaded it will run.

If you need to modify your PIC program, just do it in the compiler on the PC, re-compile and down load it by pressing the reset button.  PIC's can generally be reprogrammed about 100,000 times this way. 


The programming development setup. Left is a stabilised 5 volt power supply. The grey box at the top is the RS232 converter, which will only be used for programming. The output of the converter connects to the PIC PCB five pin programming socket, via an adaptor cable. The 40 x 2 LCD display is plugged into the PIC PCB again via an adaptor cable. Running on the monitor is the Proton+ Basic compiler.  




The Proton+ Basic Compiler by Crownhill.


Let us assume I want to do something simple like print the word "something" onto an LCD display at character position number 14 on line 2. To do it in assembler would take reams of incomprehensible  assembler code .... that I simply would be unable to produce, nor would I want to !. On the other hand I have been using the BASIC language to write programs that translate languages, construct ballistic tables with retardation and decipher secret messages for example. The only reason I could do those things was because BASIC uses words I already know and syntax that is easy to understand.  If we look at the simple little problem I mentioned above and put it into plain English, it would look something like this ....

Print on line 2, at character position number 14 the word "something".

In Proton BASIC we simply say ....

Print at 2, 14, "something"

For bit manipulation we might want to make bit 3 of port B go to a high state ....

High PortB.3

If we wanted to generate a variable duty waveform of  2000 Hz, with a duty cycle of 25% on the output of the CCP2 module, in Proton BASIC it would translate to ...

HPWM 2,64, 2000     ( The 64 stands for 64/255 = 25%)

If we start to talk about multiple floating point calculations involving complex trig functions .... forget about it ! .... unless you are using a decent Basic compiler !.  The latter enables one to produce results ....  while others only produce excuses !. 

I believe that one can download a free version pf Proton+ from Crownhill, that has a 50 line limit, which is certainly enough for evaluation purposes and many of the smaller projects. I was just thinking of the days when we missed out all the spaces between words to save memory and multiple code  lines were never much less than 255 bytes !.  Proton also now comes with an IDE, which for beginners will be a great introduction to the subject.  The free version is limited to about four PIC types but the 16F628 and the 16F877 are included. Apart from that, it is fully functional and non expiring. You can download a copy from ...... 


They have a very good Forum in the English style at .....



Also included with Proton+ is a cut down version of Proteus Virtual System Modeling software, which is now an integrated part of the Proton  development suite. The idea is you design and test your software on the PC, rather than a conventional 'bread board'. If you would like to see what it is all about go to ....


....... where you can download a free version. 

With  the freebie version of Proteus there are only six virtual Hardware 'boards' which contain things like virtual display, keyboards, terminals and virtual test equipment such as oscilloscopes, logic analysers etc.. Fortunately they do include boards for our old friends the 16F628 and 16F877 PIC's which are ideal for beginners to learn with. If you eventually want to develop a design for a PIC that they have not provided a virtual hardware board for you can still do it. This is because PIC programs written with Proton are largely portable. For example if I eventually want to use an 18F452, it has the same pinout as the 16F877, so I can develop the program for a 16F877 and when I have it working, I only need change the device declaration on one line to make it work on the 18F452. In fact they have provided virtual hardware boards for the 18F452 as well ...... Lucky me !.





Proton comes with a large library of example programs that cover just about everything a PIC is capable of. These examples are written for specific chips. However if you want to use any of these on other pin compatible PIC's all you need do is change the device declaration to the PIC you want to use.  A typical Device declaration line  would be ......

Device = 18F452

It is as easy as that !.  If you want to use a different frequency Xtal then you simply change the Xtal frequency declaration line .....

Xtal = 20 

OK let's have a look at a real program written with Proton+. Please note that anything following an apostrophe is simply a comment and is ignored by the compiler. This simple little program sets up bit zero of Port A as a serial input working at 9600 baud inverted and then prints the received characters onto an LCD display.


Device 16F84                                         ' We are using a PIC16F84

XTAL = 4                                                '  And a 4MHz crystal

RSIN_PIN = PORTA.0                             ' Serial in at PORTA bit-0

SERIAL_BAUD = 9600                             '9600 Baud

RSOUT_MODE = Inverted                        ' Inverted

Dim Byte_In As Byte                      


Again: Byte_In = RSIn                              'Read the serial data

Print Byte_In                                           ' Display the serial data

GoTo Again                                              ' Do it forever



The PIC mindset.

Probably the best bit of advice I can give, is before you use a new type of PIC is to download the data sheet and read it. These can be downloaded from Microchips web site at ....


Also on the same site you can find the Application Notes (AN's) for each PIC type and some of these can be very informative. You will also find educational articles, and free software. 

I found that as I progressed with PIC's I had to change some of my ideas. Back in the 60's we were promised multi-purpose components, that could be programmed to produce any function. This was at a time when a single computer cost the earth and for many years it seemed impossible that any design would ever include two processors!. This left some of us with a hang-up about including more than one micro-processor into a design. The reality is that you can use as many as you need to. . A good example is where you need to generate waveforms of more than one frequency. A  16F877 for example has two CCP modules but both have to be on the same frequency, although they can both have different duty cycles.  The economics of the problem rule that it is cheaper to use two low price chips than one high level expensive one that has more than two CCP modules. 

If we take the idea a little further, it makes good sense for a peripheral device, to be able to continue to function, even though the main processor might fail. If the peripheral device has it's own micro-processor then this becomes possible.  If we take this concept to the extreme we are talking about "Smart Dust". The problem with concepts, is that they are useless without an application and these are appearing in abundance.