Possible Applications & Pros/Cons
Many students choose PICs for their projects because PICs are relatively inexpensive, easy to use, and (re)programmable. However, once you start working with a PIC, you might be disappointed with the performance when programming in C. If you want the highest possible performance, you'll have to write your program in assembly language. Well-written ASM code might run about 2 to 10 times faster than C code. Also, you can very accurately predict how long your code will take to run (see tips & trick below). While assembly language is more difficult to code, it can be done.
- Faster execution
- Very predictable performance
- Difficult to code
Possible applications for ASM programming include any moderately complex project where performance is important. For example, Fall 2007's project 1 used a PIC programmed in assembly language to implement a finite state machine.
It's a good idea to try programming in C first to get familiar with the PIC, compiler, and programmer. The are lots of guides and lots of sample code to look at for C programming help. Once you're confident with C programming, try ASM.
Tutorials and References
Spend the time to read and understand these 13 excellent tutorials. You'll also need to refer to the 16F877A PIC Datasheet. You can also take a look at the code from past projects, but this is not a good way to learn programming.
Several I/O pins can be used for several purposes, such as digital I/O or an analog input. If, for example, you want to use some PORT A pins as digital inputs or outputs, you'll first have to disable the analog input capability. Referring to page 43 of the datasheet linked above, here's what you would add to the beginning of your code:
- BSF STATUS, RP0
Also refer to the Instruction Set beginning on page 162 of the datasheet linked above. By referring to the Cycles column on page 162, you can figure out exactly how many clock cycles your code will take to execute. Note that if you're using a 20 MHz oscillator, each clock cycle here is 200 ns.