Lots of people disparage PIC microcontrollers however—as I now know to my price—these little rascals have an addictive high quality about them. They will make life extremely simple with respect to prototyping initiatives and producing merchandise, assuming you recognize what you might be doing, and therein lies the story…
I minimize my programming tooth writing meeting language on 8-bit microprocessors circa the late Nineteen Seventies. We’re speaking about units just like the Z80 from Zilog and the 6502 from MOS Expertise. Now, I’m not saying that these processors didn’t have their quirks, however I don’t recall something that set my mind gyrating on its gimbals.
Over the previous decade or so, I’ve spent quite a lot of time taking part in with 8-bit microcontrollers just like the Arduino Uno R3, which relies on the well-known ATmega328P processor. This processor was initially created by Atmel, which was acquired by Microchip in 2016. In contrast to microprocessors, which require exterior units like reminiscences to operate, microcontrollers embrace reminiscence (SRAM, Flash, and EEPROM) on the identical chip because the processor, together with different capabilities like analog-to-digital converters (ADCs), pulse-width modulators (PWMs), and counter/timers. Fortunately, we are able to use the Arduino’s Built-in Improvement Atmosphere (IDE) to seize our applications in C/C++, so no meeting language there.
This subsequent half is a bit scary, so chances are you’ll need to cowl your eyes. A few years in the past as I pen these phrases, I discovered myself in a little bit of a pickle. I’d foolishly agreed to take over the upkeep of an organization’s code focused at 8-bit PIC microcontrollers from Microchip. I used to be conscious that this code was in meeting language. Nevertheless, having labored with a number of such languages prior to now—even creating a few my very own throughout my profession—I worry I’d grown a bit of blasé and complacent. In consequence, I overestimated my talents whereas underestimating my tolerance for ache; that’s, ache because it pertains to programming in PIC meeting language.
Once I got here to really have a look at the code in query, my eyes began to water and shivers ran up and down my backbone. I’d been informed that the supply code was closely commented. It was. Sadly, the feedback hadn’t been up to date to replicate any adjustments made within the code through the years, leaving the 2 entities virtually completely unrelated.
Even worse, once I regarded on the code itself, I understood it in addition to if it had been written in Egyptian Hieroglyphics. I downloaded and printed a guide for one of many PICs. It was 300 pages!
One of many issues is that the processors with which I’m acquainted are based mostly on a Von Neumann structure during which the reminiscence accommodates each knowledge and directions. By comparability, the 8-bit PICs with which I used to be dealing are based mostly on a Harvard structure, which includes separate reminiscence and sign pathways for knowledge and directions. Additionally, though this isn’t significantly related to what we’re speaking about right here, PICs are Decreased Instruction Set Laptop (RISC) machines, versus the Advanced Instruction Set Laptop (CISC) machines with which I’m extra acquainted.
One other problem is that the reminiscence in PICs isn’t offered as a single contiguous complete. As a substitute, it’s break up into small “banks” which might be 128 bytes in dimension. Because of this completely different variables find yourself being saved in numerous banks and—in case you are writing in meeting language—you might be obliged to maintain observe of issues and change forwards and backwards between banks as required to entry your variables. It will get worse. When you have been fascinated with creating an array of some knowledge kind the place that array crosses from one reminiscence financial institution to a different, then my recommendation is to consider doing one thing else, like coaching squirrels, which might be a lot much less sporting on the nerves.
And yet another consideration is that PICs include an inordinate variety of Management and Standing Registers (CSRs)—a lot of which can actually be realized in reminiscence—whose bits you usually should tweak to appreciate no matter impact you might be hoping to attain. On the one hand, this provides you a rare quantity of fine-grained management over the system. Alternatively, you might be typically obliged to carry out quite a lot of tweaking to attain the only consequence.
The entire above is the dangerous information. However flip that frown the wrong way up right into a smile as a result of I even have nice tidings to impart. As I famous earlier, when you’ve mastered the artwork of programming these little scamps, PICs could make life extremely simple with respect to prototyping initiatives and producing merchandise.
One of many nice issues about PICs is that they are often programmed utilizing cheap-and-cheerful {hardware}, such because the PICkit 2 proven under (be aware that it is a clone as a result of it doesn’t carry the Microchip emblem).
PICkit 2 PIC programmer. CLIVE “MAX” MAXFIELD
This little magnificence is simply 3½ in. lengthy by 1½ in. large by ½ in. thick. On the far finish (within the picture above) is a mini-USB port, which you utilize to attach the programmer to your host pc. On the close to finish is a 6-pin 0.1-in. pitch header socket. I exploit solely 5 of those pins (+ve, GND, Reset, Clock, and Information) and, with out wanting it up, I can’t recall what the sixth pin is used for.
One other beauty of PICs is that there are such a lot of of them. I wouldn’t be stunned if there are 1,000+ of the little scamps, every with completely different combos of reminiscence sizes and kinds/portions of peripheral capabilities. On the one hand, it is a blessing as a result of it’s often potential to trace down a PIC that’s ideally suited to the duty at hand. Alternatively, for those who aren’t cautious, you may spend quite a lot of time pondering over the right half.
PICs are available 8-bit, 16-bit, and 32-bit flavors. For the needs of this sequence of columns, we’ll deal with 8-bit units as a result of these are those I’m predominantly taking part in with in the mean time. After all, these numbers consult with the width of the information bus, which isn’t associated to the variety of pins on the system. Talking of which, you’ll find PICs with anyplace from 6 to 100 pins.
The good factor for hobbyists and prototyping is that PICs with 6 to 40 pins are often obtainable in each twin in-line (DIP or DIL) and surface-mount expertise (SMT) packages. This implies you may prototype a mission by sticking a DIL packaged half instantly right into a breadboard as seen under, after which transfer to a smaller SMT system while you go into manufacturing.
Programming a PIC plugged instantly right into a breadboard. JOE FARR
The PICkit can be utilized to energy the PIC throughout programming. It may even energy a small mission or analysis board. The worth of +ve from the PICkit is configurable to assist regardless of the PIC getting used requires. The extra crimson and black wires connecting the PIC to the ability and floor rails within the above picture are there for when the programmer is eliminated, further elements are added, and we have to energy the PIC from the breadboard.
As we famous earlier, along with +ve and GND, we’d like solely three pins to program the PIC—Reset, Clock, and Information—all of which can be utilized as common inputs or outputs when the PIC is in operation.
Within the above instance, my chum Joe Farr used a strip of 0.1-in. pitch header pins to attach a PICKit programmer instantly into the breadboard after which run wires to the suitable pins on the PIC. Now, that’s nice for a PIC in isolation, however what concerning the case the place you’ve been increase the prototype circuit across the PIC on the breadboard? You don’t need the PIC to aim to energy all the opposite units forming the prototype circuit.
To handle this problem, Joe has constructed a set of little service boards to deal with completely different dimension PICs. As we see within the picture under, the PIC plugs right into a socket on the service board, and the service board plugs into the breadboard so the PIC can speak to the remainder of the circuit.
Programming a PIC plugged right into a service board. JOE FARR
The service board additionally has a 6-pin header (solely 5 pins are used right here) that connects to the programmer. What you may’t see right here is that there are a few surface-mount elements (diodes, resistors, capacitors) mounted on the underside of the service board. These detect when the programmer is powering the PIC and mechanically isolate it from the remainder of the circuit on the breadboard. Fairly intelligent, huh?
Wow. I can’t imagine I’ve waffled on for therefore lengthy. In my subsequent column, we’ll focus on organising an surroundings during which to seize code at a better degree of abstraction than meeting language after which compile this code and program our PIC. Till then, as all the time, I welcome your feedback, questions, and strategies.