In Half 1 of this soon-to-be-legendary mini-mega-series, I made point out of the truth that—a few years in the past as I pen these phrases—I foolishly agreed to take over the upkeep of an organization’s library of code focused at 8-bit PIC microcontrollers from Microchip.
Unhappy to narrate, this code was in PIC meeting language, which—if the reality be advised—is a little bit of a beast, not least as a result of the inner structure of PICs is … let’s say “attention-grabbing” and go away it at that.
I’m sorry, I can’t go away it at that due to what’s to return. As one small instance, the reminiscence within the 8-bit PICs which are the main target of those columns isn’t offered as a single contiguous entire. Fairly, it’s cut up up into 128-byte banks, which suggests you typically want to make use of particular meeting directions to modify banks to entry no matter it’s you want to entry. Additionally, PICs comprise an inordinate variety of Management and Standing Registers (CSRs) whose bits you sometimes should tweak to understand no matter impact you might be hoping to realize.
To be sincere, as soon as I absolutely comprehended the complexity of the issue I began to despair. Significantly, I used to be waking up in the midst of the night time pondering, “how on Earth am I going to tug this one off?” Fortunately, I remembered that my buddy Joe Farr, who hails from an attractive village exterior London, England, is a grasp of the mystic arts in the case of PICs. I can’t inform you what number of occasions we’ve bounced concepts backwards and forwards about some venture on a Friday, solely to search out that Joe has a totally purposeful PIC-based prototype up-and-running by Monday morning (and this consists of fabricating his personal printed circuit boards).
Thus it was that I referred to as Joe and inquired tips on how to deal with my meeting language downside. Joe responded that the answer was clear … that I ought to neglect the thought of programming in PIC meeting language. Clearly, this was solely step one within the answer, in any other case I’d be up the coding creek with no metaphorical paddle. Joe knowledgeable me that—in his humble opinion—my best choice was to recode all the things from the bottom up in a higher-level language that might be simpler to know and to take care of.
My knee-jerk response was that Joe was going to recommend I program my PICs utilizing C or C++, so you may solely think about my shock when he really helpful utilizing BASIC (if you’re within the historical past of computer systems, you actually ought to watch The Delivery of BASIC video).
I have to admit to being a tad shocked (“BASIC is so twentieth Century, my pricey!”), however Joe proceeded to clarify {that a} man referred to as Les Johnson has created some superior Positron PIC BASIC Compilers. The rationale for the plural (“compilers” as a substitute of “compiler”) is that Les provides totally different compilers for 8-bit and 16-bit PICs. The related built-in design surroundings (IDE) routinely selects the suitable compiler while you inform it which PIC you need to program utilizing a particular assertion in your BASIC supply code. The entire package and caboodle (the compilers and IDE), together with all future upgrades and enhancements, might be yours for a one-time cost of £39.99 (which equates to ~$50 within the USA). All I can say is that this was probably the greatest $50 investments I ever made.
As an apart, a number of older programmers declare they’ll create higher code in meeting than others can obtain utilizing higher-level languages like BASIC or C (by “higher” they meant utilizing much less reminiscence and/or fewer clock cycles). This was presumably true within the early days when interpreters and compilers had been of their infancy, however these instruments have developed dramatically through the years, together with their means to use optimizations many human programmers wouldn’t even consider. However the actual benefit of utilizing higher-level languages is that they allow you to discover a number of eventualities within the answer area, which isn’t one thing you need to attempt doing if you’re capturing your code in meeting language. Nonetheless, we digress…
As quickly as I’d downloaded and put in my Positron PIC BASIC Compilers, I used the context-sensitive editor within the IDE to take one of many smaller packages I’d undertaken to help, replicate it in BASIC, and compile it into machine code. The outcomes, that are saved in .hex recordsdata (i.e., textual content recordsdata in hexadecimal format) are proven under.
Hex file dimension comparability: unique (left) vs. new (proper). CLIVE “MAX” MAXFIELD
I blurred this picture to guard the harmless (i.e., me). As we see, my model on the correct is considerably extra concise than the unique model on the left, although that model had been painstakingly handcrafted by an meeting language guru. The good factor is that in the event you have a look at the .asm (meeting) recordsdata generated by the Positron Compiler along with the .hex recordsdata, you’ll see that the compiler has taken care of all obligatory reminiscence financial institution switching and management register bit flipping for you.
In my earlier column I launched the PICkit programmer. This at present is available in 4 flavors: PICkit2, PICKit3, PICKit4, and PICKit5. I initially opted to make use of the PICKit3 as a result of it was a fraction of the value of a PICkit4, and it’s nonetheless cheaper than the just lately introduced PICKit5. Nonetheless, as we’ll see in my subsequent column, for causes that will likely be revealed, I’ve now moved to utilizing a PICkit2.
The parents at Microchip provide the MPLAB X IDE, which gives superior capabilities and options for skilled programmers. Sadly, it makes my head damage. Fortunately, additionally they have one thing referred to as MPLAB X IPE, the place IPE stands for built-in programming surroundings, which is targeted on programming and gives a a lot less complicated interface than the IDE.
I can now not bear in mind the reasoning, however I used to be advised by somebody that the model I wanted to make use of was MPLAB X IPE v6.00. The good factor is that the oldsters at Microchip present all the sooner variations of their instruments without spending a dime obtain.
As I discussed in my earlier column, utilizing the PICkit of your selection, you may program a PIC straight on a breadboard. Nonetheless, one weekend when he had some spare time on his arms, Joe designed a particular improvement and take a look at board for me as proven under.
My super-duper PIC programming board. CLIVE “MAX” MAXFIELD
The cable on the right-hand facet plugs into my PICkit programmer. The cyan-colored zero insertion power (ZIF) socket within the center helps velocity issues if I’m programming a bunch of elements. The ability provide part within the decrease right-hand nook might be fed from a 7 V to fifteen V AC or DC supply, and I can choose between 5 V and three.3 V to energy the board relying on the PIC elements I’m working with. There are a bunch of different options, however these are all associated to my very own particular necessities.
All we’d like bear in mind for the second is that we are able to seize our PIC supply code utilizing the Positron BASIC Compilers, which generate each .asm and .hex output recordsdata. And we are able to use the free MPLAB X IPE v6.00 at the side of one of many PICKits to add hex recordsdata into our PICs, no matter whether or not these gadgets are on an affordable and cheerful breadboard or a particular improvement and take a look at board just like the one proven above.
In my subsequent column, we are going to contemplate the programming of PICs in a bit extra element. Additionally, we are going to take our PIC improvement surroundings to the following degree by way of… however no, you’ll simply have to attend. Till that frabjous day, I welcome your feedback, questions, and recommendations.
And be sure you learn Half 1.