Joe's Hobby Electronics
  • Joe's Hobby Electronics
  • Electronics Blog
  • Contact Me

The great religious programming debate – PICs and Programming languages

30/8/2011

1 Comment

 
The one subject that rears its ugly head from time to time and is always a catalyst for a heated debate is “which programming language to use for PICs”, and I am talking about only PICs here, though this is probably the same for many of the embedded type CPUs available.

Assembler is the best to use as it allows the developer to get as close to the PIC hardware as possible, gives the most flexibility and offers the best performance, and this is true… to a point.
It’s also the most complicated as many simple tasks require a lot of assembler instructions, so there’s more you have to type, so more you can get wrong. On the flip side, you have the opportunity to fine tune the program and validate the need for every instruction within your program. Another problem is that every CPU manufacturer has their own assembler and instruction set and it can be a real pain moving from one CPU manufacturer to another. Often, the same manufacturers often use different assemblers and instruction sets for different CPUs that they make.

For the best performance and the tightest control, in the hands of a highly skilled programmer you can’t beat assembler, but you have to know exactly what your doing and you stand an equal chance of creating slow, bloated and buggy code.

“C” – is a higher level language than Assembler which means that it shields the programmer from some of the nitty gritty issues that Assembler has. It’s easy to read but like any programming language, if you don’t know what you’re doing, you can make a complete pigs ear of things.

The advocates of “C” will brandish around phrases and words like “ANIS compatible”, and portable. Unfortunately, in the PIC world, one also has to brandish around the word “rubbish” as well.  “C” as a programming language has only a few instructions associated with it. Most of the really cool features like talking to the A2D ports or the I2C hardware within the PIC are actually performed by 3rd party libraries; there is NO native support within the "C" language for this. Usually supplied with the language installation, some of these libraries are written in Assembler, some written in “C” but they aren’t standard across all “C” implementations. This means that “C” from Microchip is different from the “C” version from FED so you can’t just cut and paste source code and watch it re-compile without a page full of warnings or errors.

Now my favourite, BASIC.

I can hear the groans and moans about how BASIC is slow and bloated. RUBBISH !!!!

BASIC comes in two main flavours; compiled or interpreted. Interpreted BASIC’s are usually slower than compiled languages and that’s a fact, but you get a bit more flexibility. For example, you don’t always need a PC (or a PIC programmer) to program a PIC that’s running interpreted BASIC; a dumb terminal will often suffice. (Yes, you can get boot-loaders, but they still usually need a PC unless you use a PICKIT with it's on-the-go programming feature and these don't allow you to develop new code; just flash the device with a pre-built HEX file).
Also, interpreted programs are often simpler to debug as you can stop them mid way through execution and look at variable contents. However, speed and code size are usually more important than ease of development once you've passed the pure beginner stage.

Compiled BASICs are no slower than “C” programs and I’ve timed them to prove it !!

All the “C” compilers that I’ve found work in the same way. They parse your “C” source code, spit out a file containing PIC assembler, and then pass that assembler to the standard Microchip assembler to generate the .HEX file for the PIC. And why not? There's no point creating your own assembler as the Microchip one is maintained by the supplier, is free, and rather good at what it does.

The BASIC compilers tend to work in exactly the same way.

The ONLY thing that makes one compiler better than another is the tightness of the assembler code it produces and the quality of the associated libraries and code optimiser and this rule is programming language independant.  I compared Proton+ BASIC with Microchip “C” and found no difference in the speed of execution for some simple tasks.  Some differences were noted when using some library routines and there were swings and roundabouts with both.

Yes, it’s possible to write slow and bloated code in BASIC, but that’s also true in “C” or any programming language.

Some of the cool features like bit manipulation that are common in "C" but not often found in PC type BASICs are fully supported in PIC BASICs.

The bottom line is find a programming language you like, learn it well, and you should be able to write fast and efficient code.

The "thing" about BASIC; nobody expects it to be portable or "standard".
1 Comment

    Author

    A software and hardware engineer who loves retro computers.

    Archives

    January 2019
    April 2018
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    May 2015
    April 2015
    March 2015
    January 2015
    December 2014
    September 2014
    August 2014
    April 2014
    March 2014
    January 2014
    October 2013
    September 2013
    August 2013
    July 2013
    June 2013
    April 2013
    March 2013
    January 2013
    September 2012
    June 2012
    May 2012
    April 2012
    February 2012
    January 2012
    December 2011
    November 2011
    October 2011
    September 2011
    August 2011
    July 2011
    June 2011

    Categories

    All
    18f25k20
    18F25K22
    1-wire
    74hc595
    74ls595
    86-0325
    Alarm
    Aluminium Panel
    Amicus
    Amicus18
    Amicus Basic
    Amplifier
    Antex 1107
    Anti-static Mat
    Arduino
    Assembler
    Backlight
    Bargains
    Basic
    Bat
    Batdetector
    Bat Detector
    Bats
    Bench Audio Amplifier
    Bench Light
    Breadboard
    Bubbler
    Buck
    Bug
    "C"
    Cable
    Cable Gauge
    Cable Wire Size
    Capacitors
    Catalogue
    Clive Maxfield
    Clock
    Clocks
    Common Emitter
    Compiled
    Compiler
    Connector
    Crownhill
    Custom Computer
    Custom CPU
    Damage
    Develop
    Diptrace
    Double-sided
    Double-Sided PCB
    Dry-joint
    Ds1302
    DS1820
    Ds18b20
    Ebay
    Electronics And Circuits
    Eprom
    Erase
    Etch
    Evaporator
    Failed Capacitor
    Fake
    Fax Paper
    Ferric Chloride
    Fire Hazzard
    Firmware
    Florescent Tube
    Health And Safety
    Hidden Part Numbers
    High-Voltage
    Hv
    Incubator
    Instruction Set
    Inverter
    Kickstarter
    Lcd
    LM2596
    LM386
    Maxim
    Maximum Current Rating
    Mega
    Mit
    MITx 6.002
    MK4
    Molex
    Mosfet
    NE555
    Nixie
    Nixie Clock
    Npn
    Pcb
    Pic
    Pic Basic
    Pickit2
    Pickit3
    Pic Programmer
    Pixy
    Poster
    Pre-amp
    Prices
    Programming
    Project Box
    Project Case
    Project Enclosure
    Psu
    Psu Failure
    Qfn
    Rapid Electronics
    Raspberry Pi
    Regulated 5v Psu
    Regulator
    Repair
    Review
    SCC
    SCC MK4
    Seven Segment Display
    Shift Register
    Silient 700
    Slow Progress
    Smoke
    Smps
    Soldering
    Speed
    Stepper Motor
    Supression
    Swg
    Switch Box
    Tank
    Tantalum Capacitor
    Temperature
    Temperature Sensor
    Texas Instruments
    Thermometer
    Timer
    Tools
    Traffic Lights
    Transistor
    Usb
    Uv
    Vbe
    Vcb
    Vce
    Wash
    Weller
    Wirecutters
    Wire Cutters
    Wire Strippers
    Wiring
    Wp3eu
    X2 Capacitor
    X Capacitor
    Y Capacitor

    RSS Feed

(C) Joe Farr, 2011 - 2018 - Email: HobbyElectronics@kcsl.uk.com