Let's Make Robots!

Microchip PICs - Getting Started

Learn from first principles how to select and program virgin PICs in RISC assembly.

What's one of those?

A Microchip PIC is an example of a microcontroller. Basically, a tiny but very complete computer in a single chip. The PICs have their own program memory, CPU and RAM. Most of them now have their own on-board clock, serial interfaces and ADCs as well. All in one little package. Faster processors (although less RAM) than my first home computer (a Sinclair ZX Spectrum).

Oh, I must address one unfortunate misconception: the PIC does not REQUIRE a bootloader. Many folk use a "PicAxe" which is a PIC programmed by the PicAxe people with a bootloader which will accept specially compiled programs using a BASIC compiler. We will not require a bootloader, but I can show you later why one might be useful. (I'm no PicAxe expert - please correct me if I'm the one under the misconception.)

Which one should I get?

The 16F84 is the first PIC I used. It's extremely well documented, with tons of example code all over the web and the ICSP (in circuit serial programming) works the same as on any other PIC. HOWEVER, it does require an external crystal oscillator and a pair of capacitors as the timing circuit and an external resistor to keep it out of programming mode. Also, it has no analog inputs or any of the fancy gubbins.

So.... I would recommend the 16F690. You can run it with NO (count them: zero) external components. It has a built in 8MHz clock (convenient because it means 1 command = 500ns execution time (except for jumps and conditions which can take two cycles). It has I2C (slave only), a built in asynchronous (ie RS-232 style) serial port and up to 12 analog inputs. If you want it to be an I2C master, there are loads of freely downloadable examples of "bit-banging" in code.

Engineering Smaples 

Never, never, never buy PICs. Don't do it untill you're into mass production. Microchip are happy to give them to you in the hope that one day you'll be buying them by the truck-load. They will send you a dozen at a time and they'll even pay the postage. (Microchip Direct link below.)

Microchip will not send you samples if you use a generic email address such as google or hotmail. I have also heard that comcast addresses don't work. Your ISP account may work. Failing that order through your work email. If that doesn't work, lease a custom domain name and set up aliasing. If you're really stuck, someone here might order them for you. 

Which TYPE should I get?

I've been asked which PIC16F690 to sample. There are [four] to choose from on the Microchip website:

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1335&dDocName=en023112&treeid=1

-I/ML, -I/P, I/SO, I/SS.

These suffixes refer to packaging information. I suggest the /P. This is the 0.3in dil package electronics hobbiests are probably most familiar with. I believe /SO (SOIC) and /SS (SSOP) are surface mount versions and /ML is a QFN type (wee square one and the sockets are sodding expensive). If you look at back of the data sheet, you can see the different shapes of the chips.

What programming hardware do I need?

As for the programmer, can you use a soldering iron? If so, I HIGHLY recommend the JDM programmer. (Link below.) I built a cut-down ICSP version for less than UK£4. (About US$8.) I have been using it for about 10 years and it has NEVER failed. I made the mistake of building in a ZIF (Zero Insertion-Force) socket. This works well, but the socket cost twice what the rest of the hardware cost and once I discovered ICSP, I never used it.

If you're not too handy with a soldering iron, I have to question wether robots are the true path to your happiness. There are simpler programmers. The ones which run off the serial port of a PC are usually "LUDIPIPO" programmers. (If anyone can tell me if this is an acronym, or its historical significance, please do.) Essentially, that means that rather that using the Tx line for data as you might expect, it's used to generate the programming voltage while the RTS is the clock line and DTR/CTS are used in combination for data in/out. Don't worry about all that, though: the programmer will handle it.

Beware

Some USB/RS-232 convertors do not drive LUDIPIPO porgrammers properly. I recommend you use an actual serial port if your computer is old enough to still have one. The PC has direct access to the UART which is what is required for this application.

Microchip makes the distinction that programmers not manufactured by Microchip should be considered "development programmers". Proprietary Microchip programmers are the reserve of commercial users, educational establishments and rich DIY robot fans. There's nothing wrong with home-made or OEM programmers, it's just that Microchip make no guarantees if a commercial application fails due to not using the official kit.

What software drives the programmer?

Sadly, this programmer does not work in conjunction with the Microchip IDE. (Only the Microchip programmers and a few others can program PICs directly from the IDE.) So, the software I recommend to program the PIC is the freeware WinPic. (Link, as ever, below.)

I strongly recommend this piece of software as it uses the data in the Microchip MPLAB IDE installation to set up the PIC correctly. Many other software packages have this data coded into them which means they require a modification in order to work with new PICs. WinPic should, in theory, program any PIC without modification.

What's this ICSP?

Yes, I mentioned it several times. It is simply that the PIC can be programmed while it's in a circuit. We attach a clock line, a data line and a programming voltage to the PIC and it can be programmed in situ. The programmer sends the compiled program to the PIC. It's no more complicated than that.

Serial Port Issues (ie "I don't have a serial port")

Some folk have tried LUDIPIPO (eg JDM) programmers using USB/serial interfaces. Some of them have worked. Some of then have not worked.

The problem with the serial port programmers is that the Tx line is used to switch the device into programming mode and the handshaking lines are toggled to clock the data into and out of the PIC.

I have two guesses as to why the USB converter things don't always work:

1) Since the bits need to be clocked (synchronous) into the pic, I imagine that the ons and offs need to be under direct CPU control for the synchronicity to happen properly.

2) It's possible that teh convertors just plain don't have enough "oomph" (that's a technical term which generically translates to "power").

What a pic-based USB programmer will do differently is that there's no RS-232 involved at any stage. They contain a pic which has an inbuilt native USB interface. The USB comms are sent to this pic and the pic is responsible for clocking the signal out to the pic which is being programmed.

All in all, I think the best bet might be http://www.instructables.com/id/S8PH18HFAQCISGU/. I'm tempted to have a stab at building it myself.

I'm also looking into the PicSquirt. http://www.p10link.net/plugwash/picsquirt/

It should work and you should be able to build it for less than $20.

UNFORTUNATELY, the firmware for the PicSquirt MCU only claims to support a few of the 18F series chips, and I haven't established what mods (probably software only) would be required to make it work. Nor can I figure out what windows software drives it.

I'm afraid the USB/serial converter I have is NOT one of the ones which works. I haven't tried others because I have serial ports!

What language will we be using?

In later installments, I'll look at Microchip's own native RISC assember. I'll try to knock up examples in increasing levels of complexity. I'll start with how to flash an LED, moving on to taking input from a switch, then look at creating a PWM motor control. If anyone is really dribbling at this point, I'm going to talk about "bit-twisted PWM" (oooooooh!) later, too. I might even get around to servo motor control and a little on analog inputs. I'll look at serial port I/O and introduce a line level driver IC which does all the hard work for us. Of course it wouldn't be complete without an I2C tutorial (but I have to teach myself that first!)

What other info do I need?

Whatever PIC you get, download also its data sheet. Read this next beit very carefuly. What follow is my VERY BEST tip: read the data sheet FROM COVER TO COVER. You won't understand it, but don't worry. You will have absorbed answers to questions which you haven't even asked yet. It will fall into place as soon as you try to use it.

Don't ask me questions unless you're at least skim-read it from cover to cover. I will know by your question if you've read it. You'll be embarassed if you ask me a question and my answer is: "see page 47".

One section of the data sheet you should print and keep is the instruction set. There are only 32 instructions for the PICs we'll be using. (Wait a minute. Isn't that the same number of instructions as there are in the C language once you strip away al the libraries? Think about it.) If your data sheet contains only a summary of commands, there's a Microchip document number DS31029 which contains the full explaination of all the commands.

Your first assignment is write a program and build a circuit to flash an LED at 1Hz. Shall I leave you to get on with it? Of course, I'll provide the answer eventually...

...in the mean time, ask me questions.


Useful links:

Microchip website (free Integrated Developpment Environment)

Microchip website (free PIC "samples")

WinPic (PIC programmer software)

JDM (PIC programmer hardware)

Microchip website (8-bit PIC selection table)

 

Update 2008-07-07

The guys at Gooligum Electronics have done a PERFECT walkthrough.

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

The joke was on you, I was responding the main thread :D

(Next time I will add a PS :D )

yeah right

Wasn't there a song?

"Money for nothin, and your Chips for free"

You'll love it. Only 31 instructions to learn!! You already know them all if you've done 68K. Bonus!

#1 I know nothing about picaxe. Sorry. The comment about a bootloader is speculation. The answer is "probably not." If the picaxe is, indeed, programmed by a bootloader, the protocol is likely to be different. jip's comment here appears to evidence this.

#2 Yes is the short answer. LUDIPIPO programmers (like JDM) will program all ICSP pics if they are attached to a PC serial port. Some USB/serial adapters DO work properly. My only experience is that mine DOESN'T and from what I've gleaned here, only a very few do. That said, some OLD PC (argualbly non-compliant) serial ports need and additional diode added to the JDM. That's all documented on Jens' site, though. (Hmmm... I wonder if the addition of this component would cause it to work with the USB adapter. Oh, well. If it ain't broke, don't fix it.)

#3 I don't see why not. If it has ICSP, requiring a high programming voltage on !MCLR, it should work. It won't do low voltage programming, but I've never seen a PIC which has only LVP. Jens lists PICs that he has tested it with. I've not found a pic I coldn't program with it. My personal 8-bit stock consists of: 16F628A, 16F677, 16F688, 16F690, 16F72, 16F73, 16F74, 16F77, 16F818, 16F84A, 16F872, 16F88, 16F882 and 16F883. I'm pretty sure I'll never need to introduce any others and there are some I'll never use, but they all work.

Update 2008-07-05

I can confirm that WinPIC / JDM works for most, if not all 18F devices. You may need a programming adaptor for DSPIC30 stuff.