bent machine production goes all-out geeky and

IT HURTS


the painful tale of acquiring the equipment and software to program PICs.

Welcome again to bent machine labs.

"bent machine labs: Desire is our one true engine." (TM.)



Today's mantra: I don't aspire to be an embedded applications designer - I just wanna build a better robot.

To be honest, I approached this project with some trepidation. Micros still confuse me, and I knew I would have to become at least conversant in assembly language. Also, getting started with PICs can be expensive. One site suggests a total outlay of $2000.00!!! For all the hardware and software, plus a programming language, I hope to get by at about one tenth of that. It could be done cheaper, but I usually try to spend the extra dough to get things that work well.

Worst case scenario, I was about to blow some cash on a humiliating failure just because I wanted to soup up my robot. I decided to go about it like I did with Stamps. Figure out what was really necessary, spread the cost over a couple months, read everything I could find on the web, and try not to freak out. It's still a little too early in the game to proclaim victory, but so far things are going well.

It is likely that anybody could learn to work with PICs, and for robotics the rewards are obvious. If you're curious I would say that the learning experience is worth it.

Some of my experiences and research made me figure I should put up a page detailing some of the pitfalls I avoided, as well as some I didn't. There are probably a lot of things missing here, but I hope it will be helpful.

PICs are simple little computers in a chip. PIC stands for Peripheral Interface Controller. PICs come in many different sizes and flavors, and they're pretty cheap. I'm learning with the 18 pin 16F84A, which has a whopping 1K of program space and 68 bytes of RAM. PICs are handy as an interface from the virtual world to the real world. They can talk to computers and then control servo motors, lights, LCD screens, relays, and can also talk to all kinds of different sensors.

Here's what you need to program Microchip PIC processors:

Programmer - a piece of hardware which programs chips.

Programming software - a piece of software that runs the programmer.

Development environment - software for writing your own programs for the chip. MPLAB - free from Microchip.

Knowledge of (or a willingness to learn) assembly language, C, Pascal, Forth, or a very expensive implimentation of faux PBasic.


The food chain rant:
Can I tell you about the disgusting food chain that has attached itself to PIC microprocessors? I researched how to go about doing this for at least a month.

There's a bunch of folks who offer programmers. Obviously, the programmers with more features are more expensive. Most of these folks offer good products at fair prices. I've got no truck with these people. By the way, there are plans available on the web so you can build your own on the cheap.

There are also a bunch of companies who want to rip you off. Like so:
"We've got the cheapest programmer in the world! Only $30.00 in kit form! If you want the software, that'll only be another $30.00! If you want to power it, that'll be $20 for the custom wall wart. Did we mention that it'll only program one kind of chip? For $50 more you can buy our adapter to add another two! The software is an excellent DOS command-line package specially designed for this programmer."

DOS? Did I hear DOS command line? And this isn't free? Why do I wanna buy a programmer in kit form? It's hard to believe, but if I understood it correctly, one of these sites actually advertises a PCB where they designed it wrong and you have to cut some traces and jump them to make it work. But hey - it's cheap! Can I tell you how good I am at electronics? I suck at electronics, and it's a miracle when any circuit I build works - without buying something that's already broken.

OK OK OK...

Ok. ok. I'm alright.

I decided to buy a programmer made by MicroEngineering Labs, who have figured out that the cool thing about the Basic Stamp II is the language and have come out with their own (phohibitively expensive) version of it. They also happen to offer a programmer and Jameco sells it for $60. It isn't top of the line, but it does the job - It'll program most flavors of 8 & 18 pin PICs. You've got to shell out more dough for a wall wart ($6) and a 25 pin printer extension cable ($4) which are also available from Jameco.

Make sure you get a "printer extension, all connections straight through" cable - nothing else will work.

The software is included and it works in Windows.

My advice? As with any tool, buy the best you can afford at the time and take good care of it. Another thing to consider, though: How long before this technology will be obsolete?


Next is learning to program the chips. I decided to try what everybody said was the absolute beginners book: "Easy PIC'n" by David Benson. It's a good book. There are two things you should know, however:

a) The book really doesn't cover much ground.

b) For a "beginners" book, the author assumes you know a lot about electronics (my weak spot) and micros in general. It's a good thing I already had some experience with Stamps. Logic switch? What's a logic switch? Where's the diagram? Oh yeah, we'll just set that 555 up in one-shot asynch. OK.

I looked at the other books available, but decided that I should buy the one for knuckleheads. "Easy PIC'n" did the job - only I wish it had more info, you know?

Further recommended reading, mostly available in PDF files - FREE - from the Microchip website:

Microchip MPLAB User's Guide - 282 pages

Various Application Notes from the Microchip website - 115 pages so far

Microchip Data sheet for the PIC16F8X - 124 pages

Microchip Mid-Range MCU Family Reference Manual - 700+ pages

Blonde On The Rocks by Carter Brown - $2.00 - 130 pages

I also just recently discovered the PIClist on the web. A great resource.

About a quarter of the way through "Easy PIC'n" David Benson recommends building a generic carrier board for testing. He also gives you a basic layout with some things I thought were a joke and only grudgingly added, but later found were necessary. When I started using Stamps I got a Board of Education and really liked it, so I spent some time making myself a testing board that's hooked up to a solderless breadboard for prototyping. This ended up being real cheap. Mostly I used stuff I had sitting around, but I had to buy 2 banks of dip switches.

This was worth it for sure, and as a bonus the board has enough wire in it that none of my neighbors can get radio reception when I fire the thing up.

So, you've got your software, your programmer, your cables, a chip in the testing board, a test program in assembly, and a test circuit built on your breadboard, so all you have to do is download the program into the chip and...

NOT.

I got errors from the programming software, and the test circuit didn't do anything. So I reread all the manuals, went over my testing board with a multimeter and logic probe, triple-checked the programming configuration, tried different chips. Nothing. For two days, nothing.

I don't know SQUAT about PCs, so I was unaware that the parallel port on my previously owned got-it-free laptop PC might not be set to be bi-directional. They don't mention this in the literature that comes with the programmer (which, by the way, lacks a trouble-shooting section.)

Fortunately, my buddy Tom knows about this kind of stuff and he speculated that the port might not be set up right.

You've got to find the proper control panel or setup utility or port software dialog box and make sure your parallel port is bi-directional. Otherwise it won't work! Like mine didn't! For two days! But it'll work on your computer at your job, which will deepen the mystery. For two days!

The thing that scares me is the question of how many people have given up at this point, or worse yet, bought a new computer because of it.

It works! Time to learn assembly language.

Programming is probably my strong point when it comes to robotics, but even still, I have to say that learning assembly is not as much of a pain as most people make it out to be. The coolest thing about assembly language is that you're dealing with instructions which execute at 1 or a half MHz, which means that hundreds of instructions will fly by in way way less than the blink of an eye. For someone who isn't used to it, the stuff is blindingly fast.

Maybe this is all going to be worth it.

In just a few nights I already managed to write a program so that a Stamp can send bytes to a PIC in asynchronous serial (core code from a Microship Application Note) with a double-handshake flow control (of my own design.) This way they can both be doing lots of other things between talking to each other.

I might find out later that I'm wrong about his, but for my servo controllers I wanted handshaking with extremely fast timeouts. The Stamp signals when it wants to talk and the PIC will stop to listen when it's between other time-sensitive, uninterruptable tasks. It requires 3 wires - one for serial communication and two for the handshaking (I'm not proud) - but it's solid and works at 9600 baud.

If you're curious, the (laughably primitive) code is here: Assembly code for 16F84A. PBasic code for Stamp II. A test circuit diagram is here, too.

As always, I hope somebody somewhere finds this info useful.

More fun with microprocessors to come!





home


Everything copyright, 2000 Dave Benz

hits counter