2.1 Programming Languages



The microcontroller executes the program loaded in its Flash memory. It is a so called executable code which consists of a seemingly bizarre sequence of zeros and ones. Depending on the microcontroller’s architecture, this binary code is organized in 12-, 14- or 16-bit wide words. Every word is considered by the CPU as an instruction to be executed during the operation of the microcontroller. As it is much easier for us to deal with hexadecimal numeric system, the executable code is usually represented as a sequence of hexadecimal numbers called a Hex code which, long time ago, used to be written by the programmer. All instructions that the microcontroller can recognize and execute are collectively known as the Instruction set. For PIC microcontrollers with 14-bit wide program words, the instruction set includes 35 different instructions.

As the writing of executable code was endlessly tiring, the first high-level programming language called assembly language was created. It made the process of programming a bit more complicated, but on the other hand the process of writing program stopped being a nightmare. Assembly instructions consist of meaningful abbreviations which are compiled into executable code by means of a special program installed on a PC called assembler. It compiles instruction by instruction without optimization. The main advantages of assembly language are its simplicity and the fact that each program instruction matches only one memory location. In other words, assembly language enables a complete control of all processes being under way within the microcontroller, which still makes it popular nowadays.

On the other hand, programs are always executed at high speeds and in most cases it is not necessary to know in detail what is going on within the microcontroller. Despite all good attributes of the assembly language, programmers have always needed a programming language similar to the language they use in everyday speech. Finally, high-level programming languages, including Basic, have been created. The main advantage of these languages is a simplicity of program writing. Several assembly instructions are now replaced by one statement in Basic. The programmer is not required to be familiar with the instruction set of the microcontroller in use any more. It is no longer possible to know how each statement is executed, but it doesn’t matter anyway. In case it does, the problem is solved by adding a sequence written in assembly language to the program.


Similar to assembly language, a specialized program installed on the PC is in charge of compiling program into machine code. Unlike assembler, compilers for high-level programming languages create an executable code which is not always the shortest possible.


Figure above gives a rough illustration of what is going on during the process of compiling a program written in Basic into a hex code.

Here is an example of a simple program written in Basic:



If you have any experience in writing programs for PIC microcontrollers in assembly language, then you are probably familiar with the other side of the medal of RISC architecture – the lack of instructions. For example, there is no appropriate instruction for multiplying two numbers. Of course, there is a way to solve this issue owing to mathematics which enables you to perform complex operations by breaking them into a number of simple ones. Accordingly, multiplication can be easily substituted by successive addition (a x b = a + a + a + … + a). And here we are, just at the beginning of a very long story… Still there is no reason to be worried about as far as you use one of the high-level programming languages, such as Basic, as the compiler will automatically find a solution to these and similar issues. Simply write a*b.