Wednesday, June 8, 2016

Arduino dsPIC30F programmer

This post talks about an ArduinoUNO based PIC programmer useful for dsPIC or similar devices, in particular the dsPIC30F which can be programmed with 5v. PIC devices like dsPIC33/PIC24 have a working voltage of 3.3v and requires a hardware level translator for connecting to Arduino. This will be covered towards the end of this article.

Why an Arduino based programmer ?


For a recent project I had to work with dsPIC30F2020 device. This is the first PIC that I ever worked with and is one off project. So I didn't want to buy a PICkit programmer. But if you are looking to work with various PIC devices, then I think PICkit is a good investment.

The dsPIC30F series is simpler to setup especially with the programming voltage same as Vdd. So no charge pump or similar solutions are required for Vpp. The flash programming specification document is an excellent reference and looks well suited for a bitbang implementation. I was already familiar with Arduino programming environment, so I decided to use it as the device programming interface to PC.

Disclaimer: Please note that if you use this code or hardware, then you are completely on your own. Myself and the original authors are not responsible for any direct or indirect consequences.


Software

The application software is modified to work with a serial device, just like it works with the usb programmer. The serial device can be any device which understands the usbpicprog programming protocol. In my case the serial device is provided by Arduino, so the Arduino code has to do the heavy lifting of understanding the protocol and converting the commands into signals.


Application

I didn't want to reinvent the wheel of developing software resulting in yet another PIC programmer. So I though of finding an opensource pic programmer to add arduino support to it. That left me usbpicprog as the only viable option as it was opensource, has a command line mode (later I found this needs X to work) and a decent GUI. The downside was that it is not actively maintained.

I just hacked the existing code to fit the serial support,  so please excuse the code organisation. The application source is maintained in the git repo here.


Arduino software

The original pic programmer uses a secondary PIC to program the device. I use ArduinoUNO instead of the secondary PIC. The Arduino program understands the programmer protocol and converts it into PIC ICSP instructions and drives the PGC and PGD accordingly. The  source for Arduino can be found here.

The binaries and hexfiles can be downloaded from here.


Hardware

dsPIC 5V Vpp devices


dsPIC30F devices has programming and operating voltage of 5v and programming current (Iddp) of 30mA. Apart from power, we need clock (PGC), data (PGD) and reset (MCLR) pins. The pins and power rails can be directly connected to Arduino as the DC characteristics of these pins also are well within the ArudinoUno's specification

The connection was as simple as connecting all the power and ground rails (Vdd, AVdd, Vss, AVss) to Arduino headers. Then identify 3 digital I/O ports (non PMW pins is sufficient) and connect them to MCLR, PGC and PGD of the chip. No crystal is required as we will be using the ICSP mode in which the system clock is provided externally through PGC. No decoupling caps were used as the device is powered directly from Arduino. Although a 100nf is recommended near the PIC device across the VCC and ground. 

My particular setup (and the software defaults) is shown below. I used a breadboard to test the programmer.
Programmer and LED test wiring
dsPIC30F2020 running LED blink program
Current (mA) measurement of 5V supply rail with blinking LED program

As you may have noticed, I have added a resistor and an LED for a LED blinking "helloworld" test program, but this is not essential if you just want to program the device. The ledblink program is included along with the Arduino source.


Modification for 3.3v devices [untested]

The  3V3 devices, dsPIC33E/F/PIC24 series has an operating voltage of 3.3v. Apart from Vdd, the MCLR, PGC and PGD pin also operates to a safe maximum of 3.3v. Fortunately the ArduinoUno (and may be others too) support a regulated 3.3v rail which can source upto 50mA. But again the digital I/O pins work on 5v which is a pity. So we need some circuitry to translate the 5v TTL logic levels to 3v3  level and back (for PGD pin).

There is an excellent article from microchip which explores the various options available for the conversion. Among the various options, I like the resistor divider which is simpler and cheaper to source. Whatever options you might choose, remember to get,
- Two 5V to 3V3 output pins for MCLR and PGC - a resistor divider network should be good here.
- A bidirectional for PGD - a simple solution is described here
- 3V3 power rail which can source enough current for programming (typically 30mA but refer datasheet of the chip to be sure). The ArduinoUno 3v3 rail should be good enough.

Please drop a message in the comments, in case you find this useful.  Any productive comments or suggestions are  welcome. Thanks for reading and happy hacking.