Tuesday, October 6, 2009

Choosing a Programming Language for Your Microcontroller

Assembler

Assembler is the most obvious language that you'll consider using as you probably won't need to buy any other tools to use it. More than likely the manufacturer will provide an assembler for the chip and you won't need any books as all the instructions are in the datasheet and you just start coding.

This route to programming is very easy but you may be setting yourself up for problems later on as:

  1. It is trivial to write short programs.
  2. It becomes progressively more difficult to write large programs.
  3. It seems to be the best option as it gives the fastest code.
  4. It seems to be the best option as it gives the smallest code.

Assembler: Fast & small code

There is no doubt that assembler gives the fastest and most optimized code (your brain is better at optimization than any compiler!) but assembler is difficult – typically you'll spend ten times as long writing assembler as you would writing in a high level language.

Assembler / Compiler Trade off

This is the trade off; to write the fastest most optimized code or to get the task solved more quickly.

Another problem with assembler is that to do even the most trivial task you have to think about every aspect of the code and all implications on registers and register flags.

Even making a microcontroller perform the most trivial task is difficult e.g. for making a loop in assembler you need to think about which register to use and which instructions all the while thinking about how those registers should not interact with the loop register/other registers etc.

Assembler: Problem - changing the target

Another difficulty is when you change from one microcontroller to another (even in the same device family) the assembler instructions may be changed e.g. more instructions to improve microcontroller performance. So you will have to learn an entirely different instruction set when moving either to a new target microcontroller or moving to a different device within the same family i.e. code re-use is not possible unless you stay with one microcontroller (or devices with a similar internal architecture).

High level languages

HLL: Retarget

Retargeting code to another microcontroller is easier since the HLL will know the details of the new target i.e. instruction set, fuses etc. All you need to worry about is the specific differences between the different microcontrollers (in the same family this will be setting up the internal peripherals).

The important point is that the HLL takes care of the assembler code needed to do the job.

HLL: Easy to understand.

The most useful aspect of a high level language (HLL) is that the language is written in a form you can easily understand – there are no cryptic assembler commands that you have to remember and most commands are made up of several machine code instructions – saving you coding effort (often there are built in libraries of code e.g. LCD driver, Serial port driver, I2C driver etc

So the HLL makes it easy to write code as it generates the correct assembler for the target microcontroller.

HLL : Whitespace

You can also make use of white space (areas of no code) to separate out the various operations within the program – typically assembler code is just one great big list that is really very difficult to read – I know there are comments but you need to comment almost every line so that someone else can

understand the code.

HLL: Task splitting

One of the best features of a HLL is that you can split tasks into separate functions that you can concentrate on them individually (as the HLL takes care of local variables etc.). For assembler even when using a call instruction you have to take care of preserving the register state – in the HLL it's all done

for you.

HLL: Code re-use

Once you learn the HLL you will find it easy to read code written by other people and you will be able to re-use code that you have already written whereas with assembler you will constantly need to analyze the code to see if it fits in with your new functions.

The only decision then is which high level language? There are really three contenders BASIC, C and Pascal – these are the most popular languages and for popular microcontrollers there will be an HLL compiler for each one. I'll just list the advantages and disadvantages of each

BASIC Advantages

  1. Very easy to learn and use.
  2. A BASIC compiler will produce code that runs fast as a C compiler.
  3. Many in built functions (depending on compiler).
  4. Very popular – large user base with many example programs.

BASIC Disadvantages

  1. Non standard language.
  2. If using an interpreted HLL will run very slowly.

Note: Because the language is not standardized it will be difficult to move code to a new processor target type.

Pascal Advantages

  1. Easy to learn and use.
  2. A Pascal compiler will produce code that runs fast as a C compiler.
  3. Many in built functions (depending on compiler).

Pascal Disadvantages

  1. Not as popular as C – so not as many compilers.
  2. A bit wordy – it was originally intended as a teaching language.
  3. Not as flexible as C.

C Advantages

  1. Compiled language - always runs fast.
  2. Standardized language (ANSI)- easier to port to different compilers / target devices.
  3. Many compilers available.
  4. Many in built functions (depending on compiler).
  5. Very popular – large user base with many example programs.
  6. Used in many different industries.
  7. Usable at the hardware level as well as higher abstraction levels (although C++ is better for very abstracted programming models).

C Disadvantages

  1. Hard to learn at first.
  2. Strong type checking means you spend time pleasing the compiler (although this protects you from making errors).

You can find more information from the website here and how to build a website like it here.

Copyright © John Main 2006

Free to distribute if the article is kept complete.

http://www.best-microcontroller-projects.com


PIC Micro Hardware Programming Methods

There are three ways to program a PIC microcontroller

  1. Using normal programming hardware (high volt programming HVP).
  2. Low volt programming (LVP).
  3. Bootloading.

The first two methods use the programming port of the PIC microcontroller labeled ICSP (In Circuit Serial Programming).

This port is shared between the existing pins of the microcontroller and after programming the pins revert back to normal microcontroller operation.

Note: To make ICSP work correctly you have to consider the effects and requirements of the ICSP programmer e.g. for HVP a high voltage exists at the Vpp pin (your circuit must be able to handle the high voltage - up to 13V). Also the loading for the other signals PGC and PGD must not be too high i.e. don't put an LED on these pins that uses 20mA - if you did the voltage levels would not be high enough at the inputs to the PIC for programming.

It's fairly easy to design for ICSP use by using isolation resistors to normal circuitry and choosing not to use heavy loads on these pins.

ICSP provides 6 connections from the pic ICSP programmer to your board as follows :

VPP - (or MCLRn) Programming voltage (usually 13V).

Vcc - Power (usually 5V).

GND Ground (zero volts).

PGD - Data usual port and connection RB7.

PGC - Clock usual port and connection RB6.

PGM - LVP enable usual port and connection RB3/RB4.

PIC Micro: High Volt Programming

To use the first method a hardware interface is needed or 'PIC programmer' to interface between the programming software (usually running on the PC) and the PIC chip. This hardware takes its information from the PC via one of three interfaces either:

  • The RS232 COM port
  • The Parallel port
  • The USB port

You choose the interface you want to use and then choose an appropriate PIC programmer. The PC then communicates with the hardware generating the serial (ICSP) signals to translate the PIC hex file into a serial data stream suitable for the target microcontroller.

Note: Almost all PIC microcontrollers use the ICSP interface so once you have a HVP you can program virtually any PIC microcontroller. e.g. you can program 12F675, 16F84, 16F88, 16F877(A), 18F2550, 18F452 etc.

There are several programs for programming PIC micos e.g. ICPROG and many different hardware programmers.

PIC Micro: Low volt programming (LVP)

LVP is exactly the same as HVP except:

  • The Vpp voltage is set to the normal supply voltage.
  • The PGM pin indicates programming mode.

Note: In this mode you can not use the PGM pin for anything else it is dedicated solely to LVP control.

Devices are manufactured with PGM mode enabled and the only way to turn off the PGM mode is to program it using an HVP programmer.

Note: Some PIC microcontrollers can only use the HVP method since for the LVP method you have to sacrifice one pin - PGM - (to tell the PIC Micro either that it is being programmed (high volts e.g. 5V) or that it is not being programmed (0V) ) and some PIC micros only have 8 pins e.g. 12F675. For this chip the PGM pin is not available so HVP is the only way.

The real benefit of using the LVP mode is that you can program several PIC Micros on a board without having to individually program each one - you could daisy chain each extra micro to a master micro which would then program each one in turn - and this is only possible since the Vpp signal is a normal logic level in LVP mode.

PIC Micro: Bootloading

Bootloading uses any available interface to load a program into program memory. It requires a bootstrap program to interpret the interface data and translate it into program memory instructions.

Note: Note only the newer devices that are capable of programming their own memory can use this method.

Typically a serial port is used for bootloading and the PIC micro bootstrap program will wait for a set time after power up listening on the serial port for a reserved word that tells the bootstrap program to start i.e. it listens for sequence of characters that is not normally used on the interface

Once it receives this sequence it enters bootstrap mode where a hex file is transmitted to the microcontroller over the interface. It interprets this and programs the memory of the microcontroller and then starts the program.

There are two issues with this method:

  1. You have to program the bootstrap code using HVP or LVP.
  2. It uses up some of the microcontroller resources.

Once programed it provides a convenient way of using the device as you won't need programming hardware anymore and one major benefit is that you can re-program a device without undoing the equipment e.g. if you boxed up you project you could still re-program it using the serial port!

You can find more information from the website here and how to build a website like it here.

Copyright © John Main 2006 Free to distribute if the article is kept complete.

http://www.best-microcontroller-projects.com