7 April 2008

3. Take your pic

These series of tutorials cover the Microchip pic family of microcontrollers. There are many other types of microcontrollers. If any of those take your fancy, you’ll find that beyond the specifics, a lot of what you learn here will apply for those ones as well. I happened to stumble into pics first and once you’ve made the investment into programmers, and prototyping gear and compilers and code, it’s hard to switch. Maybe I’ll try another variety someday – a lot of people talk positively about the AVR family for example. Whatever. This is about Microchip pics. Go fight your religious wars elsewhere.

You can get pic chips with as few as six pins and as many as, well, lots. Hundreds even. The smaller varieties have less memory and less functionality, and they’re cheaper obviously as well. I think the 16 and 18 series are the most fun – they’re a good platform to build on and once you have something working, you can port it to a smaller pic if you want something cheaper.

So what’s in these little guys? Each pic has some flash memory, it may have some eeprom memory as well. It has a number of hardware blocks that might be as simple as some inputs and outputs or as complex as a full blown USB implementation.

These two different types of memory are important. There’s normally lots of flash compared with much less eeprom memory. Flash is cheap, and hence big, eeprom is expensive. Flash can be overwritten 10,000 times. Eeprom can be overwritten a million times. So you can see that flash is good for programs, eeprom is good for settings, configuration, storing things that change.

In terms of pins on the chip itself, two are reserved for power. Many pics run at 5 volts (eg 16f88) at high clock speeds, or a more expensive variety (eg 16lf88) that runs as low as 2 volts. At those lower voltages however, often you can’t clock them as fast.

Speaking of clocks, with hardware it’s often all about timing. Knowing precisely how fast the pic is running turns out to be quite important (although I spend a fair amount of time trying to avoid timing issues). As such, the most accurate way to clock a pic is via a crystal, plus two small capacitors which help stabilise the oscillator circuit. This does take up two pins, however, and given that pins on these babies are often limited, microchip give you the option of running an internal clock instead. The internal clock is an RC clock – meaning it’s based on the charging time of a capacitor. Since capacitors are harder to make accurately than crystals, the timing is not as precise. Still, you get two pins back. A trade off.

Unfortunately for us, pics take four oscillator cycles to process one instruction. That means that when you’re running with an internal clock (often 8Mhz), you’re running at 2 million instructions per second (mips). That’s one instruction every 1 / 2,000,000 seconds, or every 0.5 microseconds, or 500 nanoseconds. Sounds fast until you actually try to do something even vaguely complicated Then you’ll be scraping for every mip you can get your hands on. And that’s because it’s a RISC device – a reduced instruction set. These little guys only have one register and have no concept of numbers bigger than 255. It turns out that you can get quite a lot out of these simple instructions though, including 32 bit numbers and floating point math, but my point is there’s no fdiv instruction here – you need to do quite a few instructions to accomplish anything much. Oh, and that’s not quite the full story either – some instructions take two instructions to execute – that is, 8 clock cycles. Still, for the purpose of this experiment, we’ll just blindly assume that 4 clock cycles is enough of a sacrifice to make.

No comments: