[Company Logo Image]        PIC Projects 

Home Contents Search Articles Caveat Purpose Notice                                                           

Annex A
Annex B
Annex C
Code Tips

PIC Programming

by CatsEyes and Ironsides

(with comments from Michele Schieppati)

version 33 dated 06 February 2004

Executive Summary

The 12F629/675 PICs are small 8 pin DIP devices that are ideal for our applications.  They include and internal oscillator, a comparator, an analog/digital converter and timers. 

The Microchip PICkit1 is an ideal USB programmer.  PicBasic Pro (PBP) is an easy to use package for those who have no penchant for learning the arcane language of assembler.  PBP can be integrated with a free download from Mecanique called MicroCode Studio (MCS) that makes programming in PBP much easier.  Microchip MPLAB is a free and superb compiler that can be downloaded from Microchip.  The real treat is that MCS integrates with MPLAB and PICkit1

HI-TECH Software has release of a totally free ANSI C compiler to support selected Microchip devices. The HI-TECH PICC Lite compiler is a freeware version  and supports the 16F877, 16F877A, 12F675, 12F629, 16F627, 16C84, 16F84 and 16F84A devices.   http://www.htsoft.com/products/piclite/index.html

IC Prog is a free compiler (assembler to hexidecimal) that can be downloaded.

Free PIC samples can be obtained from Microchip.

Free software to permit schematic and Printed Circuit Board design can be downloaded as ExpressPCB or from PCB123

The tale below outlines the tortured path that led Ironsides to these conclusions.


This article was written to trace the steps taken by Ironsides in his quest to understand and program PICs for use in radio control applications.  CatsEyes acted as mentor.  The primary interest was sparked by the use of PICs to trigger digital cameras in aerial photography.  However, it soon became obvious that PICs opened up a whole Pandora's box of possibilities for radio control in a number of applications.  Besides a single step operation such as trip a shutter, the programming of a PIC with multiple output lines offers up many possibilities, many of which derive from the extensive use of PICs in robotics.  For example, an airborne PIC could be used to give a much more realistic undercarriage operation with multiple steps that are spaced in operation - using only one radio channel for to control a sequence.  

      Example 1            Retracting Gear            

  1. Undo the latch

  2. Pause

  3. Open the gear doors

  4. Pause

  5. Drop the gear

Micro-switch detects gear down and resets PIC to reverse the operation when the Transmitter switch is toggled.


Example 2  Twin Engine Throttle Control

Use the gear switch ON/OFF to toggle differential throttle control.

In the gear ON phase, the rudder channel passes differential control to the throttles. 

Note:  Don't forget to select gear OFF before takeoff ;-)

Use:  Twin Engine/Motor Flying Boats - Taxiing on water in wind

Other applications that have either been implemented or are under consideration are shown in the sidebar under DEVELOPERS.

The article is not meant to be a scientific treatise.  It merely catalogues the essence of the process in learning how to DIY in PIC creation.

Introducing the PIC

So what is a PIC?  By one definition, it is a Turkish cloth measure, varying from 18 to 20 inches.  However, for our purposes it is either a (P)eripheral  (I)nterface (C)ontroller, or a (P)rogrammable (I)nterrupt (C)ontroller.  Basically, it allows you to write a computer program that can then be programmed onto this tiny chip.  The chip, when powered by about 5 volts DC and connected to your airborne camera, will happily run the program using practically no electricity and execute commands you issue from your transmitter.

A PIC is preferable to a servo in that the PIC weighs next to nothing ( it is about the size of a finger nail) and reacts much quicker than a servo.  It is so small that in many cases it can be housed with the body of the camera.

Note:  In this article the type of PIC being discussed is the newer FLASH type.  The older chips were "burned" by a programmer and required ultraviolet radiation to erase them. The newer flash types can be electrically erased and reprogrammed over and over.  Great for hobby experimentation where mistakes are part of the learning curve.

The beauty of the PIC we will use is its ability to decode the transmitted signal. The servo pulse occurs once every 20 milliseconds. The pulse itself varies in duration from approx. 1.0 milliseconds to 2.0 milliseconds, and it is this variation that contains the information you want to extract. With the stick at one extreme, the pulse for that channel would be 1.0 milliseconds long, for a centered stick, the pulse is 1.5 milliseconds long, and for the stick at the other end, the pulse is 2.0 milliseconds long. (All these are approximate and will vary with equipment used etc.)  Note:  A millisecond is 1 one thousandth of a second.

So to "decode" the signal, you just have to time its duration. There are several ways to do this, but perhaps the easiest is to use one of the built-in timers within a PIC such as the 12F629 or 12F675.  Start the PIC timer at the start of the pulse (when the input pin goes high).  At the end of the pulse (when the input pin goes back low), stop the PIC timer and read its value. The value you get from the timer will tell you how long the pulse was, and consequently the position of the stick on that channel.  

Credit:  http://www.pages.drexel.edu/~kws23/PWM.html

In real terms, this means that if an unused channel on the transmitter is tied to the camera, left stick or switch down would be camera OFF and right stick or switch up would be TAKE A PHOTO.  To see this concept in more detail click on TIMER

PIC Used for Digital Camera Control

When you first try aerial photography, a servo works well for you to try some experiments quickly and at minimum cost.  As you progress, you might find that you want to take advantage of an electronic switch to trip the shutter.  

As a broad generalization, cameras divide themselves into two categories.  Simple point and shoots with fixed focus are a single stage operation.  However, cameras that employ auto-focus are two stage affairs:  the first pressure activates the sequence and attempts to get focus lock and set exposure values while the second stage triggers the "shutter".  

Both single and two stage digitals may also have a sleep function that puts the camera into hibernation after a period of non-use.  These cameras can be woken up by sending a trigger pulse to take a photo before hibernation sets in or by keeping the camera awake through bypass circuitry, usually revolving around the USB port.

A classic example of the single stage electronic trigger for the "old" Aiptek Pencam® digital has been documented in detail by Mr. RC-CAM.  The concepts are so fundamental that you should click on the DEVELOPERS sidebar tab and read his piece before going any further.  Note that Mr. RC-CAM reserves all rights and his conditions for use are detailed on his web site.


The physical construction has been documented by 'simingx'  in the RC Groups thread:


On many single stage cameras, it is quite simple to find the shutter trigger (Hint:  It is usually right under the button that you press down).  An excellent example is provided by Michele Schieppati with his documentation of how to trigger the Mustek GSmart Mini 3® with the single stage "Schieppati Switch". 

Note: Michele indicates there is a little difference between his switch and the others described on these pages, mainly in the output stage.  The other switches work by sending a signal (pulse) to the camera to simulate the shutter activation. Instead of giving out a pulse Michele ‘replaces’ the original switch with a transistor. Using the pulse solution you have to send the pulse at the correct polarity (active high or active low) and with the right voltage, as the microcontroller works with BEC power supply at 5V and most cameras have a 3.3 supply voltage. This means that you need different switch versions or a switch with different solutions (different wiring, jumper etc) to be compatible with different cameras. Using a transistor is like connecting in parallel to the existing switch a second switch, with the only exception that the transistor, when activated, ‘closes’ the circuit in one direction only. In this way, by simply reversing the two wires it is possible to adapt the switch to the required polarity!

In September, 2003, Michele announced the ‘Twin Switch’ as a special version of his single stage switch that can control, with two independent outputs, the double-step (focus - shutter) used in some cameras. 

Note that Michele reserves all rights to his inventions and if you want purchase switches the details are all posted at:  http://www.rc-flysoft.com

Photo courtesy Karl BlazevicHowever, be advised that many cameras are not as easy as the Aiptek Pencam® and the Mustek GSmart Mini 3®.  In many cases the circuitry is densely packed and you need very high soldering skills commensurate with this Surface Mounted Technology (SMT), or Surface Mount Device (SMD).  Crudely put, the traditional pencil style soldering iron is far too large and puts out enough area heat to melt the solder under the SMT/SMD components, floating them off the circuit board.  Equally, the distance between traces is measured in thousandths of an inch.  SMT/SMD soldering is a skill unto itself.  Hint:  If you did not know what SMT/SMD meant before you read this, go get some help.

Electronic Speed Controller

Having got this far, Ironsides suddenly realised that, once you figure out how to decode the Pulse Wave Modulation (PWM), an ESC is nothing more than a PIC with a Field Effect Transistor to control current flow to an electric motor.  This is described in a roughly parallel way in this reference: 


"One common application of PWM signals is motor control. By varying the duty cycle of a PWM signal sent to a motor, you can vary the effective power of the signal and thereby slow the motor down or speed the motor up depending on how long of a pulse you send to the motor. The signal generated by the PIC can not be directly connected to the motor, however, because the PIC is unable to handle the power required by the motor. It is therefore necessary to use a transistor to regulate the flow of current to the motor. A transistor is like an electric switch. When you send a logic high (+5V) to the transistor, it allows current to flow. When a logic low (0V) is sent, it restricts the flow of current. For digital signals, this means that the signal can be reproduced exactly, except the new signal is scaled up to a much larger current. Figure 2 shows a schematic for controlling a motor using a TIP31 NPN transistor.

Photo by 'Drexel'

As the schematic shows, the output from the PIC is wired to the base. The negative terminal of the motor is then connected to the base and the collector is connected to ground. When the PWM output from the PIC is sent to the transistor, it will flip the transistor on and off and subsequently generate the same PWM signal to the motor, allowing you to control the motor with a PWM signal. "

The Challenge

To complete a PIC project we just need to assemble the basic components:

bulletA 'How To' Cookbook
bulletA prototype circuit
bulletComputer software to write the PIC code
bulletA suitable PIC
bulletA PIC programmer
bulletA ton of patience


A tutorial that provides good detail of the whole PIC process can be found at:


Another that might be a bit closer to the radio control hobby can be found at:


As another starting point, Mr. RC-CAM has pointed us to this source that deals specifically with servo control:


The Prototype Board

In the electronics hobby world, a "breadboard" (shown below) is about the single best investment you can make.  Rather than being used to cut bread, it is a flexible device with rows of interconnected traces going vertically or horizontally, depending on location.  It is used to prototype circuits.  Once a circuit has been proven, then a comparable circuit board can be used to solder the components into pace.

In the example below, Ironsides used the El Cheapo programmer on the left side, connected to the software on his PC through the Parallel Port,  to program the 16F84A in the centre.  The program was testing an application programmed into the PIC that senses the position of a switch and lights the corresponding LEDs.

Photo by Ironsides

Note: the 12F629/675 is only an 8 pin PIC while the 16F84A is an 18 pin device.  The 12F629/675, due to its smaller size and more flexible capability, will be our choice PIC for airborne usage.  The 16F84A is an older device that has been widely used by hobbyists; consequently, there is more material published on it and it makes a good training aid.  A beginner should have a look at this site:


The Software

The place to start is to go to the Microchip® web site and download and install MPLAB®, their Integrated Development Environment (IDE). The latest version (about 26 meg) can be found here:


The MPLAB® IDE allows you to write your PIC code, assemble it, and run a simulator for debugging. It's not bad for a freebee! It is also capable of writing directly to SOME programmers.  MPLAB® does not program the PIC using the "El Cheapo®" or "OREO LM399" ; instead, the IC-Prog® software is needed (See Reference below).

The IC-Prog® is just the driver for the programmer. It takes a hex file and (along with the programmer hardware) programs it into the chip. As such, it comes later in the development cycle.

The Programmer

The programmer can be a home-brew based on the ubiquitous LM339 quad comparator.  One was designed by a member of the Ottawa Region Electric Organization (OREO) and uses the IC-Prog® software.  Hereinafter named the "OREO LM399".  CatsEyes uses the OREO to program the 12F675.

The home-built version of the "El Cheapo ®" was Ironsides initial choice.  (See Reference below).  It can be programmed using its own programming interface.  Equally, if you know a very important trick, it can be used with IC-Prog to program PICs, such as the 12F629/675, that are not supported through the El Cheapo Windows interface.  The hitherto undocumented trick, which took CatsEyes and Ironsides the better part of a week to determine, is to invert the data out line in the IC-Prog interface under the hardware configuration window.  See the sidebar for details.

For the more modern flash PICs, Michele Schieppati pointed Ironsides to the Microchip PICkit-1 by Microchip that is offered at $US 36, plus taxes, shipping etc. It has also a free area on the PCB for adding some components for small circuit evaluation and a USB port for PC connection. It works with the standard MPLAB software, BUT only supports the newest flash PICs  such as the 12F629, 12F675, 16F630,  16F676. You can find the details at on the sidebar or at www.microchip.com

The image below is Ironsides home brewed version of Myke Predko's "El Cheapo"

Photo by Ironsides

The PIC Chips

Go to the Microchip web site to get the datasheets for the particular chip(s) you're interested in. You can select the device from this page:  http://www.microchip.com/1010/search/prodsel/index.htm

Under the "PICmicro® Microcontrollers" heading, drop down the list box, select the device and hit "Go".

Initially, Ironsides will be working with the entry-level 16F84A while CatsEyes has chosen the more advanced 12F629/675.  Note:  The 'El Cheapo' will program the 16F84A directly but the ICProg must be invoked and told to use the 'El Cheapo' in order to program the more modern 16F629 and 12F675.

Unlike the older style chips, the new ones can be reprogrammed over and over - up to 1,000 times in some cases.  That's an experimenter's dream come true.  The pinouts of CatsEyes PICs are shown below, while Ironsides 16F84A is shown a bit later. 

Image by Microchip

The difference between the PIC12F629 and 675 is that the 675 has an on-chip 10 bit Analog/Digital converter and the 629 doesn't.   [Michele Schieppati adds that the 12F629 has an analog comparator instead of the converter present in the 12F675].

Code for the 629 will run on the 675, and code for the 675 will run on the 629, unless it uses the A/D.

Both chips cost less than $Cdn 3.00 - hard to imagine!

A good source on-line for the PIC chips is Digikey® at:  http://www.digikey.com

The Code

The overview of the whole process has been summarized by CatsEyes, using the MPLAB GUI development environment.  We need to write some code in ASSEMBLER, or borrow a sample, COMPILE it into HEXIDECIMAL code and finally PROGRAM the hexidecimal code into the PIC.

Assembler is written in pseudo-English and the code can be commented by using semi-colons that will be ignored by the compiler.  Comments are extremely important in that code quickly becomes gibberish if you do not remind yourself how each step works.  The compiler translates your assembler code into machine language hexidecimal that includes specific addresses and instructions for the PIC to execute.

CatsEyes suggests, that when creating a masterpiece for your PIC, take the following steps:

  1. Create a new directory for your project.
  2. Take the text of sample code, or write your own from scratch, and save it with an extension of .ASM (e.g. LED_flasher.asm)
  3. Load the MPLAB software. It probably installed an icon on your desktop, but if not, from the "Start" menu choose "Microchip MPLAB IDE -> MPLAB IDE".
  4. Create a "project". From the menu, select Project -> New...
  5. Give the project a name (e.g. "LED_flasher"), use the "Browse..." button to locate the project directory you created in step 1, and click on "OK".
  6. Select the device. From the menu, Configure -> Select Device... Choose the 12F625 or 16F84A etc and hit "OK".
  7. In the project box, right-click on "Source Files" and select "Add Files..."
  8. Select your source file (e.g. LED_flasher.asm). It will then appear under "Source Files" in the project box. Double-click it to bring it up in an editor window in case you need to make changes.
  9. Hit the "Make" button (or from the menu, Project -> Make)
  10. Fix any errors and hit "Make" again until you get a clean compile. Note: ignore any warning # 302 for now.
  11. Close MPLAB. When it asks if you want to save the project workspace, click "Yes".
  12. Your project directory will now contain a bunch of files, one of which will be your .HEX file (e.g. LED_flasher.hex). This is the one you want to load into IC-Prog or your other programming software, such as the El Cheapo.


When you load MPLAB again, to re-load your project, go to File -> Recent Workspaces and choose your project. You can edit the .ASM file either inside or outside of MPLAB, just remember to hit the "Make" button when you re-load it to re-assemble it.

An Example

Image by 'Drexel'


Ironsides went through the drill of using the 16F84A PIC to make an LED flash ON and OFF.  The sample code was written to a PIC widely used in the hobby, a 16F84A.  Below is the simple circuit that will flash the LED once the 16F84A is programmed




The circuit diagram and sample code can be found at:  http://www.pages.drexel.edu/~kws23/PICTutorial.html

This is a sample of a portion the Assembler code as it would have been written in the MPLAB GUI.  The full version can be found at Annex A - see sidebar.

;*****Start of Sample*****

;*****Set up the Constants**** 

STATUS equ 03h ;Address of the STATUS register
TRISA equ 85h ;Address of the tristate register for port A
PORTA equ 05h ;Address of Port A
COUNT1 equ 08h ;First counter for our delay loops
COUNT2 equ 09h ;Second counter for our delay loops 

Note:  Michele Schieppati has pointed out that there is a much more elegant way to start out a program.

Quote: It is a good practice to use the standard definition in the picxx.inc original Microchip files, avoiding the re-declaration of PORT and other special registers.  If you do not do this, all the RAM location must be entered declaring the start-of-ram address and using the directive to reserve variable space.  The statement looks like this:

#include <p16F84A.inc>

This gives you some advantage in that code becomes completely relocatable by simply changing the right start of memory address, and it very easy to move from one micro controller to an other one without re-declaring variables or changing ram assignment.   End Quote

CatsEyes equally suggests that it would be helpful to include the lines as follows:

          list      p=16F84A            ; list directive to define processor
          #include <p16F84A.inc>        ; processor specific variable definitions
          errorlevel  -302              ; suppress message 302 from list file
          __CONFIG   _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC
The first line specifies the processor you are using, so you don't have to set it up in MPLAB (or a command line argument if you're using the command line to assemble your code).
The second line will set up all the processor-specific variable names for things like Register Files, specific bits within the register files and configuration bits. In the LED flasher sample code, for example, you can eliminate the EQUates for the STATUS, TRISA and PORTA registers, as they're already defined in the include file. In addition, bits within the registers can be named instead of using the bit number, e.g.
          bsf       STATUS, RP0            ;Switch to Bank 1
The bank status bit is named "RP0" (which is EQUated to "5" in the include file). This corresponds more closely to the code samples given in the processor datasheets.
The third line suppresses the annoying "Register in operand not in bank 0." warnings. (Of course the bank bits are correct!) Oddly, this line was omitted from the 16F84A code template.
Finally, the fourth line specifies the configuration word (so it is saved with the code, rather than you having to remember to specify it in MPLAB and/or in the programmer software). The configuration word specifies some fixed operating parameters of the chip such as oscillator modes, code protection etc. Refer to the section "SPECIAL FEATURES OF THE CPU" of the datasheet, under "Configuration Bits."

;*****End of sample*****

We save this Assembler code with a *.asm extension and then use the compiler embedded in MPLAB at:


The Compiler outputs this condensed Hexidecimal code.  Note:  The PIC only understands HEX code.


We save this code with a *.hex extension and then we program the hex code into the PIC chip using:

1.    The El Cheapo programming interface (for older PICs like the 16F84A);

2.    IC Prog (for newer PICs like the 12F629 or 12F675); or

3.    The Microchip PICkit1

In the IC-Prog software, there is an extremely helpful tool when debugging the programmer. (It's not very well documented; the help file for this options only says "This command checks the hardware that is connected to IC-Prog.")  See Annex B

'C' Compiler

If you write in "ANSI C", or have run into sample programs written in "ANSI C", then you can download a limited version of a "C" compiler from the HI-TECH®  site : http://www.htsoft.com/products/piclite/index.html

Note that it integrates into the MPLAB®  and that it supports both the PIC 12F629 and 675 chips.  The notes from the HI-TECH® are at Annex C in the sidebar. 

Hexidecimal and Decimal Calculator

Image by Ironsides from MS Windows



CatsEyes reminds that in Windows there is a scientific calculator to help you convert hexidecimal to decimal and vice versa.

As an example the decimal number '15' is shown as its hex 'F' equivalent.




Circuit Design Software and Printed Circuit Board Layout

Free software can be downloaded from ExpressPCB® or PCB123® as per the sidebar.

The Bottom Line

After wading through all this Ironsides became aware of a developer named Zagisrule! who was using ProBasic Pro (PBP)® with the MicroStudio® front end.  It all looked so much easier.  It turned out that PBP integrates with Microchip MPLAB® and PICkit1®.  Ironsides caved in and took the easy way out.  Within days, he had produced his first working PICs from scratch using PBP, MPLAB and PICkit1 with the 12F675.  It is not the cheapest solution, but it is the easiest method for those who wish to develop very simple applications such as the IronCams.

Updated Wednesday, 14 April 2004


Please see the sidebar

Home ] Annex A ] Annex B ] Annex C ] Developers ] Possibilities ] Code Tips ] PICkit-1 ] IC-Prog ] PCB123 ] ExpressPCB ] DigiTrace ] Oscilloscope ] References ]                                                                                                                                               

Send mail to ironsidz@hotmail.com with questions or comments about this web site.
Copyright © 2003 
Last modified: March 18, 2004