Friday, July 25, 2008

[electronics] PICs

Before I started programming computers, I was always trying to build some eletronic boards doing various stuff. Well, I am saying 'trying' because I never quite managed! Problem was, doing cool stuff quickly required a lot of equipement. Plus, contrary to the computer world, you have to go to a shop in person to buy the parts - and sometimes wait for weeks for them to ship in (no, there was no 'order-it-over-the-internet' at this time ... who said I am getting old?).

Anyway, I never dropped the idea of coming back to electronics one day. I mean, computers are great, but actually building a physical device really is something I find amazing.

Luckily, a new collegue of mine - David Grelaud - turns out to be an electronics guru :) He pointed me out a familly of microcontrollers - PIC's from Microchip - which are in fact fairly easy to use. These little things are programmed in assembler. Code is compiled on the PC and sent to the device through a programmer. You can order a relatively inexpensive programmer, which comes with a demo board, and get started! All tools to program are provided, as well as lessons to learn progressively the assembly language. The programmer is extremely simple to use: Just plug it to your computer USB!!

Ok, but how about the circuit board? Well, this is what is so cool about the PICs. They are extremely easy to use from a hardware point of view. I made a simple demo card with only a few wires, one resistor and a tricolor LED! I can program it from the PC, unplug the programmer and it becomes independent! Here is my - really ugly - card, plugged to the programmer:

You can also see how simple it is on this tutorial.
There are even C compilers for the PICs (C18, CCS, MikroC, PICC, SDCC). I am using SDCC which is open-source, but expect some not-so-easy install process. I had to hack a little bit because my PIC (18f4580) was not directly supported. But well, nothing too horrible if you are familiar with low level hacking (for those interested, use the 18f4585 template and setup the config bits directly in hexadecimal in the c code). The compiler produces a file containing the raw byte code (a .hex file) that is directly written into the PIC using the programmer. And voila, it just works !!

Amazing - now I am spending hours tweaking my rgb LED :-) (by quickly switching red/green/blue you can produce any color).

My head is just buzzing with cool projects to try on. I expect things will quickly complicate, but I know that the first step is always the most difficult.

Hey, did you know you can even create USB devices with a PIC ???!!?