Why you need a pic compiler.
You need a pic compiler because it
simplifies programming pic microcontrollers.
It's much easier working with a high level language (such as C or BASIC) because it uses words that are easily recognized by humans and this typically results in a program that is much easier to write and understand.
In fact I would say that it takes about 1/10th the time to write a high level language program compared to writing the same program in assembler. This is because the complier hides the low level detail letting you get on with solving the problem.
With a pic compiler you can visually describe the program flow using tabs and spaces making it easy to see how a program works.
A good pic compiler also includes a help system for looking up elements of the language and a simulator which makes debugging the code easy.
Compiler or Interpreter ?
The other great advantage that a compiler has over interpreted language compilers is that you can get the maximum performance out of the chip. Compilers create executable hex files that run directly on the chip whereas interpreted language systems use an intermediate step and store an intermediate program either on the chip or in a separate storage device. Interpreting code from the memory takes time so you get less performance out of it.
The one advantage that the interpreted system has is that since the memory device is separate it can be made as large as required - so you can create big programs.
The fact that the interpreted language is slow is not advertised and you can tell the difference as a compiler will work with any chip but for an interpreted system you must buy a ready programmed chip - probably on a pcb. Compilers will work with any chip as long as you have a programmer.
And this brings us to the next point - If you want to create lots of projects then for a compiler you have a one time non recurring cost - the compiler itself. But for an interpreted system you must keep buying the pcb and support chips - so your projects will cost a lot more to make.
What is a pic compiler ?
A pic compiler (or any compiler) is a program that translates one computer program into another. Source code written in a high level language is translated into machine code that the microcontroller understands. This simplifies the task since you do not have to write assembler - you are only concentrating on the high level language.
Here is an example of a 'C' program and its assembler output i.e. the (machine code) output of the pic compiler :
Example output
Example high level 'C' language source code for a pic compiler:
void main() {
PORTB = 0;
TRISB = 0;
do {
switch (PORTB) {
case 0x00: PORTB = 0xFF; break;
case 0xFF: PORTB = 0x00;
}
Delay_ms(1000);
} while (1);
}
Output generated by the compiler i.e. machine code (and assembler) :
This following text is quite long please scroll down to past it to see comments about it and continue.
; ADDRESS OPCODE ASM
; ----------------------------------------------
$0000 $2804 GOTO _main
$0004 $ _main:
$0004 $1303 BCF STATUS,RP1
$0005 $1283 BCF STATUS,RP0
$0006 $0186 CLRF PORTB
$0007 $1683 BSF STATUS,RP0
$0008 $0186 CLRF TRISB
$0009 $ L_main_0:
$0009 $1303 BCF STATUS,RP1
$000A $1683 BSF STATUS,RP0
$000B $2812 GOTO L_main_2
$000C $ L_main_4:
$000C $30FF MOVLW 255
$000D $1283 BCF STATUS,RP0
$000E $0086 MOVWF PORTB
$000F $281F GOTO L_main_3
$0010 $ L_main_5:
$0010 $0186 CLRF PORTB
$0011 $281F GOTO L_main_3
$0012 $ L_main_2:
$0012 $1303 BCF STATUS,RP1
$0013 $1283 BCF STATUS,RP0
$0014 $01F0 CLRF STACK_0
$0015 $0870 MOVF STACK_0,W
$0016 $0206 SUBWF PORTB,W
$0017 $1903 BTFSC STATUS,Z
$0018 $280C GOTO L_main_4
$0019 $30FF MOVLW 255
$001A $00F0 MOVWF STACK_0
$001B $0870 MOVF STACK_0,W
$001C $0206 SUBWF PORTB,W
$001D $1903 BTFSC STATUS,Z
$001E $2810 GOTO L_main_5
$001F $ L_main_3:
$001F $300B MOVLW 11
$0020 $00F0 MOVWF STACK_0
$0021 $30FF MOVLW 255
$0022 $00F1 MOVWF STACK_1
$0023 $30FF MOVLW 255
$0024 $00F2 MOVWF STACK_2
$0025 $0BF0 DECFSZ STACK_0,F
$0026 $2828 GOTO $+2
$0027 $282F GOTO $+8
$0028 $0BF1 DECFSZ STACK_1,F
$0029 $282B GOTO $+2
$002A $282E GOTO $+4
$002B $0BF2 DECFSZ STACK_2,F
$002C $282B GOTO $-1
$002D $2828 GOTO $-5
$002E $2825 GOTO $-9
$002F $3033 MOVLW 51
$0030 $00F3 MOVWF STACK_3
$0031 $30FF MOVLW 255
$0032 $00F4 MOVWF STACK_4
$0033 $0BF3 DECFSZ STACK_3,F
$0034 $2836 GOTO $+2
$0035 $2839 GOTO $+4
$0036 $0BF4 DECFSZ STACK_4,F
$0037 $2836 GOTO $-1
$0038 $2833 GOTO $-5
$0039 $3088 MOVLW 136
$003A $00F5 MOVWF STACK_5
$003B $0BF5 DECFSZ STACK_5,F
$003C $283B GOTO $-1
$003D $0000 nop
$003E $0000 nop
$003F $2809 GOTO L_main_0
$0040 $ L_main_1:
$0040 $2840 GOTO $
The second column is the actual machine code that the microcontroller reads (opcode) while the third and fourth are the assembler translation (a bit more readable than the opcode).
As you can see the source code is much more compact than the assembler output and is far more readable. You can also see that it is doing something with PORTB and there is a delay included whereas with the machine code you can't really tell what is going on and there are a lot more lines of code to work through.
Of course comments are (hopefully) included for hand written assembler but there are still the same number of statements to understand.
What is the advantage of a pic compiler ?
The compiler's language lets you...
- Code applications more quickly - less lines of code than assembler.
- Write programs that are easier to understand.
- Use ready made libraries - you don't need to write as much code.
- Concentrate on the task - it manages low level hardware details.
- Use the same source code for different target micros.
- Work only in the high level language - no need for assembler.
- Read the code easily (see layout of the example above).
- Program different microcontrollers in the same family e.g. PIC.
- Learn a standard language such as C to use in a profession.
A good pic compiler will also let you...
- Simulate the source code - no need for assembler.
- View procedure memory usage showing the effect of code changes.
Types of pic compiler
There are two main languages used for pic microcontroller programming BASIC and C. These are by far the most popular languages and there are many compilers with varying cost/performance ratios. Other less popular pic compilers are PASCAL, FORTH and JAL.
High level language compilers available for the PIC Micro:
- C The most widely used programming language.
- BASIC Beginners All Purpose Symbolic Instruction Code.
- PASCAL Based on ALGOL and named in honor of Blaise Pascal.
- FORTH by Chuck Moore named as suitable for fourth generation computers.
- JAL Just Another Language by Wouter van Ooijen (PASCAL based).
Key features:
ANSI standardized
- C has an ANSI standard, lots of code available, Widely used.
- PASCAL has an ANSI standard, lots of code available, Widely used.
- FORTH has an ANSI standard.
- BASIC Major drawback - it doesn't follow a standard.
- JAL No standard - but there is only one compiler so it does not matter.
Having an ANSI standard associated with a language means that any pic compiler can be used to compile the source code - and the compiler will accept that source code regardless of the target micro family. Note you may have to make some adjustments e.g. register names, hardware operation etc.
It is a problem for BASIC as there is no standard - so if you write BASIC source code using one compiler it most likely will not work if you use a different compiler.
Ease of use
- C easy - difficult.
- PASCAL easy - difficult.
- FORTH very difficult.
- BASIC easy.
- JAL easy.
Main benefit:
- C Can access low level hardware, structured layout.
- PASCAL Easy to write, structured layout.
- FORTH compact code, and very fast - not as structured as above.
- BASIC Easy to write, can be structured depending on compiler.
- JAL based on Pascal, structured layout.
Main Disadvantage:
- C Depending on how it is written can be extremely cryptic.
- PASCAL Lengthy to write and distinguishes between procedures/Functions.
- FORTH Uses Reverse Polish Notation and code is very difficult to read.
- BASIC Does not follow a standard.
- JAL Only one compiler - no other choice i.e. no support.
Availability for pic micro:
- C A large number of compilers are available.
- PASCAL Only one or two compilers available.
- FORTH A few compilers available.
- BASIC A large number of compilers are available.
- JAL Only one compiler available.
What to look for in a compiler.
When starting out you should use a pic compiler that has a text editor and simulator built in known as an IDE (Integrated Development Environment).
A good help system will give examples of the language which saves getting a text book out (Some help files also have examples of typical hardware solutions).
Obviously cost is a consideration - the best are going to cost more. However you can use some that are very low cost and perform well (see below).
Recommendations.
The compiler I recommend is Mikroelectronica's FREE pic compiler for the C language which generously gives access to all the supported PIC microcontrollers and allows 2k words of code space for free. So you can fully program a 16F84, half program a 16F88, quarter program a 16F877A.
Note: You can get a lot of functionality out of this compiler even in 2k.
This compiler seems to me to be the best one to start out with as it is not too complex and has a clean intuitive interface. Once you start using it you will find it easy to navigate around - and there is tons of useful information in the help file.
It has a useful debugger/simulator and has memory and function code size viewers.
There is also context help. If you select a built-in function pressing F1 takes you to the definition and use of that function - great.
They also have a support forum which seems to get answers quickly and there is also a user forum where you can ask each other questions on their pic compiler.
They also do a BASIC and PASCAL compiler with the same 2k limit.