Meetings Program
 and Reports
Contact Us
Headlines List

Page updated Monday
16th 2007f April 2007
23:07:24 UTC
Now 15:20:36 UTC

  Beyond PICAXE - Programming PICs in C

Date:April 13, 2007
Time:7:00 pm.
Place:WEA, 223 Angas Street, ADELAIDE
Presenter:Peter Reid

Beyond PICAXE - Programming PICs in C

The PICAXE system is a fine low cost way to introduce youngsters to microcontroller programming but the claim that this is the 555 chip of the new century is just not credible. As soon as you get much beyond flashing a few LEDs and simple sequential programming you come up against the limitations of the PICAXE. This session will be a demo of programming PICs with a free C compiler and Microchip's PICkit2 USB programming hardware. There are plenty of DIY parallel and serial port programmers and software on the web for a zero cost approach.

PICAXE limitations
- interrupts are polled and multiple inputs are ANDed whereas you really want them ORed,
- PICs have a range of peripherals (analog comparator, timers, capture compare module, watch dog timer etc.) that are difficult or impossible to access in PICAXEs,
- PICs have a range of oscillator options but PICAXEs are much more limited,
- you lose an I/O pin to the programming interface (a PIC with in circuit serial programming need lose no I/O pins and no switching is required if designed carefully),
- one of my PICAXE projects crashes every few weeks despite being battery powered,
- PICAXE Basic is slow and it is difficult to write a decent structured program.

There are workarounds for many of these limitations. You can use peek & poke to access some of the peripherals but you need to know how the PICAXE uses them and this info is not readily available. You can use external circuitry e.g. as a watch dog timer or to process interrupts, but why should you have to? You cannot get around PICAXE Basic and its lack of speed.

If you want to go beyond PICAXEs and program a PIC, what are the alternatives? I have programmed PIC16C84s in assembler but did not find this very pleasant so I looked at free C compilers:
- CC5X is a command line compiler.
- PICClite (the free version of HiTech C) supports only a limited range of PICs and has to be used with MPLAB.
- Mark speaks well of Microchip's C18 demo.

I settled on mikroC, which is a free 9 MB download from http://www.mikroelektronika.co.yu/
The only limitation of the free version is that it cannot generate hex output over 2k of program words. It supports a very wide range of PIC12s and PIC16s. Full support for PIC18s is in progress. There is an IDE interface with all the usual project, editing, source level debugging and helpful help and a large library of useful functions. MicroElectronika also have free Pascal and Basic compilers for PIC with the same 2k limitation. All of these output a hex file for loading into your hardware programming software.

MikroC has a few bugs; sometimes it objects to a line of code for no apparent reason and cutting and pasting back fix this. Optimisation is minimal if any.

Here is a comparison of the measured maxim speed at which PICAXEs and PICs can toggle an I/O pin within a continuous loop:
4MHz PICAXE08M - Basic 1.25 kHz
4MHz PIC12F675 - mikroC 200 kHz
4MHz PIC12F675 - assembler 250 kHz
The PICAXE's slowness explains why they cannot cope with a garden variety IR remote; you have to use another PICAXE or a particular remote from the UK. MikroC is slower than assembler because it sets the register bank within the loop. This points to poor or no optimisation but then you can always hand optimise any time critical code in inline assembler. MikroC has assembler output to help with this. This is how you can bring mikroC's speed up to that of assembler by setting the register bank outside the loop:
while( 1)
{ asm{ BSF GPIO, 5

There are many DIY serial & parallel port programmers and software on the web. To save time, I chose Microchip's own PICkit 2 USB programmer. This is an in circuit serial programmer. Supplies of the PICkit 2 seem to be very limited in Australia. Microchip will sell it to you direct for $US50 plus freight.

Today Microchip's web site http://www.microchip.com/ has the PICkit 2 Debug Express for the same price. As well as the programming hardware you get a 44 pin PIC16F917 demo board.

These PIC chips are readily available to replace PICAXEs:
PIC12F675 - this is similar to the PIC12F683 (which the PICAXE 08M is based on) but has half the flash program memory, SRAM and EEPROM, no hardware PWM and 2 timers rather than 3.
PIC16F88 - this is the chip that the PICAXE 18X is based on.