What's the simplest way to programa PIC Micro?
"Use a PIC Compiler"...
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.
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.
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.
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 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 $
This shows you how much easier
it is using a compiled language over
raw assembler.
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.
The compiler's language lets you...
A good pic compiler will also let you...
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.
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.
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.
Jump from pic compiler page to
Best-Microcontroller-Projects Home Page
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.