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.

Thanks for that really interesting post!

And I love Dire Straits :P I will do a robot who plays guitar with Knopfler Style using just fingerpicking :P 

Will the JDM programmer work for 16bit pics like the PIC24 and dsPIC33 series?

So far the only chip I wasn't able to program was the 16F818. Even then, I found out why. The programming voltage input has to be set in order for the device to go into programming mode. With the 818 they widened the programming voltage so instead of from 10..13V, it's 3..13V. The JDM outputs a slightly positive voltage all the time and it was just enough to supply a programming voltage before powerup. I fixed it with a pulldown resistor.

I guess it depends what you want from your hobby. If you just want something you can plug into a servo, program it and go and you have a budget then by all means by a commercial programmer and a commercial MCU board. As far as I'm concerned the lower level you get the better - otherwise you may as well just go out and buy a commercially avaialable robot and save yourself a lot of effort! Where's the challenge?

I have some PIC24 and dsPIC33 chips that I got as samples. Eveery time I read about homegrown programmers, it's the 8-it PICS that are mentioned. I was just wondering if it was worth the effort to try and use the JDM programmer for these chips or if I should go and buy a commercial programmer. I'd prefer to build it myself, because that's the best way to learn how things work. I'll give it a try, when I get some spare time next week and post my results here.

Basically, if it's got ICSP, the JDM will program it. I've never met a PIC which JDM and WinPIC couldn't program.

It would be best to get a real programmer that is known to support those chips, JDM are always risky. Look at my other post.

I would NOT RECCOMEND A JDM PROGRAMMER! People use JDM's to program their ICD2 :p

 

The PICKIT2 Has become popular.

That's unfortunate. Is it prejudicial or from experience?

The Pickit 2 is certainly popular, but it isn't free. Building your own programmer gives you an understanding of ICSP which you may not get from buying one. Once you build a programmer you'll wonder why you ever bought one.

Also, I don't understand the relevance of the fact that they're used to program ICD2s.

Keen you hear your experiences... 

I always have connection issues when using a JDM. my PICKIT2 (I have 2 of them) and my ICD2 are great and I have only had one or two problems with them. I buy mine in a kit form from www.blueroomelectronics.com and they are fully compatable with MPLAB software. ICSP can be fun but it is hard when playing with a JDM. As I said, I tried to program my ICD2 with a JDM but it would not connect, so I bought blueroomelectronics's Inchworm+. IF you are going for a kit of these type I would go for his Junebug, as it is a fully compatable PICKIT2 with a tutor on it.

then i came to this very cool site and i've caught the robotics bug again. So ive ordered up some samples of the pic16f690 dip and have been perusing the chip manual. I also have a Stamp kit from my last flirtation with robots. i think i will dust that off as well. My last bit of electronics fun was modding an Xbox, but it never could navigate itself around the room. :P