Choosing a Programming Language for Your Microcontroller

AssemblerYou can also make use of white space (areas of
Assembler is the most obvious language that you'llno code) to separate out the various operations
consider using as you probably won't need to buywithin the program - typically assembler code is
any other tools to use it. More than likely thejust one great big list that is really very difficult to
manufacturer will provide an assembler for theread - I know there are comments but you need
chip and you won't need any books as all theto comment almost every line so that someone
instructions are in the datasheet and you justelse canunderstand the code.
start coding.HLL: Task splitting
This route to programming is very easy but youOne of the best features of a HLL is that you
may be setting yourself up for problems later oncan split tasks into separate functions that you
as:can concentrate on them individually (as the HLL
takes care of local variables etc.). For assembler
1. It is trivial to write short programs.even when using a call instruction you have to
2. It becomes progressively more difficult to writetake care of preserving the register state - in the
large programs.HLL it's all donefor you.
3. It seems to be the best option as it gives theHLL: Code re-use
fastest code.Once you learn the HLL you will find it easy to
4. It seems to be the best option as it gives theread code written by other people and you will be
smallest code.able to re-use code that you have already written
Assembler: Fast & small codewhereas with assembler you will constantly need
There is no doubt that assembler gives theto analyze the code to see if it fits in with your
fastest and most optimized code (your brain isnew functions.
better at optimization than any compiler!) butThe only decision then is which high level
assembler is difficult - typically you'll spend tenlanguage? There are really three contenders
times as long writing assembler as you wouldBASIC, C and Pascal - these are the most popular
writing in a high level language.languages and for popular microcontrollers there
Assembler / Compiler Trade offwill be an HLL compiler for each one. I'll just list
This is the trade off; to write the fastest mostthe advantages and disadvantages of each
optimized code or to get the task solved moreBASIC Advantages
quickly.
Another problem with assembler is that to do1. Very easy to learn and use.
even the most trivial task you have to think2. A BASIC compiler will produce code that runs
about every aspect of the code and allfast as a C compiler.
implications on registers and register flags.3. Many in built functions (depending on compiler).
Even making a microcontroller perform the most4. Very popular - large user base with many
trivial task is difficult e.g. for making a loop inexample programs.
assembler you need to think about which registerBASIC Disadvantages
to use and which instructions all the while thinking
about how those registers should not interact1. Non standard language.
with the loop register/other registers etc.2. If using an interpreted HLL will run very slowly.
Assembler: Problem - changing the targetNote: Because the language is not standardized it
Another difficulty is when you change from onewill be difficult to move code to a new processor
microcontroller to another (even in the sametarget type.
device family) the assembler instructions may bePascal Advantages
changed e.g. more instructions to improve
microcontroller performance. So you will have to1. Easy to learn and use.
learn an entirely different instruction set when2. A Pascal compiler will produce code that runs
moving either to a new target microcontroller orfast as a C compiler.
moving to a different device within the same3. Many in built functions (depending on compiler).
family i.e. code re-use is not possible unless youPascal Disadvantages
stay with one microcontroller (or devices with a
similar internal architecture).1. Not as popular as C - so not as many
High level languagescompilers.
HLL: Retarget2. A bit wordy - it was originally intended as a
Retargeting code to another microcontroller isteaching language.
easier since the HLL will know the details of the3. Not as flexible as C.
new target i.e. instruction set, fuses etc. All youC Advantages
need to worry about is the specific differences
between the different microcontrollers (in the1. Compiled language - always runs fast.
same family this will be setting up the internal2. Standardized language (ANSI)- easier to port to
peripherals).different compilers / target devices.
The important point is that the HLL takes care of3. Many compilers available.
the assembler code needed to do the job.4. Many in built functions (depending on compiler).
HLL: Easy to understand.5. Very popular - large user base with many
The most useful aspect of a high level languageexample programs.
(HLL) is that the language is written in a form you6. Used in many different industries.
can easily understand - there are no cryptic7. Usable at the hardware level as well as higher
assembler commands that you have toabstraction levels (although C++ is better for
remember and most commands are made up ofvery abstracted programming models).
several machine code instructions - saving youC Disadvantages
coding effort (often there are built in libraries of
code e.g. LCD driver, Serial port driver, I2C driver1. Hard to learn at first.
etc2. Strong type checking means you spend time
So the HLL makes it easy to write code as itpleasing the compiler (although this protects you
generates the correct assembler for the targetfrom making errors).
microcontroller.You can find more information from the website
HLL : Whitespacehere and how to build a website like it here.